Debian Codegraph @

rhino/testsrc/tests/ecma_3/extensions/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 RegExp testing -
10 */
11 //-----------------------------------------------------------------------------
12
13 gTestsubsuite = 'extensions';
14
15 var MSG_PATTERN = '\nregexp = ';
16 var MSG_STRING = '\nstring = ';
17 var MSG_EXPECT = '\nExpect: ';
18 var MSG_ACTUAL = '\nActual: ';
19 var ERR_LENGTH = '\nERROR !!! match arrays have different lengths:';
20 var ERR_MATCH = '\nERROR !!! regexp failed to give expected match array:';
21 var ERR_NO_MATCH = '\nERROR !!! regexp FAILED to match anything !!!';
22 var ERR_UNEXP_MATCH = '\nERROR !!! regexp MATCHED when we expected it to fail !!!';
23 var CHAR_LBRACKET = '[';
24 var CHAR_RBRACKET = ']';
25 var CHAR_QT_DBL = '"';
26 var CHAR_QT = "'";
27 var CHAR_NL = '\n';
28 var CHAR_COMMA = ',';
29 var CHAR_SPACE = ' ';
30 var TYPE_STRING = typeof 'abc';
31
32
33
34 function testRegExp(statuses, patterns, strings, actualmatches, expectedmatches)
35 {
36 var status = '';
37 var pattern = new RegExp();
38 var string = '';
39 var actualmatch = new Array();
40 var expectedmatch = new Array();
41 var state = '';
42 var lActual = -1;
43 var lExpect = -1;
44
45
46 for (var i=0; i != patterns.length; i++)
47 {
48 status = statuses[i];
49 pattern = patterns[i];
50 string = strings[i];
51 actualmatch=actualmatches[i];
52 expectedmatch=expectedmatches[i];
53 state = getState(status, pattern, string);
54
55 description = status;
56
57 if(actualmatch)
58 {
59 actual = formatArray(actualmatch);
60 if(expectedmatch)
61 {
62 // expectedmatch and actualmatch are arrays -
63 lExpect = expectedmatch.length;
64 lActual = actualmatch.length;
65
66 var expected = formatArray(expectedmatch);
67
68 if (lActual != lExpect)
69 {
70 reportCompare(lExpect, lActual,
71 state + ERR_LENGTH +
72 MSG_EXPECT + expected +
73 MSG_ACTUAL + actual +
74 CHAR_NL
75 );
76 continue;
77 }
78
79 // OK, the arrays have same length -
80 if (expected != actual)
81 {
82 reportCompare(expected, actual,
83 state + ERR_MATCH +
84 MSG_EXPECT + expected +
85 MSG_ACTUAL + actual +
86 CHAR_NL
87 );
88 }
89 else
90 {
91 reportCompare(expected, actual, state)
92 }
93
94 }
95 else //expectedmatch is null - that is, we did not expect a match -
96 {
97 expected = expectedmatch;
98 reportCompare(expected, actual,
99 state + ERR_UNEXP_MATCH +
100 MSG_EXPECT + expectedmatch +
101 MSG_ACTUAL + actual +
102 CHAR_NL
103 );
104 }
105
106 }
107 else // actualmatch is null
108 {
109 if (expectedmatch)
110 {
111 actual = actualmatch;
112 reportCompare(expected, actual,
113 state + ERR_NO_MATCH +
114 MSG_EXPECT + expectedmatch +
115 MSG_ACTUAL + actualmatch +
116 CHAR_NL
117 );
118 }
119 else // we did not expect a match
120 {
121 // Being ultra-cautious. Presumably expectedmatch===actualmatch===null
122 expected = expectedmatch;
123 actual = actualmatch;
124 reportCompare (expectedmatch, actualmatch, state);
125 }
126 }
127 }
128 }
129
130
131 function getState(status, pattern, string)
132 {
133 /*
134 * Escape \n's, etc. to make them LITERAL in the presentation string.
135 * We don't have to worry about this in |pattern|; such escaping is
136 * done automatically by pattern.toString(), invoked implicitly below.
137 *
138 * One would like to simply do: string = string.replace(/(\s)/g, '\$1').
139 * However, the backreference $1 is not a literal string value,
140 * so this method doesn't work.
141 *
142 * Also tried string = string.replace(/(\s)/g, escape('$1'));
143 * but this just inserts the escape of the literal '$1', i.e. '%241'.
144 */
145 string = string.replace(/\n/g, '\\n');
146 string = string.replace(/\r/g, '\\r');
147 string = string.replace(/\t/g, '\\t');
148 string = string.replace(/\v/g, '\\v');
149 string = string.replace(/\f/g, '\\f');
150
151 return (status + MSG_PATTERN + pattern + MSG_STRING + singleQuote(string));
152 }
153
154
155 /*
156 * If available, arr.toSource() gives more detail than arr.toString()
157 *
158 * var arr = Array(1,2,'3');
159 *
160 * arr.toSource()
161 * [1, 2, "3"]
162 *
163 * arr.toString()
164 * 1,2,3
165 *
166 * But toSource() doesn't exist in Rhino, so use our own imitation, below -
167 *
168 */
169 function formatArray(arr)
170 {
171 try
172 {
173 return arr.toSource();
174 }
175 catch(e)
176 {
177 return toSource(arr);
178 }
179 }
180
181
182 /*
183 * Imitate SpiderMonkey's arr.toSource() method:
184 *
185 * a) Double-quote each array element that is of string type
186 * b) Represent |undefined| and |null| by empty strings
187 * c) Delimit elements by a comma + single space
188 * d) Do not add delimiter at the end UNLESS the last element is |undefined|
189 * e) Add square brackets to the beginning and end of the string
190 */
191 function toSource(arr)
192 {
193 var delim = CHAR_COMMA + CHAR_SPACE;
194 var elt = '';
195 var ret = '';
196 var len = arr.length;
197
198 for (i=0; i<len; i++)
199 {
200 elt = arr[i];
201
202 switch(true)
203 {
204 case (typeof elt === TYPE_STRING) :
205 ret += doubleQuote(elt);
206 break;
207
208 case (elt === undefined || elt === null) :
209 break; // add nothing but the delimiter, below -
210
211 default:
212 ret += elt.toString();
213 }
214
215 if ((i < len-1) || (elt === undefined))
216 ret += delim;
217 }
218
219 return CHAR_LBRACKET + ret + CHAR_RBRACKET;
220 }
221
222
223 function doubleQuote(text)
224 {
225 return CHAR_QT_DBL + text + CHAR_QT_DBL;
226 }
227
228
229 function singleQuote(text)
230 {
231 return CHAR_QT + text + CHAR_QT;
232 }
233