Debian Codegraph @

rhino/testsrc/tests/js1_5/Expressions/shell.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 * Date: 07 February 2001
8 *
9 * Functionality common to Array testing -
10 */
11 //-----------------------------------------------------------------------------
12
13 gTestsubsuite = 'Expressions';
14
15 var CHAR_LBRACKET = '[';
16 var CHAR_RBRACKET = ']';
17 var CHAR_QT_DBL = '"';
18 var CHAR_QT = "'";
19 var CHAR_NL = '\n';
20 var CHAR_COMMA = ',';
21 var CHAR_SPACE = ' ';
22 var TYPE_STRING = typeof 'abc';
23
24
25 /*
26 * If available, arr.toSource() gives more detail than arr.toString()
27 *
28 * var arr = Array(1,2,'3');
29 *
30 * arr.toSource()
31 * [1, 2, "3"]
32 *
33 * arr.toString()
34 * 1,2,3
35 *
36 * But toSource() doesn't exist in Rhino, so use our own imitation, below -
37 *
38 */
39 function formatArray(arr)
40 {
41 try
42 {
43 return arr.toSource();
44 }
45 catch(e)
46 {
47 return toSource(arr);
48 }
49 }
50
51
52
53 /*
54 * Imitate SpiderMonkey's arr.toSource() method:
55 *
56 * a) Double-quote each array element that is of string type
57 * b) Represent |undefined| and |null| by empty strings
58 * c) Delimit elements by a comma + single space
59 * d) Do not add delimiter at the end UNLESS the last element is |undefined|
60 * e) Add square brackets to the beginning and end of the string
61 */
62 function toSource(arr)
63 {
64 var delim = CHAR_COMMA + CHAR_SPACE;
65 var elt = '';
66 var ret = '';
67 var len = arr.length;
68
69 for (i=0; i<len; i++)
70 {
71 elt = arr[i];
72
73 switch(true)
74 {
75 case (typeof elt === TYPE_STRING) :
76 ret += doubleQuote(elt);
77 break;
78
79 case (elt === undefined || elt === null) :
80 break; // add nothing but the delimiter, below -
81
82 default:
83 ret += elt.toString();
84 }
85
86 if ((i < len-1) || (elt === undefined))
87 ret += delim;
88 }
89
90 return CHAR_LBRACKET + ret + CHAR_RBRACKET;
91 }
92
93
94 function doubleQuote(text)
95 {
96 return CHAR_QT_DBL + text + CHAR_QT_DBL;
97 }
98
99
100 function singleQuote(text)
101 {
102 return CHAR_QT + text + CHAR_QT;
103 }
104