summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_5/String/split-xregexp.js
blob: 9dfe79b00bd9f05684337bb9fe91c07e7b2ac5e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Tests from http://xregexp.com/tests/split.html
 *
 * Copyright (C) 2007 by Steven Levithan <stevenlevithan.com>
 *
 * Distributed under the terms of the MIT license.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
var BUGNUMBER = 614608;
var summary = "String.prototype.split with regexp separator";

print(BUGNUMBER + ": " + summary);

/**************
 * BEGIN TEST *
 **************/

var ecmaSampleRe = /<(\/)?([^<>]+)>/;

var testCode = [
    ["''.split()",                   [""]],
    ["''.split(/./)",                [""]],
    ["''.split(/.?/)",               []],
    ["''.split(/.??/)",              []],
    ["'ab'.split(/a*/)",             ["", "b"]],
    ["'ab'.split(/a*?/)",            ["a", "b"]],
    ["'ab'.split(/(?:ab)/)",         ["", ""]],
    ["'ab'.split(/(?:ab)*/)",        ["", ""]],
    ["'ab'.split(/(?:ab)*?/)",       ["a", "b"]],
    ["'test'.split('')",             ["t", "e", "s", "t"]],
    ["'test'.split()",               ["test"]],
    ["'111'.split(1)",               ["", "", "", ""]],
    ["'test'.split(/(?:)/, 2)",      ["t", "e"]],
    ["'test'.split(/(?:)/, -1)",     ["t", "e", "s", "t"]],
    ["'test'.split(/(?:)/, undefined)", ["t", "e", "s", "t"]],
    ["'test'.split(/(?:)/, null)",   []],
    ["'test'.split(/(?:)/, NaN)",    []],
    ["'test'.split(/(?:)/, true)",   ["t"]],
    ["'test'.split(/(?:)/, '2')",    ["t", "e"]],
    ["'test'.split(/(?:)/, 'two')",  []],
    ["'a'.split(/-/)",               ["a"]],
    ["'a'.split(/-?/)",              ["a"]],
    ["'a'.split(/-??/)",             ["a"]],
    ["'a'.split(/a/)",               ["", ""]],
    ["'a'.split(/a?/)",              ["", ""]],
    ["'a'.split(/a??/)",             ["a"]],
    ["'ab'.split(/-/)",              ["ab"]],
    ["'ab'.split(/-?/)",             ["a", "b"]],
    ["'ab'.split(/-??/)",            ["a", "b"]],
    ["'a-b'.split(/-/)",             ["a", "b"]],
    ["'a-b'.split(/-?/)",            ["a", "b"]],
    ["'a-b'.split(/-??/)",           ["a", "-", "b"]],
    ["'a--b'.split(/-/)",            ["a", "", "b"]],
    ["'a--b'.split(/-?/)",           ["a", "", "b"]],
    ["'a--b'.split(/-??/)",          ["a", "-", "-", "b"]],
    ["''.split(/()()/)",             []],
    ["'.'.split(/()()/)",            ["."]],
    ["'.'.split(/(.?)(.?)/)",        ["", ".", "", ""]],
    ["'.'.split(/(.??)(.??)/)",      ["."]],
    ["'.'.split(/(.)?(.)?/)",        ["", ".", undefined, ""]],
    ["'A<B>bold</B>and<CODE>coded</CODE>'.split(ecmaSampleRe)",
                                     ["A", undefined, "B", "bold", "/", "B",
                                      "and", undefined, "CODE", "coded", "/",
                                      "CODE", ""]],
    ["'tesst'.split(/(s)*/)",        ["t", undefined, "e", "s", "t"]],
    ["'tesst'.split(/(s)*?/)",       ["t", undefined, "e", undefined, "s",
                                      undefined, "s", undefined, "t"]],
    ["'tesst'.split(/(s*)/)",        ["t", "", "e", "ss", "t"]],
    ["'tesst'.split(/(s*?)/)",       ["t", "", "e", "", "s", "", "s", "", "t"]],
    ["'tesst'.split(/(?:s)*/)",      ["t", "e", "t"]],
    ["'tesst'.split(/(?=s+)/)",      ["te", "s", "st"]],
    ["'test'.split('t')",            ["", "es", ""]],
    ["'test'.split('es')",           ["t", "t"]],
    ["'test'.split(/t/)",            ["", "es", ""]],
    ["'test'.split(/es/)",           ["t", "t"]],
    ["'test'.split(/(t)/)",          ["", "t", "es", "t", ""]],
    ["'test'.split(/(es)/)",         ["t", "es", "t"]],
    ["'test'.split(/(t)(e)(s)(t)/)", ["", "t", "e", "s", "t", ""]],
    ["'.'.split(/(((.((.??)))))/)",  ["", ".", ".", ".", "", "", ""]],
    ["'.'.split(/(((((.??)))))/)",   ["."]]
];

function testSplit() {
    for (var i = 0; i < testCode.length; i++) {
        var actual = eval(testCode[i][0]);
        var expected = testCode[i][1];

        assertEq(actual.length, expected.length);

        for(var j=0; j<actual.length; j++) {
            assertEq(actual[j], expected[j], testCode[i][0]);
        }
    }
}

testSplit();

if (typeof reportCompare === "function")
    reportCompare(true, true);

print("All tests passed!");