Home | History | Annotate | Download | only in regexp
      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:     special_characters.js
     24 	Description:  'Tests regular expressions containing 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: special_charaters';
     34 
     35 	writeHeaderToLog('Executing script: special_characters.js');
     36 	writeHeaderToLog( SECTION + " "+ TITLE);
     37 
     38 	var count = 0;
     39 	var testcases = new Array();
     40 
     41     // testing backslash '\'
     42 	testcases[count++] = new TestCase ( SECTION, "'^abcdefghi'.match(/\^abc/)", String(["^abc"]), String('^abcdefghi'.match(/\^abc/)));
     43 
     44 	// testing beginning of line '^'
     45 	testcases[count++] = new TestCase ( SECTION, "'abcdefghi'.match(/^abc/)", String(["abc"]), String('abcdefghi'.match(/^abc/)));
     46 
     47 	// testing end of line '$'
     48 	testcases[count++] = new TestCase ( SECTION, "'abcdefghi'.match(/fghi$/)", String(["ghi"]), String('abcdefghi'.match(/ghi$/)));
     49 
     50 	// testing repeat '*'
     51 	testcases[count++] = new TestCase ( SECTION, "'eeeefghi'.match(/e*/)", String(["eeee"]), String('eeeefghi'.match(/e*/)));
     52 
     53 	// testing repeat 1 or more times '+'
     54 	testcases[count++] = new TestCase ( SECTION, "'abcdeeeefghi'.match(/e+/)", String(["eeee"]), String('abcdeeeefghi'.match(/e+/)));
     55 
     56 	// testing repeat 0 or 1 time '?'
     57 	testcases[count++] = new TestCase ( SECTION, "'abcdefghi'.match(/abc?de/)", String(["abcde"]), String('abcdefghi'.match(/abc?de/)));
     58 
     59 	// testing any character '.'
     60 	testcases[count++] = new TestCase ( SECTION, "'abcdefghi'.match(/c.e/)", String(["cde"]), String('abcdefghi'.match(/c.e/)));
     61 
     62 	// testing remembering ()
     63 	testcases[count++] = new TestCase ( SECTION, "'abcewirjskjdabciewjsdf'.match(/(abc).+\\1'/)",
     64 	                                    String(["abcewirjskjdabc","abc"]), String('abcewirjskjdabciewjsdf'.match(/(abc).+\1/)));
     65 
     66 	// testing or match '|'
     67 	testcases[count++] = new TestCase ( SECTION, "'abcdefghi'.match(/xyz|def/)", String(["def"]), String('abcdefghi'.match(/xyz|def/)));
     68 
     69 	// testing repeat n {n}
     70 	testcases[count++] = new TestCase ( SECTION, "'abcdeeeefghi'.match(/e{3}/)", String(["eee"]), String('abcdeeeefghi'.match(/e{3}/)));
     71 
     72 	// testing min repeat n {n,}
     73 	testcases[count++] = new TestCase ( SECTION, "'abcdeeeefghi'.match(/e{3,}/)", String(["eeee"]), String('abcdeeeefghi'.match(/e{3,}/)));
     74 
     75 	// testing min/max repeat {min, max}
     76 	testcases[count++] = new TestCase ( SECTION, "'abcdeeeefghi'.match(/e{2,8}/)", String(["eeee"]), String('abcdeeeefghi'.match(/e{2,8}/)));
     77 
     78 	// testing any in set [abc...]
     79 	testcases[count++] = new TestCase ( SECTION, "'abcdefghi'.match(/cd[xey]fgh/)", String(["cdefgh"]), String('abcdefghi'.match(/cd[xey]fgh/)));
     80 
     81 	// testing any in set [a-z]
     82 	testcases[count++] = new TestCase ( SECTION, "'netscape inc'.match(/t[r-v]ca/)", String(["tsca"]), String('netscape inc'.match(/t[r-v]ca/)));
     83 
     84 	// testing any not in set [^abc...]
     85 	testcases[count++] = new TestCase ( SECTION, "'abcdefghi'.match(/cd[^xy]fgh/)", String(["cdefgh"]), String('abcdefghi'.match(/cd[^xy]fgh/)));
     86 
     87 	// testing any not in set [^a-z]
     88 	testcases[count++] = new TestCase ( SECTION, "'netscape inc'.match(/t[^a-c]ca/)", String(["tsca"]), String('netscape inc'.match(/t[^a-c]ca/)));
     89 
     90 	// testing backspace [\b]
     91 	testcases[count++] = new TestCase ( SECTION, "'this is b\ba test'.match(/is b[\b]a test/)",
     92 	                                    String(["is b\ba test"]), String('this is b\ba test'.match(/is b[\b]a test/)));
     93 
     94 	// testing word boundary \b
     95 	testcases[count++] = new TestCase ( SECTION, "'today is now - day is not now'.match(/\bday.*now/)",
     96 	                                    String(["day is not now"]), String('today is now - day is not now'.match(/\bday.*now/)));
     97 
     98 	// control characters???
     99 
    100 	// testing any digit \d
    101 	testcases[count++] = new TestCase ( SECTION, "'a dog - 1 dog'.match(/\d dog/)", String(["1 dog"]), String('a dog - 1 dog'.match(/\d dog/)));
    102 
    103 	// testing any non digit \d
    104 	testcases[count++] = new TestCase ( SECTION, "'a dog - 1 dog'.match(/\D dog/)", String(["a dog"]), String('a dog - 1 dog'.match(/\D dog/)));
    105 
    106 	// testing form feed '\f'
    107 	testcases[count++] = new TestCase ( SECTION, "'a b a\fb'.match(/a\fb/)", String(["a\fb"]), String('a b a\fb'.match(/a\fb/)));
    108 
    109 	// testing line feed '\n'
    110 	testcases[count++] = new TestCase ( SECTION, "'a b a\nb'.match(/a\nb/)", String(["a\nb"]), String('a b a\nb'.match(/a\nb/)));
    111 
    112 	// testing carriage return '\r'
    113 	testcases[count++] = new TestCase ( SECTION, "'a b a\rb'.match(/a\rb/)", String(["a\rb"]), String('a b a\rb'.match(/a\rb/)));
    114 
    115 	// testing whitespace '\s'
    116 	testcases[count++] = new TestCase ( SECTION, "'xa\f\n\r\t\vbz'.match(/a\s+b/)", String(["a\f\n\r\t\vb"]), String('xa\f\n\r\t\vbz'.match(/a\s+b/)));
    117 
    118 	// testing non whitespace '\S'
    119 	testcases[count++] = new TestCase ( SECTION, "'a\tb a b a-b'.match(/a\Sb/)", String(["a-b"]), String('a\tb a b a-b'.match(/a\Sb/)));
    120 
    121 	// testing tab '\t'
    122 	testcases[count++] = new TestCase ( SECTION, "'a\t\tb a  b'.match(/a\t{2}/)", String(["a\t\t"]), String('a\t\tb a  b'.match(/a\t{2}/)));
    123 
    124 	// testing vertical tab '\v'
    125 	testcases[count++] = new TestCase ( SECTION, "'a\v\vb a  b'.match(/a\v{2}/)", String(["a\v\v"]), String('a\v\vb a  b'.match(/a\v{2}/)));
    126 
    127 	// testing alphnumeric characters '\w'
    128 	testcases[count++] = new TestCase ( SECTION, "'%AZaz09_$'.match(/\w+/)", String(["AZaz09_"]), String('%AZaz09_$'.match(/\w+/)));
    129 
    130 	// testing non alphnumeric characters '\W'
    131 	testcases[count++] = new TestCase ( SECTION, "'azx$%#@*4534'.match(/\W+/)", String(["$%#@*"]), String('azx$%#@*4534'.match(/\W+/)));
    132 
    133 	// testing back references '\<number>'
    134 	testcases[count++] = new TestCase ( SECTION, "'test'.match(/(t)es\\1/)", String(["test","t"]), String('test'.match(/(t)es\1/)));
    135 
    136 	// testing hex excaping with '\'
    137 	testcases[count++] = new TestCase ( SECTION, "'abcdef'.match(/\x63\x64/)", String(["cd"]), String('abcdef'.match(/\x63\x64/)));
    138 
    139 	// testing oct excaping with '\'
    140 	testcases[count++] = new TestCase ( SECTION, "'abcdef'.match(/\\143\\144/)", String(["cd"]), String('abcdef'.match(/\143\144/)));
    141 
    142 	function test()
    143 	{
    144 	   for ( tc=0; tc < testcases.length; tc++ ) {
    145 	        testcases[tc].passed = writeTestCaseResult(
    146 	        testcases[tc].expect,
    147 	        testcases[tc].actual,
    148 	        testcases[tc].description +" = "+
    149 	        testcases[tc].actual );
    150 	        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
    151 	   }
    152 	   stopTest();
    153 	   return ( testcases );
    154 	}
    155 
    156 	test();
    157 
    158