Debian Codegraph @

rhino/testsrc/tests/ecma_2/String/split-003.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'split-003.js';
7
8 /**
9 * File Name: String/split-003.js
10 * ECMA Section: 15.6.4.9
11 * Description: Based on ECMA 2 Draft 7 February 1999
12 *
13 * Author: [email protected]
14 * Date: 19 February 1999
15 */
16
17 /*
18 * Since regular expressions have been part of JavaScript since 1.2, there
19 * are already tests for regular expressions in the js1_2/regexp folder.
20 *
21 * These new tests try to supplement the existing tests, and verify that
22 * our implementation of RegExp conforms to the ECMA specification, but
23 * does not try to be as exhaustive as in previous tests.
24 *
25 * The [,limit] argument to String.split is new, and not covered in any
26 * existing tests.
27 *
28 * String.split cases are covered in ecma/String/15.5.4.8-*.js.
29 * String.split where separator is a RegExp are in
30 * js1_2/regexp/string_split.js
31 *
32 */
33
34 var SECTION = "ecma_2/String/split-003.js";
35 var VERSION = "ECMA_2";
36 var TITLE = "String.prototype.split( regexp, [,limit] )";
37
38 startTest();
39
40 // separator is a regexp
41 // separator regexp value global setting is set
42 // string is an empty string
43 // if separator is an empty string, split each by character
44
45
46 AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] );
47
48 AddSplitCases( "hello", /l/, "/l/", ["he","","o"] );
49 AddLimitedSplitCases( "hello", /l/, "/l/", 0, [] );
50 AddLimitedSplitCases( "hello", /l/, "/l/", 1, ["he"] );
51 AddLimitedSplitCases( "hello", /l/, "/l/", 2, ["he",""] );
52 AddLimitedSplitCases( "hello", /l/, "/l/", 3, ["he","","o"] );
53 AddLimitedSplitCases( "hello", /l/, "/l/", 4, ["he","","o"] );
54 AddLimitedSplitCases( "hello", /l/, "/l/", void 0, ["he","","o"] );
55 AddLimitedSplitCases( "hello", /l/, "/l/", "hi", [] );
56 AddLimitedSplitCases( "hello", /l/, "/l/", undefined, ["he","","o"] );
57
58 AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] );
59 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 0, [] );
60 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 1, ["h"] );
61 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 2, ["h","e"] );
62 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 3, ["h","e","l"] );
63 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 4, ["h","e","l","l"] );
64 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", void 0, ["h","e","l","l","o"] );
65 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", "hi", [] );
66 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", undefined, ["h","e","l","l","o"] );
67
68 test();
69
70 function AddSplitCases( string, separator, str_sep, split_array ) {
71 // verify that the result of split is an object of type Array
72 AddTestCase(
73 "( " + string + " ).split(" + str_sep +").constructor == Array",
74 true,
75 string.split(separator).constructor == Array );
76
77 // check the number of items in the array
78 AddTestCase(
79 "( " + string + " ).split(" + str_sep +").length",
80 split_array.length,
81 string.split(separator).length );
82
83 // check the value of each array item
84 var limit = (split_array.length > string.split(separator).length )
85 ? split_array.length : string.split(separator).length;
86
87 for ( var matches = 0; matches < split_array.length; matches++ ) {
88 AddTestCase(
89 "( " + string + " ).split(" + str_sep +")[" + matches +"]",
90 split_array[matches],
91 string.split( separator )[matches] );
92 }
93 }
94
95 function AddLimitedSplitCases(
96 string, separator, str_sep, limit, split_array ) {
97
98 // verify that the result of split is an object of type Array
99
100 AddTestCase(
101 "( " + string + " ).split(" + str_sep +", " + limit +
102 " ).constructor == Array",
103 true,
104 string.split(separator, limit).constructor == Array );
105
106 // check the length of the array
107
108 AddTestCase(
109 "( " + string + " ).split(" + str_sep +", " + limit + " ).length",
110 split_array.length,
111 string.split(separator, limit).length );
112
113 // check the value of each array item
114
115 var slimit = (split_array.length > string.split(separator).length )
116 ? split_array.length : string.split(separator, limit).length;
117
118 for ( var matches = 0; matches < slimit; matches++ ) {
119 AddTestCase(
120 "( " + string + " ).split(" + str_sep +", " + limit + " )[" + matches +"]",
121 split_array[matches],
122 string.split( separator, limit )[matches] );
123 }
124 }