|
1 /* |
|
2 * Tests from http://xregexp.com/tests/split.html |
|
3 * |
|
4 * Copyright (C) 2007 by Steven Levithan <stevenlevithan.com> |
|
5 * |
|
6 * Distributed under the terms of the MIT license. |
|
7 * |
|
8 * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9 * of this software and associated documentation files (the "Software"), to deal |
|
10 * in the Software without restriction, including without limitation the rights |
|
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12 * copies of the Software, and to permit persons to whom the Software is |
|
13 * furnished to do so, subject to the following conditions: |
|
14 |
|
15 * The above copyright notice and this permission notice shall be included in |
|
16 * all copies or substantial portions of the Software. |
|
17 |
|
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24 * THE SOFTWARE. |
|
25 */ |
|
26 var BUGNUMBER = 614608; |
|
27 var summary = "String.prototype.split with regexp separator"; |
|
28 |
|
29 print(BUGNUMBER + ": " + summary); |
|
30 |
|
31 /************** |
|
32 * BEGIN TEST * |
|
33 **************/ |
|
34 |
|
35 var ecmaSampleRe = /<(\/)?([^<>]+)>/; |
|
36 |
|
37 var testCode = [ |
|
38 ["''.split()", [""]], |
|
39 ["''.split(/./)", [""]], |
|
40 ["''.split(/.?/)", []], |
|
41 ["''.split(/.??/)", []], |
|
42 ["'ab'.split(/a*/)", ["", "b"]], |
|
43 ["'ab'.split(/a*?/)", ["a", "b"]], |
|
44 ["'ab'.split(/(?:ab)/)", ["", ""]], |
|
45 ["'ab'.split(/(?:ab)*/)", ["", ""]], |
|
46 ["'ab'.split(/(?:ab)*?/)", ["a", "b"]], |
|
47 ["'test'.split('')", ["t", "e", "s", "t"]], |
|
48 ["'test'.split()", ["test"]], |
|
49 ["'111'.split(1)", ["", "", "", ""]], |
|
50 ["'test'.split(/(?:)/, 2)", ["t", "e"]], |
|
51 ["'test'.split(/(?:)/, -1)", ["t", "e", "s", "t"]], |
|
52 ["'test'.split(/(?:)/, undefined)", ["t", "e", "s", "t"]], |
|
53 ["'test'.split(/(?:)/, null)", []], |
|
54 ["'test'.split(/(?:)/, NaN)", []], |
|
55 ["'test'.split(/(?:)/, true)", ["t"]], |
|
56 ["'test'.split(/(?:)/, '2')", ["t", "e"]], |
|
57 ["'test'.split(/(?:)/, 'two')", []], |
|
58 ["'a'.split(/-/)", ["a"]], |
|
59 ["'a'.split(/-?/)", ["a"]], |
|
60 ["'a'.split(/-??/)", ["a"]], |
|
61 ["'a'.split(/a/)", ["", ""]], |
|
62 ["'a'.split(/a?/)", ["", ""]], |
|
63 ["'a'.split(/a??/)", ["a"]], |
|
64 ["'ab'.split(/-/)", ["ab"]], |
|
65 ["'ab'.split(/-?/)", ["a", "b"]], |
|
66 ["'ab'.split(/-??/)", ["a", "b"]], |
|
67 ["'a-b'.split(/-/)", ["a", "b"]], |
|
68 ["'a-b'.split(/-?/)", ["a", "b"]], |
|
69 ["'a-b'.split(/-??/)", ["a", "-", "b"]], |
|
70 ["'a--b'.split(/-/)", ["a", "", "b"]], |
|
71 ["'a--b'.split(/-?/)", ["a", "", "b"]], |
|
72 ["'a--b'.split(/-??/)", ["a", "-", "-", "b"]], |
|
73 ["''.split(/()()/)", []], |
|
74 ["'.'.split(/()()/)", ["."]], |
|
75 ["'.'.split(/(.?)(.?)/)", ["", ".", "", ""]], |
|
76 ["'.'.split(/(.??)(.??)/)", ["."]], |
|
77 ["'.'.split(/(.)?(.)?/)", ["", ".", undefined, ""]], |
|
78 ["'A<B>bold</B>and<CODE>coded</CODE>'.split(ecmaSampleRe)", |
|
79 ["A", undefined, "B", "bold", "/", "B", |
|
80 "and", undefined, "CODE", "coded", "/", |
|
81 "CODE", ""]], |
|
82 ["'tesst'.split(/(s)*/)", ["t", undefined, "e", "s", "t"]], |
|
83 ["'tesst'.split(/(s)*?/)", ["t", undefined, "e", undefined, "s", |
|
84 undefined, "s", undefined, "t"]], |
|
85 ["'tesst'.split(/(s*)/)", ["t", "", "e", "ss", "t"]], |
|
86 ["'tesst'.split(/(s*?)/)", ["t", "", "e", "", "s", "", "s", "", "t"]], |
|
87 ["'tesst'.split(/(?:s)*/)", ["t", "e", "t"]], |
|
88 ["'tesst'.split(/(?=s+)/)", ["te", "s", "st"]], |
|
89 ["'test'.split('t')", ["", "es", ""]], |
|
90 ["'test'.split('es')", ["t", "t"]], |
|
91 ["'test'.split(/t/)", ["", "es", ""]], |
|
92 ["'test'.split(/es/)", ["t", "t"]], |
|
93 ["'test'.split(/(t)/)", ["", "t", "es", "t", ""]], |
|
94 ["'test'.split(/(es)/)", ["t", "es", "t"]], |
|
95 ["'test'.split(/(t)(e)(s)(t)/)", ["", "t", "e", "s", "t", ""]], |
|
96 ["'.'.split(/(((.((.??)))))/)", ["", ".", ".", ".", "", "", ""]], |
|
97 ["'.'.split(/(((((.??)))))/)", ["."]] |
|
98 ]; |
|
99 |
|
100 function testSplit() { |
|
101 for (var i = 0; i < testCode.length; i++) { |
|
102 var actual = eval(testCode[i][0]); |
|
103 var expected = testCode[i][1]; |
|
104 |
|
105 assertEq(actual.length, expected.length); |
|
106 |
|
107 for(var j=0; j<actual.length; j++) { |
|
108 assertEq(actual[j], expected[j], testCode[i][0]); |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 testSplit(); |
|
114 |
|
115 if (typeof reportCompare === "function") |
|
116 reportCompare(true, true); |
|
117 |
|
118 print("All tests passed!"); |