michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * File Name: String/split-001.js michael@0: * ECMA Section: 15.6.4.9 michael@0: * Description: Based on ECMA 2 Draft 7 February 1999 michael@0: * michael@0: * Author: christine@netscape.com michael@0: * Date: 19 February 1999 michael@0: */ michael@0: michael@0: /* michael@0: * Since regular expressions have been part of JavaScript since 1.2, there michael@0: * are already tests for regular expressions in the js1_2/regexp folder. michael@0: * michael@0: * These new tests try to supplement the existing tests, and verify that michael@0: * our implementation of RegExp conforms to the ECMA specification, but michael@0: * does not try to be as exhaustive as in previous tests. michael@0: * michael@0: * The [,limit] argument to String.split is new, and not covered in any michael@0: * existing tests. michael@0: * michael@0: * String.split cases are covered in ecma/String/15.5.4.8-*.js. michael@0: * String.split where separator is a RegExp are in michael@0: * js1_2/regexp/string_split.js michael@0: * michael@0: */ michael@0: michael@0: var SECTION = "ecma_2/String/split-001.js"; michael@0: var VERSION = "ECMA_2"; michael@0: var TITLE = "String.prototype.split( regexp, [,limit] )"; michael@0: michael@0: startTest(); michael@0: michael@0: // the separator is not supplied michael@0: // separator is undefined michael@0: // separator is an empty string michael@0: michael@0: AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] ); michael@0: AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] ); michael@0: michael@0: // separartor is a regexp michael@0: // separator regexp value global setting is set michael@0: // string is an empty string michael@0: // if separator is an empty string, split each by character michael@0: michael@0: // this is not a String object michael@0: michael@0: // limit is not a number michael@0: // limit is undefined michael@0: // limit is larger than 2^32-1 michael@0: // limit is a negative number michael@0: michael@0: test(); michael@0: michael@0: function AddSplitCases( string, separator, str_sep, split_array ) { michael@0: michael@0: // verify that the result of split is an object of type Array michael@0: AddTestCase( michael@0: "( " + string + " ).split(" + str_sep +").constructor == Array", michael@0: true, michael@0: string.split(separator).constructor == Array ); michael@0: michael@0: // check the number of items in the array michael@0: AddTestCase( michael@0: "( " + string + " ).split(" + str_sep +").length", michael@0: split_array.length, michael@0: string.split(separator).length ); michael@0: michael@0: // check the value of each array item michael@0: var limit = (split_array.length > string.split(separator).length ) michael@0: ? split_array.length : string.split(separator).length; michael@0: michael@0: for ( var matches = 0; matches < split_array.length; matches++ ) { michael@0: AddTestCase( michael@0: "( " + string + " ).split(" + str_sep +")[" + matches +"]", michael@0: split_array[matches], michael@0: string.split( separator )[matches] ); michael@0: } michael@0: } michael@0: michael@0: function AddLimitedSplitCases( michael@0: string, separator, str_sep, limit, str_limit, split_array ) { michael@0: michael@0: // verify that the result of split is an object of type Array michael@0: michael@0: AddTestCase( michael@0: "( " + string + " ).split(" + str_sep +", " + str_limit + michael@0: " ).constructor == Array", michael@0: true, michael@0: string.split(separator, limit).constructor == Array ); michael@0: michael@0: // check the length of the array michael@0: michael@0: AddTestCase( michael@0: "( " + string + " ).split(" + str_sep +", " + str_limit + " ).length", michael@0: length, michael@0: string.split(separator).length ); michael@0: michael@0: // check the value of each array item michael@0: michael@0: for ( var matches = 0; matches < split_array.length; matches++ ) { michael@0: AddTestCase( michael@0: "( " + string + " ).split(" + str_sep +", " + str_limit + " )[" + matches +"]", michael@0: split_array[matches], michael@0: string.split( separator )[matches] ); michael@0: } michael@0: }