Debian Codegraph @

rhino/testsrc/tests/js1_2/regexp/alphanumeric.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'alphanumeric.js';
7
8 /**
9 Filename: alphanumeric.js
10 Description: 'Tests regular expressions with \w and \W special characters'
11
12 Author: Nick Lerissa
13 Date: March 10, 1998
14 */
15
16 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
17 var VERSION = 'no version';
18 startTest();
19 var TITLE = 'RegExp: \\w and \\W';
20
21 writeHeaderToLog('Executing script: alphanumeric.js');
22 writeHeaderToLog( SECTION + " " + TITLE);
23
24 var non_alphanumeric = "~`!@#$%^&*()-+={[}]|\\:;'<,>./?\f\n\r\t\v " + '"';
25 var alphanumeric = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
26
27 // be sure all alphanumerics are matched by \w
28 new TestCase ( SECTION,
29 "'" + alphanumeric + "'.match(new RegExp('\\w+'))",
30 String([alphanumeric]), String(alphanumeric.match(new RegExp('\\w+'))));
31
32 // be sure all non-alphanumerics are matched by \W
33 new TestCase ( SECTION,
34 "'" + non_alphanumeric + "'.match(new RegExp('\\W+'))",
35 String([non_alphanumeric]), String(non_alphanumeric.match(new RegExp('\\W+'))));
36
37 // be sure all non-alphanumerics are not matched by \w
38 new TestCase ( SECTION,
39 "'" + non_alphanumeric + "'.match(new RegExp('\\w'))",
40 null, non_alphanumeric.match(new RegExp('\\w')));
41
42 // be sure all alphanumerics are not matched by \W
43 new TestCase ( SECTION,
44 "'" + alphanumeric + "'.match(new RegExp('\\W'))",
45 null, alphanumeric.match(new RegExp('\\W')));
46
47 var s = non_alphanumeric + alphanumeric;
48
49 // be sure all alphanumerics are matched by \w
50 new TestCase ( SECTION,
51 "'" + s + "'.match(new RegExp('\\w+'))",
52 String([alphanumeric]), String(s.match(new RegExp('\\w+'))));
53
54 s = alphanumeric + non_alphanumeric;
55
56 // be sure all non-alphanumerics are matched by \W
57 new TestCase ( SECTION,
58 "'" + s + "'.match(new RegExp('\\W+'))",
59 String([non_alphanumeric]), String(s.match(new RegExp('\\W+'))));
60
61 // be sure all alphanumerics are matched by \w (using literals)
62 new TestCase ( SECTION,
63 "'" + s + "'.match(/\w+/)",
64 String([alphanumeric]), String(s.match(/\w+/)));
65
66 s = alphanumeric + non_alphanumeric;
67
68 // be sure all non-alphanumerics are matched by \W (using literals)
69 new TestCase ( SECTION,
70 "'" + s + "'.match(/\W+/)",
71 String([non_alphanumeric]), String(s.match(/\W+/)));
72
73 s = 'abcd*&^%$$';
74 // be sure the following test behaves consistently
75 new TestCase ( SECTION,
76 "'" + s + "'.match(/(\w+)...(\W+)/)",
77 String([s , 'abcd' , '%$$']), String(s.match(/(\w+)...(\W+)/)));
78
79 var i;
80
81 // be sure all alphanumeric characters match individually
82 for (i = 0; i < alphanumeric.length; ++i)
83 {
84 s = '#$' + alphanumeric[i] + '%^';
85 new TestCase ( SECTION,
86 "'" + s + "'.match(new RegExp('\\w'))",
87 String([alphanumeric[i]]), String(s.match(new RegExp('\\w'))));
88 }
89 // be sure all non_alphanumeric characters match individually
90 for (i = 0; i < non_alphanumeric.length; ++i)
91 {
92 s = 'sd' + non_alphanumeric[i] + String((i+10) * (i+10) - 2 * (i+10));
93 new TestCase ( SECTION,
94 "'" + s + "'.match(new RegExp('\\W'))",
95 String([non_alphanumeric[i]]), String(s.match(new RegExp('\\W'))));
96 }
97
98 test();