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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
23 Filename: word_boundary.js
24 Description: 'Tests regular expressions containing \b and \B'
30 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
31 var VERSION = 'no version';
33 var TITLE = 'RegExp: \\b and \\B';
35 writeHeaderToLog('Executing script: word_boundary.js');
36 writeHeaderToLog( SECTION + " "+ TITLE);
39 var testcases = new Array();
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'))));
45 var boundary_characters = "\f\n\r\t\v~`!@#$%^&*()-+={[}]|\\:;'<,>./? " + '"';
46 var non_boundary_characters = '1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
50 // testing whether all boundary characters are matched when they should be
51 for (i = 0; i < boundary_characters.length; ++i)
53 s = '123ab' + boundary_characters.charAt(i) + '123c' + boundary_characters.charAt(i);
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'))));
60 // testing whether all non-boundary characters are matched when they should be
61 for (i = 0; i < non_boundary_characters.length; ++i)
63 s = '123ab' + non_boundary_characters.charAt(i) + '123c' + non_boundary_characters.charAt(i);
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'))));
72 // testing whether all boundary characters are not matched when they should not be
73 for (i = 0; i < boundary_characters.length; ++i)
75 s += boundary_characters[i] + "a" + i + "b";
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'))));
83 testcases[count++] = new TestCase ( SECTION,
84 "'" + s + "'.match(/\\Ba\\d+b\\B/)",
85 String(["a1111b"]), String(s.match(/\Ba\d+b\B/)));
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)
92 s += non_boundary_characters[i] + "a" + i + "b";
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'))));
100 testcases[count++] = new TestCase ( SECTION,
101 "'" + s + "'.match(/\\ba\\d+b\\b/)",
102 String(["a1111b"]), String(s.match(/\ba\d+b\b/)));
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 ";
116 return ( testcases );