• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:     alphanumeric.js
24	Description:  'Tests regular expressions with \w and \W special characters'
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: \\w and \\W';
34
35	writeHeaderToLog('Executing script: alphanumeric.js');
36	writeHeaderToLog( SECTION + " " + TITLE);
37
38	var count = 0;
39	var testcases = new Array();
40
41	var non_alphanumeric = "~`!@#$%^&*()-+={[}]|\\:;'<,>./?\f\n\r\t\v " + '"';
42    var alphanumeric     = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
43
44    // be sure all alphanumerics are matched by \w
45	testcases[count++] = new TestCase ( SECTION,
46	                                    "'" + alphanumeric + "'.match(new RegExp('\\w+'))",
47	                                    String([alphanumeric]), String(alphanumeric.match(new RegExp('\\w+'))));
48
49    // be sure all non-alphanumerics are matched by \W
50	testcases[count++] = new TestCase ( SECTION,
51	                                    "'" + non_alphanumeric + "'.match(new RegExp('\\W+'))",
52	                                    String([non_alphanumeric]), String(non_alphanumeric.match(new RegExp('\\W+'))));
53
54    // be sure all non-alphanumerics are not matched by \w
55	testcases[count++] = new TestCase ( SECTION,
56	                                    "'" + non_alphanumeric + "'.match(new RegExp('\\w'))",
57	                                    null, non_alphanumeric.match(new RegExp('\\w')));
58
59    // be sure all alphanumerics are not matched by \W
60	testcases[count++] = new TestCase ( SECTION,
61	                                    "'" + alphanumeric + "'.match(new RegExp('\\W'))",
62	                                    null, alphanumeric.match(new RegExp('\\W')));
63
64	var s = non_alphanumeric + alphanumeric;
65
66    // be sure all alphanumerics are matched by \w
67	testcases[count++] = new TestCase ( SECTION,
68	                                    "'" + s + "'.match(new RegExp('\\w+'))",
69	                                    String([alphanumeric]), String(s.match(new RegExp('\\w+'))));
70
71	s = alphanumeric + non_alphanumeric;
72
73    // be sure all non-alphanumerics are matched by \W
74	testcases[count++] = new TestCase ( SECTION,
75	                                    "'" + s + "'.match(new RegExp('\\W+'))",
76	                                    String([non_alphanumeric]), String(s.match(new RegExp('\\W+'))));
77
78    // be sure all alphanumerics are matched by \w (using literals)
79	testcases[count++] = new TestCase ( SECTION,
80	                                    "'" + s + "'.match(/\w+/)",
81	                                    String([alphanumeric]), String(s.match(/\w+/)));
82
83	s = alphanumeric + non_alphanumeric;
84
85    // be sure all non-alphanumerics are matched by \W (using literals)
86	testcases[count++] = new TestCase ( SECTION,
87	                                    "'" + s + "'.match(/\W+/)",
88	                                    String([non_alphanumeric]), String(s.match(/\W+/)));
89
90    s = 'abcd*&^%$$';
91    // be sure the following test behaves consistently
92	testcases[count++] = new TestCase ( SECTION,
93	                                    "'" + s + "'.match(/(\w+)...(\W+)/)",
94	                                    String([s , 'abcd' , '%$$']), String(s.match(/(\w+)...(\W+)/)));
95
96    var i;
97
98    // be sure all alphanumeric characters match individually
99	for (i = 0; i < alphanumeric.length; ++i)
100	{
101	    s = '#$' + alphanumeric[i] + '%^';
102    	testcases[count++] = new TestCase ( SECTION,
103    	                                    "'" + s + "'.match(new RegExp('\\w'))",
104    	                                    String([alphanumeric[i]]), String(s.match(new RegExp('\\w'))));
105	}
106    // be sure all non_alphanumeric characters match individually
107	for (i = 0; i < non_alphanumeric.length; ++i)
108	{
109	    s = 'sd' + non_alphanumeric[i] + String((i+10) * (i+10) - 2 * (i+10));
110    	testcases[count++] = new TestCase ( SECTION,
111    	                                    "'" + s + "'.match(new RegExp('\\W'))",
112    	                                    String([non_alphanumeric[i]]), String(s.match(new RegExp('\\W'))));
113	}
114
115	function test()
116	{
117	   for ( tc=0; tc < testcases.length; tc++ ) {
118	        testcases[tc].passed = writeTestCaseResult(
119	        testcases[tc].expect,
120	        testcases[tc].actual,
121	        testcases[tc].description +" = "+
122	        testcases[tc].actual );
123	        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
124	   }
125	   stopTest();
126	   return ( testcases );
127	}
128
129	test();
130