blob: c741f6a3e59d639ba4403860927e762890da66d5 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28expected = ["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""];
29result = "A<B>bold</B>and<CODE>coded</CODE>".split(/<(\/)?([^<>]+)>/);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000030assertArrayEquals(expected, result);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000031
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000032
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000033assertArrayEquals(["a", "b"], "ab".split(/a*?/));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000034
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000035assertArrayEquals(["", "b"], "ab".split(/a*/));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000036
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000037assertArrayEquals(["a"], "ab".split(/a*?/, 1));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000038
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000039assertArrayEquals([""], "ab".split(/a*/, 1));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041assertArrayEquals(["as","fas","fas","f"], "asdfasdfasdf".split("d"));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000042
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000043assertArrayEquals(["as","fas","fas","f"], "asdfasdfasdf".split("d", -1));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000045assertArrayEquals(["as", "fas"], "asdfasdfasdf".split("d", 2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000046
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000047assertArrayEquals([], "asdfasdfasdf".split("d", 0));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000048
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000049assertArrayEquals(["as","fas","fas",""], "asdfasdfasd".split("d"));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000051assertArrayEquals([], "".split(""));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000052
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000053assertArrayEquals([""], "".split("a"));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000054
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000055assertArrayEquals(["a","b"], "axxb".split(/x*/));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000056
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000057assertArrayEquals(["a","b"], "axxb".split(/x+/));
58
59assertArrayEquals(["a","","b"], "axxb".split(/x/));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000060
61// This was http://b/issue?id=1151354
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000062assertArrayEquals(["div", "#id", ".class"], "div#id.class".split(/(?=[#.])/));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000063
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000064
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000065assertArrayEquals(["div", "#i", "d", ".class"], "div#id.class".split(/(?=[d#.])/));
66
67assertArrayEquals(["a", "b", "c"], "abc".split(/(?=.)/));
68
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000069
70/* "ab".split(/((?=.))/)
71 *
72 * KJS: ,a,,b
73 * SM: a,,b,
74 * IE: a,b
75 * Opera: a,,b
76 * V8: a,,b
77 *
78 * Opera seems to have this right. The others make no sense.
79 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000080assertArrayEquals(["a", "", "b"], "ab".split(/((?=.))/));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000081
82/* "ab".split(/(?=)/)
83 *
84 * KJS: a,b
85 * SM: ab
86 * IE: a,b
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000087 * Opera: a,bb
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000088 * V8: a,b
89 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000090assertArrayEquals(["a", "b"], "ab".split(/(?=)/));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000091
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000092
93// For issue http://code.google.com/p/v8/issues/detail?id=924
94// Splitting the empty string is a special case.
95assertEquals([""], ''.split());
96assertEquals([""], ''.split(/./));
97assertEquals([], ''.split(/.?/));
98assertEquals([], ''.split(/.??/));
99assertEquals([], ''.split(/()()/));