1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is JavaScript Engine testing utilities.
17 * The Initial Developer of the Original Code is
18 * Netscape Communication Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 gTestfile = 'split-003.js';
41 * File Name: String/split-003.js
42 * ECMA Section: 15.6.4.9
43 * Description: Based on ECMA 2 Draft 7 February 1999
45 * Author: christine@netscape.com
46 * Date: 19 February 1999
50 * Since regular expressions have been part of JavaScript since 1.2, there
51 * are already tests for regular expressions in the js1_2/regexp folder.
53 * These new tests try to supplement the existing tests, and verify that
54 * our implementation of RegExp conforms to the ECMA specification, but
55 * does not try to be as exhaustive as in previous tests.
57 * The [,limit] argument to String.split is new, and not covered in any
60 * String.split cases are covered in ecma/String/15.5.4.8-*.js.
61 * String.split where separator is a RegExp are in
62 * js1_2/regexp/string_split.js
66 var SECTION = "ecma_2/String/split-003.js";
67 var VERSION = "ECMA_2";
68 var TITLE = "String.prototype.split( regexp, [,limit] )";
72 // separator is a regexp
73 // separator regexp value global setting is set
74 // string is an empty string
75 // if separator is an empty string, split each by character
78 AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] );
80 AddSplitCases( "hello", /l/, "/l/", ["he","","o"] );
81 AddLimitedSplitCases( "hello", /l/, "/l/", 0, [] );
82 AddLimitedSplitCases( "hello", /l/, "/l/", 1, ["he"] );
83 AddLimitedSplitCases( "hello", /l/, "/l/", 2, ["he",""] );
84 AddLimitedSplitCases( "hello", /l/, "/l/", 3, ["he","","o"] );
85 AddLimitedSplitCases( "hello", /l/, "/l/", 4, ["he","","o"] );
86 AddLimitedSplitCases( "hello", /l/, "/l/", void 0, ["he","","o"] );
87 AddLimitedSplitCases( "hello", /l/, "/l/", "hi", [] );
88 AddLimitedSplitCases( "hello", /l/, "/l/", undefined, ["he","","o"] );
90 AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] );
91 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 0, [] );
92 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 1, ["h"] );
93 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 2, ["h","e"] );
94 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 3, ["h","e","l"] );
95 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 4, ["h","e","l","l"] );
96 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", void 0, ["h","e","l","l","o"] );
97 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", "hi", [] );
98 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", undefined, ["h","e","l","l","o"] );
102 function AddSplitCases( string, separator, str_sep, split_array ) {
103 // verify that the result of split is an object of type Array
105 "( " + string + " ).split(" + str_sep +").constructor == Array",
107 string.split(separator).constructor == Array );
109 // check the number of items in the array
111 "( " + string + " ).split(" + str_sep +").length",
113 string.split(separator).length );
115 // check the value of each array item
116 var limit = (split_array.length > string.split(separator).length )
117 ? split_array.length : string.split(separator).length;
119 for ( var matches = 0; matches < split_array.length; matches++ ) {
121 "( " + string + " ).split(" + str_sep +")[" + matches +"]",
122 split_array[matches],
123 string.split( separator )[matches] );
127 function AddLimitedSplitCases(
128 string, separator, str_sep, limit, split_array ) {
130 // verify that the result of split is an object of type Array
133 "( " + string + " ).split(" + str_sep +", " + limit +
134 " ).constructor == Array",
136 string.split(separator, limit).constructor == Array );
138 // check the length of the array
141 "( " + string + " ).split(" + str_sep +", " + limit + " ).length",
143 string.split(separator, limit).length );
145 // check the value of each array item
147 var slimit = (split_array.length > string.split(separator).length )
148 ? split_array.length : string.split(separator, limit).length;
150 for ( var matches = 0; matches < slimit; matches++ ) {
152 "( " + string + " ).split(" + str_sep +", " + limit + " )[" + matches +"]",
153 split_array[matches],
154 string.split( separator, limit )[matches] );