1/* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22/** 23 Filename: word_boundary.js 24 Description: 'Tests regular expressions containing \b and \B' 25 26 Author: Nick Lerissa 27 Date: March 10, 1998 28*/ 29 30 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; 31 var VERSION = 'no version'; 32 startTest(); 33 var TITLE = 'RegExp: \\b and \\B'; 34 35 writeHeaderToLog('Executing script: word_boundary.js'); 36 writeHeaderToLog( SECTION + " "+ TITLE); 37 38 var count = 0; 39 var testcases = new Array(); 40 41 // 'cowboy boyish boy'.match(new RegExp('\bboy\b')) 42 testcases[count++] = new TestCase ( SECTION, "'cowboy boyish boy'.match(new RegExp('\\bboy\\b'))", 43 String(["boy"]), String('cowboy boyish boy'.match(new RegExp('\\bboy\\b')))); 44 45 var boundary_characters = "\f\n\r\t\v~`!@#$%^&*()-+={[}]|\\:;'<,>./? " + '"'; 46 var non_boundary_characters = '1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 47 var s = ''; 48 var i; 49 50 // testing whether all boundary characters are matched when they should be 51 for (i = 0; i < boundary_characters.length; ++i) 52 { 53 s = '123ab' + boundary_characters.charAt(i) + '123c' + boundary_characters.charAt(i); 54 55 testcases[count++] = new TestCase ( SECTION, 56 "'" + s + "'.match(new RegExp('\\b123[a-z]\\b'))", 57 String(["123c"]), String(s.match(new RegExp('\\b123[a-z]\\b')))); 58 } 59 60 // testing whether all non-boundary characters are matched when they should be 61 for (i = 0; i < non_boundary_characters.length; ++i) 62 { 63 s = '123ab' + non_boundary_characters.charAt(i) + '123c' + non_boundary_characters.charAt(i); 64 65 testcases[count++] = new TestCase ( SECTION, 66 "'" + s + "'.match(new RegExp('\\B123[a-z]\\B'))", 67 String(["123c"]), String(s.match(new RegExp('\\B123[a-z]\\B')))); 68 } 69 70 s = ''; 71 72 // testing whether all boundary characters are not matched when they should not be 73 for (i = 0; i < boundary_characters.length; ++i) 74 { 75 s += boundary_characters[i] + "a" + i + "b"; 76 } 77 s += "xa1111bx"; 78 79 testcases[count++] = new TestCase ( SECTION, 80 "'" + s + "'.match(new RegExp('\\Ba\\d+b\\B'))", 81 String(["a1111b"]), String(s.match(new RegExp('\\Ba\\d+b\\B')))); 82 83 testcases[count++] = new TestCase ( SECTION, 84 "'" + s + "'.match(/\\Ba\\d+b\\B/)", 85 String(["a1111b"]), String(s.match(/\Ba\d+b\B/))); 86 87 s = ''; 88 89 // testing whether all non-boundary characters are not matched when they should not be 90 for (i = 0; i < non_boundary_characters.length; ++i) 91 { 92 s += non_boundary_characters[i] + "a" + i + "b"; 93 } 94 s += "(a1111b)"; 95 96 testcases[count++] = new TestCase ( SECTION, 97 "'" + s + "'.match(new RegExp('\\ba\\d+b\\b'))", 98 String(["a1111b"]), String(s.match(new RegExp('\\ba\\d+b\\b')))); 99 100 testcases[count++] = new TestCase ( SECTION, 101 "'" + s + "'.match(/\\ba\\d+b\\b/)", 102 String(["a1111b"]), String(s.match(/\ba\d+b\b/))); 103 104 105 function test() 106 { 107 for ( tc=0; tc < testcases.length; tc++ ) { 108 testcases[tc].passed = writeTestCaseResult( 109 testcases[tc].expect, 110 testcases[tc].actual, 111 testcases[tc].description +" = "+ 112 testcases[tc].actual ); 113 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 114 } 115 stopTest(); 116 return ( testcases ); 117 } 118 119 test(); 120