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
24 Description: 'Tests regular expressions containing \d'
30 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
31 var VERSION = 'no version';
33 var TITLE = 'RegExp: \\d';
35 writeHeaderToLog('Executing script: digit.js');
36 writeHeaderToLog( SECTION + " "+ TITLE);
39 var testcases = new Array();
41 var non_digits = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\f\n\r\t\v~`!@#$%^&*()-+={[}]|\\:;'<,>./? " + '"';
43 var digits = "1234567890";
45 // be sure all digits are matched by \d
46 testcases[count++] = new TestCase ( SECTION,
47 "'" + digits + "'.match(new RegExp('\\d+'))",
48 String([digits]), String(digits.match(new RegExp('\\d+'))));
50 // be sure all non-digits are matched by \D
51 testcases[count++] = new TestCase ( SECTION,
52 "'" + non_digits + "'.match(new RegExp('\\D+'))",
53 String([non_digits]), String(non_digits.match(new RegExp('\\D+'))));
55 // be sure all non-digits are not matched by \d
56 testcases[count++] = new TestCase ( SECTION,
57 "'" + non_digits + "'.match(new RegExp('\\d'))",
58 null, non_digits.match(new RegExp('\\d')));
60 // be sure all digits are not matched by \D
61 testcases[count++] = new TestCase ( SECTION,
62 "'" + digits + "'.match(new RegExp('\\D'))",
63 null, digits.match(new RegExp('\\D')));
65 var s = non_digits + digits;
67 // be sure all digits are matched by \d
68 testcases[count++] = new TestCase ( SECTION,
69 "'" + s + "'.match(new RegExp('\\d+'))",
70 String([digits]), String(s.match(new RegExp('\\d+'))));
72 var s = digits + non_digits;
74 // be sure all non-digits are matched by \D
75 testcases[count++] = new TestCase ( SECTION,
76 "'" + s + "'.match(new RegExp('\\D+'))",
77 String([non_digits]), String(s.match(new RegExp('\\D+'))));
81 // be sure all digits match individually
82 for (i = 0; i < digits.length; ++i)
84 s = 'ab' + digits[i] + 'cd';
85 testcases[count++] = new TestCase ( SECTION,
86 "'" + s + "'.match(new RegExp('\\d'))",
87 String([digits[i]]), String(s.match(new RegExp('\\d'))));
88 testcases[count++] = new TestCase ( SECTION,
89 "'" + s + "'.match(/\\d/)",
90 String([digits[i]]), String(s.match(/\d/)));
92 // be sure all non_digits match individually
93 for (i = 0; i < non_digits.length; ++i)
95 s = '12' + non_digits[i] + '34';
96 testcases[count++] = new TestCase ( SECTION,
97 "'" + s + "'.match(new RegExp('\\D'))",
98 String([non_digits[i]]), String(s.match(new RegExp('\\D'))));
99 testcases[count++] = new TestCase ( SECTION,
100 "'" + s + "'.match(/\\D/)",
101 String([non_digits[i]]), String(s.match(/\D/)));
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 );