Debian Codegraph @

rhino/testsrc/tests/ecma_2/String/split-001.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-001.js';
7
8 /**
9 * File Name: String/split-001.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-001.js";
35 var VERSION = "ECMA_2";
36 var TITLE = "String.prototype.split( regexp, [,limit] )";
37
38 startTest();
39
40 // the separator is not supplied
41 // separator is undefined
42 // separator is an empty string
43
44 AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] );
45 AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] );
46
47 // separartor is a regexp
48 // separator regexp value global setting is set
49 // string is an empty string
50 // if separator is an empty string, split each by character
51
52 // this is not a String object
53
54 // limit is not a number
55 // limit is undefined
56 // limit is larger than 2^32-1
57 // limit is a negative number
58
59 test();
60
61 function AddSplitCases( string, separator, str_sep, split_array ) {
62
63 // verify that the result of split is an object of type Array
64 AddTestCase(
65 "( " + string + " ).split(" + str_sep +").constructor == Array",
66 true,
67 string.split(separator).constructor == Array );
68
69 // check the number of items in the array
70 AddTestCase(
71 "( " + string + " ).split(" + str_sep +").length",
72 split_array.length,
73 string.split(separator).length );
74
75 // check the value of each array item
76 var limit = (split_array.length > string.split(separator).length )
77 ? split_array.length : string.split(separator).length;
78
79 for ( var matches = 0; matches < split_array.length; matches++ ) {
80 AddTestCase(
81 "( " + string + " ).split(" + str_sep +")[" + matches +"]",
82 split_array[matches],
83 string.split( separator )[matches] );
84 }
85 }
86
87 function AddLimitedSplitCases(
88 string, separator, str_sep, limit, str_limit, split_array ) {
89
90 // verify that the result of split is an object of type Array
91
92 AddTestCase(
93 "( " + string + " ).split(" + str_sep +", " + str_limit +
94 " ).constructor == Array",
95 true,
96 string.split(separator, limit).constructor == Array );
97
98 // check the length of the array
99
100 AddTestCase(
101 "( " + string + " ).split(" + str_sep +", " + str_limit + " ).length",
102 length,
103 string.split(separator).length );
104
105 // check the value of each array item
106
107 for ( var matches = 0; matches < split_array.length; matches++ ) {
108 AddTestCase(
109 "( " + string + " ).split(" + str_sep +", " + str_limit + " )[" + matches +"]",
110 split_array[matches],
111 string.split( separator )[matches] );
112 }
113 }