1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_2/String/split-003.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,123 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + * File Name: String/split-003.js 1.12 + * ECMA Section: 15.6.4.9 1.13 + * Description: Based on ECMA 2 Draft 7 February 1999 1.14 + * 1.15 + * Author: christine@netscape.com 1.16 + * Date: 19 February 1999 1.17 + */ 1.18 + 1.19 +/* 1.20 + * Since regular expressions have been part of JavaScript since 1.2, there 1.21 + * are already tests for regular expressions in the js1_2/regexp folder. 1.22 + * 1.23 + * These new tests try to supplement the existing tests, and verify that 1.24 + * our implementation of RegExp conforms to the ECMA specification, but 1.25 + * does not try to be as exhaustive as in previous tests. 1.26 + * 1.27 + * The [,limit] argument to String.split is new, and not covered in any 1.28 + * existing tests. 1.29 + * 1.30 + * String.split cases are covered in ecma/String/15.5.4.8-*.js. 1.31 + * String.split where separator is a RegExp are in 1.32 + * js1_2/regexp/string_split.js 1.33 + * 1.34 + */ 1.35 + 1.36 +var SECTION = "ecma_2/String/split-003.js"; 1.37 +var VERSION = "ECMA_2"; 1.38 +var TITLE = "String.prototype.split( regexp, [,limit] )"; 1.39 + 1.40 +startTest(); 1.41 + 1.42 +// separator is a regexp 1.43 +// separator regexp value global setting is set 1.44 +// string is an empty string 1.45 +// if separator is an empty string, split each by character 1.46 + 1.47 + 1.48 +AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] ); 1.49 + 1.50 +AddSplitCases( "hello", /l/, "/l/", ["he","","o"] ); 1.51 +AddLimitedSplitCases( "hello", /l/, "/l/", 0, [] ); 1.52 +AddLimitedSplitCases( "hello", /l/, "/l/", 1, ["he"] ); 1.53 +AddLimitedSplitCases( "hello", /l/, "/l/", 2, ["he",""] ); 1.54 +AddLimitedSplitCases( "hello", /l/, "/l/", 3, ["he","","o"] ); 1.55 +AddLimitedSplitCases( "hello", /l/, "/l/", 4, ["he","","o"] ); 1.56 +AddLimitedSplitCases( "hello", /l/, "/l/", void 0, ["he","","o"] ); 1.57 +AddLimitedSplitCases( "hello", /l/, "/l/", "hi", [] ); 1.58 +AddLimitedSplitCases( "hello", /l/, "/l/", undefined, ["he","","o"] ); 1.59 + 1.60 +AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] ); 1.61 +AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 0, [] ); 1.62 +AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 1, ["h"] ); 1.63 +AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 2, ["h","e"] ); 1.64 +AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 3, ["h","e","l"] ); 1.65 +AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 4, ["h","e","l","l"] ); 1.66 +AddLimitedSplitCases( "hello", new RegExp, "new RegExp", void 0, ["h","e","l","l","o"] ); 1.67 +AddLimitedSplitCases( "hello", new RegExp, "new RegExp", "hi", [] ); 1.68 +AddLimitedSplitCases( "hello", new RegExp, "new RegExp", undefined, ["h","e","l","l","o"] ); 1.69 + 1.70 +test(); 1.71 + 1.72 +function AddSplitCases( string, separator, str_sep, split_array ) { 1.73 + // verify that the result of split is an object of type Array 1.74 + AddTestCase( 1.75 + "( " + string + " ).split(" + str_sep +").constructor == Array", 1.76 + true, 1.77 + string.split(separator).constructor == Array ); 1.78 + 1.79 + // check the number of items in the array 1.80 + AddTestCase( 1.81 + "( " + string + " ).split(" + str_sep +").length", 1.82 + split_array.length, 1.83 + string.split(separator).length ); 1.84 + 1.85 + // check the value of each array item 1.86 + var limit = (split_array.length > string.split(separator).length ) 1.87 + ? split_array.length : string.split(separator).length; 1.88 + 1.89 + for ( var matches = 0; matches < split_array.length; matches++ ) { 1.90 + AddTestCase( 1.91 + "( " + string + " ).split(" + str_sep +")[" + matches +"]", 1.92 + split_array[matches], 1.93 + string.split( separator )[matches] ); 1.94 + } 1.95 +} 1.96 + 1.97 +function AddLimitedSplitCases( 1.98 + string, separator, str_sep, limit, split_array ) { 1.99 + 1.100 + // verify that the result of split is an object of type Array 1.101 + 1.102 + AddTestCase( 1.103 + "( " + string + " ).split(" + str_sep +", " + limit + 1.104 + " ).constructor == Array", 1.105 + true, 1.106 + string.split(separator, limit).constructor == Array ); 1.107 + 1.108 + // check the length of the array 1.109 + 1.110 + AddTestCase( 1.111 + "( " + string + " ).split(" + str_sep +", " + limit + " ).length", 1.112 + split_array.length, 1.113 + string.split(separator, limit).length ); 1.114 + 1.115 + // check the value of each array item 1.116 + 1.117 + var slimit = (split_array.length > string.split(separator).length ) 1.118 + ? split_array.length : string.split(separator, limit).length; 1.119 + 1.120 + for ( var matches = 0; matches < slimit; matches++ ) { 1.121 + AddTestCase( 1.122 + "( " + string + " ).split(" + str_sep +", " + limit + " )[" + matches +"]", 1.123 + split_array[matches], 1.124 + string.split( separator, limit )[matches] ); 1.125 + } 1.126 +}