1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25'This test checks for a regression against <a href="https://bugs.webkit.org/show_bug.cgi?id=68348">String#split is buggy</a>.'
26);
27
28// The following JavaScript code (including "".split tests) is copyright by Steven Levithan,
29// and released under the MIT License
30var testCode = [
31    ["''.split()",                        [""]],
32    ["''.split(/./)",                [""]],
33    ["''.split(/.?/)",                []],
34    ["''.split(/.??/)",                []],
35    ["'ab'.split(/a*/)",                ["", "b"]],
36    ["'ab'.split(/a*?/)",                ["a", "b"]],
37    ["'ab'.split(/(?:ab)/)",        ["", ""]],
38    ["'ab'.split(/(?:ab)*/)",        ["", ""]],
39    ["'ab'.split(/(?:ab)*?/)",        ["a", "b"]],
40    ["'test'.split('')",                ["t", "e", "s", "t"]],
41    ["'test'.split()",                ["test"]],
42    ["'111'.split(1)",                ["", "", "", ""]],
43    ["'test'.split(/(?:)/, 2)",                ["t", "e"]],
44    ["'test'.split(/(?:)/, -1)",        ["t", "e", "s", "t"]],
45    ["'test'.split(/(?:)/, undefined)",        ["t", "e", "s", "t"]],
46    ["'test'.split(/(?:)/, null)",        []],
47    ["'test'.split(/(?:)/, NaN)",        []],
48    ["'test'.split(/(?:)/, true)",        ["t"]],
49    ["'test'.split(/(?:)/, '2')",        ["t", "e"]],
50    ["'test'.split(/(?:)/, 'two')",        []],
51    ["'a'.split(/-/)",                ["a"]],
52    ["'a'.split(/-?/)",                ["a"]],
53    ["'a'.split(/-??/)",                ["a"]],
54    ["'a'.split(/a/)",                ["", ""]],
55    ["'a'.split(/a?/)",                ["", ""]],
56    ["'a'.split(/a??/)",                ["a"]],
57    ["'ab'.split(/-/)",                ["ab"]],
58    ["'ab'.split(/-?/)",                ["a", "b"]],
59    ["'ab'.split(/-??/)",                ["a", "b"]],
60    ["'a-b'.split(/-/)",                ["a", "b"]],
61    ["'a-b'.split(/-?/)",                ["a", "b"]],
62    ["'a-b'.split(/-??/)",                ["a", "-", "b"]],
63    ["'a--b'.split(/-/)",                ["a", "", "b"]],
64    ["'a--b'.split(/-?/)",                ["a", "", "b"]],
65    ["'a--b'.split(/-??/)",                ["a", "-", "-", "b"]],
66    ["''.split(/()()/)",                []],
67    ["'.'.split(/()()/)",                ["."]],
68    ["'.'.split(/(.?)(.?)/)",        ["", ".", "", ""]],
69    ["'.'.split(/(.??)(.??)/)",        ["."]],
70    ["'.'.split(/(.)?(.)?/)",        ["", ".", undefined, ""]],
71    ["'A<B>bold</B>and<CODE>coded</CODE>'.split(ecmaSampleRe)", ["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""]],
72    ["'tesst'.split(/(s)*/)",        ["t", undefined, "e", "s", "t"]],
73    ["'tesst'.split(/(s)*?/)",        ["t", undefined, "e", undefined, "s", undefined, "s", undefined, "t"]],
74    ["'tesst'.split(/(s*)/)",        ["t", "", "e", "ss", "t"]],
75    ["'tesst'.split(/(s*?)/)",        ["t", "", "e", "", "s", "", "s", "", "t"]],
76    ["'tesst'.split(/(?:s)*/)",        ["t", "e", "t"]],
77    ["'tesst'.split(/(?=s+)/)",        ["te", "s", "st"]],
78    ["'test'.split('t')",                ["", "es", ""]],
79    ["'test'.split('es')",                ["t", "t"]],
80    ["'test'.split(/t/)",                ["", "es", ""]],
81    ["'test'.split(/es/)",                ["t", "t"]],
82    ["'test'.split(/(t)/)",                ["", "t", "es", "t", ""]],
83    ["'test'.split(/(es)/)",        ["t", "es", "t"]],
84    ["'test'.split(/(t)(e)(s)(t)/)",["", "t", "e", "s", "t", ""]],
85    ["'.'.split(/(((.((.??)))))/)",        ["", ".", ".", ".", "", "", ""]],
86    ["'.'.split(/(((((.??)))))/)",        ["."]]
87];
88var ecmaSampleRe = /<(\/)?([^<>]+)>/;
89
90for (var i in testCode)
91    shouldBe(testCode[i][0], 'testCode[i][1]');
92
93// Check that split works with object separators, and that steps 8 & 9 are performed in the correct order.
94var separatorToStringCalled = "ToString not called on the separator";
95shouldBe("'hello'.split({toString:function(){return 'e';}})", '["h", "llo"]');
96shouldBe("var a = 'hello'.split({toString:function(){separatorToStringCalled='OKAY';return 'e';}},0); a.push(separatorToStringCalled); a", '["OKAY"]');
97