js/src/tests/js1_5/Expressions/shell.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_5/Expressions/shell.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     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 + * Date: 07 February 2001
    1.11 + *
    1.12 + * Functionality common to Array testing -
    1.13 + */
    1.14 +//-----------------------------------------------------------------------------
    1.15 +
    1.16 +
    1.17 +var CHAR_LBRACKET = '[';
    1.18 +var CHAR_RBRACKET = ']';
    1.19 +var CHAR_QT_DBL = '"';
    1.20 +var CHAR_QT = "'";
    1.21 +var CHAR_NL = '\n';
    1.22 +var CHAR_COMMA = ',';
    1.23 +var CHAR_SPACE = ' ';
    1.24 +var TYPE_STRING = typeof 'abc';
    1.25 +
    1.26 +
    1.27 +/*
    1.28 + * If available, arr.toSource() gives more detail than arr.toString()
    1.29 + *
    1.30 + * var arr = Array(1,2,'3');
    1.31 + *
    1.32 + * arr.toSource()
    1.33 + * [1, 2, "3"]
    1.34 + *
    1.35 + * arr.toString()
    1.36 + * 1,2,3
    1.37 + *
    1.38 + * But toSource() doesn't exist in Rhino, so use our own imitation, below -
    1.39 + *
    1.40 + */
    1.41 +function formatArray(arr)
    1.42 +{
    1.43 +  try
    1.44 +  {
    1.45 +    return arr.toSource();
    1.46 +  }
    1.47 +  catch(e)
    1.48 +  {
    1.49 +    return toSource(arr);
    1.50 +  }
    1.51 +}
    1.52 +
    1.53 +
    1.54 +
    1.55 +/*
    1.56 + * Imitate SpiderMonkey's arr.toSource() method:
    1.57 + *
    1.58 + * a) Double-quote each array element that is of string type
    1.59 + * b) Represent |undefined| and |null| by empty strings
    1.60 + * c) Delimit elements by a comma + single space
    1.61 + * d) Do not add delimiter at the end UNLESS the last element is |undefined|
    1.62 + * e) Add square brackets to the beginning and end of the string
    1.63 + */
    1.64 +function toSource(arr)
    1.65 +{
    1.66 +  var delim = CHAR_COMMA + CHAR_SPACE;
    1.67 +  var elt = '';
    1.68 +  var ret = '';
    1.69 +  var len = arr.length;
    1.70 +
    1.71 +  for (i=0; i<len; i++)
    1.72 +  {
    1.73 +    elt = arr[i];
    1.74 +
    1.75 +    switch(true)
    1.76 +    {
    1.77 +    case (typeof elt === TYPE_STRING) :
    1.78 +      ret += doubleQuote(elt);
    1.79 +      break;
    1.80 +
    1.81 +    case (elt === undefined || elt === null) :
    1.82 +      break; // add nothing but the delimiter, below -
    1.83 +
    1.84 +    default:
    1.85 +      ret += elt.toString();
    1.86 +    }
    1.87 +
    1.88 +    if ((i < len-1) || (elt === undefined))
    1.89 +      ret += delim;
    1.90 +  }
    1.91 +
    1.92 +  return  CHAR_LBRACKET + ret + CHAR_RBRACKET;
    1.93 +}
    1.94 +
    1.95 +
    1.96 +function doubleQuote(text)
    1.97 +{
    1.98 +  return CHAR_QT_DBL + text + CHAR_QT_DBL;
    1.99 +}
   1.100 +
   1.101 +
   1.102 +function singleQuote(text)
   1.103 +{
   1.104 +  return CHAR_QT + text + CHAR_QT;
   1.105 +}
   1.106 +

mercurial