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