js/src/tests/ecma_2/String/split-001.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_2/String/split-001.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     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-001.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-001.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 +// the separator is not supplied
    1.43 +// separator is undefined
    1.44 +// separator is an empty string
    1.45 +
    1.46 +AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] );
    1.47 +AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] );
    1.48 +
    1.49 +// separartor is a regexp
    1.50 +// separator regexp value global setting is set
    1.51 +// string is an empty string
    1.52 +// if separator is an empty string, split each by character
    1.53 +
    1.54 +// this is not a String object
    1.55 +
    1.56 +// limit is not a number
    1.57 +// limit is undefined
    1.58 +// limit is larger than 2^32-1
    1.59 +// limit is a negative number
    1.60 +
    1.61 +test();
    1.62 +
    1.63 +function AddSplitCases( string, separator, str_sep, split_array ) {
    1.64 +
    1.65 +  // verify that the result of split is an object of type Array
    1.66 +  AddTestCase(
    1.67 +    "( " + string  + " ).split(" + str_sep +").constructor == Array",
    1.68 +    true,
    1.69 +    string.split(separator).constructor == Array );
    1.70 +
    1.71 +  // check the number of items in the array
    1.72 +  AddTestCase(
    1.73 +    "( " + string  + " ).split(" + str_sep +").length",
    1.74 +    split_array.length,
    1.75 +    string.split(separator).length );
    1.76 +
    1.77 +  // check the value of each array item
    1.78 +  var limit = (split_array.length > string.split(separator).length )
    1.79 +    ? split_array.length : string.split(separator).length;
    1.80 +
    1.81 +  for ( var matches = 0; matches < split_array.length; matches++ ) {
    1.82 +    AddTestCase(
    1.83 +      "( " + string + " ).split(" + str_sep +")[" + matches +"]",
    1.84 +      split_array[matches],
    1.85 +      string.split( separator )[matches] );
    1.86 +  }
    1.87 +}
    1.88 +
    1.89 +function AddLimitedSplitCases(
    1.90 +  string, separator, str_sep, limit, str_limit, split_array ) {
    1.91 +
    1.92 +  // verify that the result of split is an object of type Array
    1.93 +
    1.94 +  AddTestCase(
    1.95 +    "( " + string  + " ).split(" + str_sep +", " + str_limit +
    1.96 +    " ).constructor == Array",
    1.97 +    true,
    1.98 +    string.split(separator, limit).constructor == Array );
    1.99 +
   1.100 +  // check the length of the array
   1.101 +
   1.102 +  AddTestCase(
   1.103 +    "( " + string + " ).split(" + str_sep  +", " + str_limit + " ).length",
   1.104 +    length,
   1.105 +    string.split(separator).length );
   1.106 +
   1.107 +  // check the value of each array item
   1.108 +
   1.109 +  for ( var matches = 0; matches < split_array.length; matches++ ) {
   1.110 +    AddTestCase(
   1.111 +      "( " + string + " ).split(" + str_sep +", " + str_limit + " )[" + matches +"]",
   1.112 +      split_array[matches],
   1.113 +      string.split( separator )[matches] );
   1.114 +  }
   1.115 +}

mercurial