Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_5 / misc / future-reserved-words.js
1 /*
2  * Any copyright is dedicated to the Public Domain.
3  * http://creativecommons.org/licenses/publicdomain/
4  */
5
6 //-----------------------------------------------------------------------------
7 var BUGNUMBER = 497869;
8 var summary = "Implement FutureReservedWords per-spec";
9
10 print(BUGNUMBER + ": " + summary);
11
12 /**************
13  * BEGIN TEST *
14  **************/
15
16 var futureReservedWords =
17   [
18    "class",
19    // "const", // Mozilla extension enabled even for versionless code
20    "enum",
21    "export",
22    "extends",
23    "import",
24    "super",
25   ];
26
27 var strictFutureReservedWords =
28   [
29    "implements",
30    "interface",
31    "let", // enabled: this file doesn't execute as JS1.7
32    "package",
33    "private",
34    "protected",
35    "public",
36    "static",
37    "yield", // enabled: this file doesn't execute as JS1.7
38   ];
39
40 function testWord(word, expectNormal, expectStrict)
41 {
42   var actual, status;
43
44   // USE AS LHS FOR ASSIGNMENT
45
46   if (expectNormal !== "skip")
47   {
48     actual = "";
49     status = summary + ": " + word + ": normal assignment";
50     try
51     {
52       eval(word + " = 'foo';");
53       actual = "no error";
54     }
55     catch(e)
56     {
57       actual = e.name;
58       status +=  ", " + e.name + ": " + e.message + " ";
59     }
60     reportCompare(expectNormal, actual, status);
61   }
62
63   actual = "";
64   status = summary + ": " + word + ": strict assignment";
65   try
66   {
67     eval("'use strict'; " + word + " = 'foo';");
68     actual = "no error";
69   }
70   catch(e)
71   {
72     actual = e.name;
73     status +=  ", " + e.name + ": " + e.message + " ";
74   }
75   reportCompare(expectStrict, actual, status);
76
77   // USE IN VARIABLE DECLARATION
78
79   if (expectNormal !== "skip")
80   {
81     actual = "";
82     status = summary + ": " + word + ": normal var";
83     try
84     {
85       eval("var " + word + ";");
86       actual = "no error";
87     }
88     catch (e)
89     {
90       actual = e.name;
91       status +=  ", " + e.name + ": " + e.message + " ";
92     }
93     reportCompare(expectNormal, actual, status);
94   }
95
96   actual = "";
97   status = summary + ": " + word + ": strict var";
98   try
99   {
100     eval("'use strict'; var " + word + ";");
101     actual = "no error";
102   }
103   catch (e)
104   {
105     actual = e.name;
106     status +=  ", " + e.name + ": " + e.message + " ";
107   }
108   reportCompare(expectStrict, actual, status);
109
110   // USE IN FOR-IN VARIABLE DECLARATION
111
112   if (expectNormal !== "skip")
113   {
114     actual = "";
115     status = summary + ": " + word + ": normal for-in var";
116     try
117     {
118       eval("for (var " + word + " in {});");
119       actual = "no error";
120     }
121     catch (e)
122     {
123       actual = e.name;
124       status +=  ", " + e.name + ": " + e.message + " ";
125     }
126     reportCompare(expectNormal, actual, status);
127   }
128
129   actual = "";
130   status = summary + ": " + word + ": strict for-in var";
131   try
132   {
133     eval("'use strict'; for (var " + word + " in {});");
134     actual = "no error";
135   }
136   catch (e)
137   {
138     actual = e.name;
139     status +=  ", " + e.name + ": " + e.message + " ";
140   }
141   reportCompare(expectStrict, actual, status);
142
143   // USE AS CATCH IDENTIFIER
144
145   if (expectNormal !== "skip")
146   {
147     actual = "";
148     status = summary + ": " + word + ": normal var";
149     try
150     {
151       eval("try { } catch (" + word + ") { }");
152       actual = "no error";
153     }
154     catch (e)
155     {
156       actual = e.name;
157       status +=  ", " + e.name + ": " + e.message + " ";
158     }
159     reportCompare(expectNormal, actual, status);
160   }
161
162   actual = "";
163   status = summary + ": " + word + ": strict var";
164   try
165   {
166     eval("'use strict'; try { } catch (" + word + ") { }");
167     actual = "no error";
168   }
169   catch (e)
170   {
171     actual = e.name;
172     status +=  ", " + e.name + ": " + e.message + " ";
173   }
174   reportCompare(expectStrict, actual, status);
175
176   // USE AS LABEL
177
178   if (expectNormal !== "skip")
179   {
180     actual = "";
181     status = summary + ": " + word + ": normal label";
182     try
183     {
184       eval(word + ": while (false);");
185       actual = "no error";
186     }
187     catch (e)
188     {
189       actual = e.name;
190       status +=  ", " + e.name + ": " + e.message + " ";
191     }
192     reportCompare(expectNormal, actual, status);
193   }
194
195   actual = "";
196   status = summary + ": " + word + ": strict label";
197   try
198   {
199     eval("'use strict'; " + word + ": while (false);");
200     actual = "no error";
201   }
202   catch (e)
203   {
204     actual = e.name;
205     status +=  ", " + e.name + ": " + e.message + " ";
206   }
207   reportCompare(expectStrict, actual, status);
208
209   // USE AS ARGUMENT NAME IN FUNCTION DECLARATION
210
211   if (expectNormal !== "skip")
212   {
213     actual = "";
214     status = summary + ": " + word + ": normal function argument";
215     try
216     {
217       eval("function foo(" + word + ") { }");
218       actual = "no error";
219     }
220     catch (e)
221     {
222       actual = e.name;
223       status +=  ", " + e.name + ": " + e.message + " ";
224     }
225     reportCompare(expectNormal, actual, status);
226   }
227
228   actual = "";
229   status = summary + ": " + word + ": strict function argument";
230   try
231   {
232     eval("'use strict'; function foo(" + word + ") { }");
233     actual = "no error";
234   }
235   catch (e)
236   {
237     actual = e.name;
238     status +=  ", " + e.name + ": " + e.message + " ";
239   }
240   reportCompare(expectStrict, actual, status);
241
242   actual = "";
243   status = summary + ": " + word + ": function argument retroactively strict";
244   try
245   {
246     eval("function foo(" + word + ") { 'use strict'; }");
247     actual = "no error";
248   }
249   catch (e)
250   {
251     actual = e.name;
252     status +=  ", " + e.name + ": " + e.message + " ";
253   }
254   reportCompare(expectStrict, actual, status);
255
256   // USE AS ARGUMENT NAME IN FUNCTION EXPRESSION
257
258   if (expectNormal !== "skip")
259   {
260     actual = "";
261     status = summary + ": " + word + ": normal function expression argument";
262     try
263     {
264       eval("var s = (function foo(" + word + ") { });");
265       actual = "no error";
266     }
267     catch (e)
268     {
269       actual = e.name;
270       status +=  ", " + e.name + ": " + e.message + " ";
271     }
272     reportCompare(expectNormal, actual, status);
273   }
274
275   actual = "";
276   status = summary + ": " + word + ": strict function expression argument";
277   try
278   {
279     eval("'use strict'; var s = (function foo(" + word + ") { });");
280     actual = "no error";
281   }
282   catch (e)
283   {
284     actual = e.name;
285     status +=  ", " + e.name + ": " + e.message + " ";
286   }
287   reportCompare(expectStrict, actual, status);
288
289   actual = "";
290   status = summary + ": " + word + ": function expression argument retroactively strict";
291   try
292   {
293     eval("var s = (function foo(" + word + ") { 'use strict'; });");
294     actual = "no error";
295   }
296   catch (e)
297   {
298     actual = e.name;
299     status +=  ", " + e.name + ": " + e.message + " ";
300   }
301   reportCompare(expectStrict, actual, status);
302
303   // USE AS ARGUMENT NAME WITH FUNCTION CONSTRUCTOR
304
305   if (expectNormal !== "skip")
306   {
307     actual = "";
308     status = summary + ": " + word + ": argument with normal Function";
309     try
310     {
311       Function(word, "return 17");
312       actual = "no error";
313     }
314     catch (e)
315     {
316       actual = e.name;
317       status +=  ", " + e.name + ": " + e.message + " ";
318     }
319     reportCompare(expectNormal, actual, status);
320   }
321
322   actual = "";
323   status = summary + ": " + word + ": argument with strict Function";
324   try
325   {
326     Function(word, "'use strict'; return 17");
327     actual = "no error";
328   }
329   catch (e)
330   {
331     actual = e.name;
332     status +=  ", " + e.name + ": " + e.message + " ";
333   }
334   reportCompare(expectStrict, actual, status);
335
336   // USE AS ARGUMENT NAME IN PROPERTY SETTER
337
338   if (expectNormal !== "skip")
339   {
340     actual = "";
341     status = summary + ": " + word + ": normal property setter argument";
342     try
343     {
344       eval("var o = { set x(" + word + ") { } };");
345       actual = "no error";
346     }
347     catch (e)
348     {
349       actual = e.name;
350       status +=  ", " + e.name + ": " + e.message + " ";
351     }
352     reportCompare(expectNormal, actual, status);
353   }
354
355   actual = "";
356   status = summary + ": " + word + ": strict property setter argument";
357   try
358   {
359     eval("'use strict'; var o = { set x(" + word + ") { } };");
360     actual = "no error";
361   }
362   catch (e)
363   {
364     actual = e.name;
365     status +=  ", " + e.name + ": " + e.message + " ";
366   }
367   reportCompare(expectStrict, actual, status);
368
369   actual = "";
370   status = summary + ": " + word + ": property setter argument retroactively strict";
371   try
372   {
373     eval("var o = { set x(" + word + ") { 'use strict'; } };");
374     actual = "no error";
375   }
376   catch (e)
377   {
378     actual = e.name;
379     status +=  ", " + e.name + ": " + e.message + " ";
380   }
381   reportCompare(expectStrict, actual, status);
382
383   // USE AS FUNCTION NAME IN FUNCTION DECLARATION
384
385   if (expectNormal !== "skip")
386   {
387     actual = "";
388     status = summary + ": " + word + ": normal function name";
389     try
390     {
391       eval("function " + word + "() { }");
392       actual = "no error";
393     }
394     catch (e)
395     {
396       actual = e.name;
397       status +=  ", " + e.name + ": " + e.message + " ";
398     }
399     reportCompare(expectNormal, actual, status);
400   }
401
402   actual = "";
403   status = summary + ": " + word + ": strict function name";
404   try
405   {
406     eval("'use strict'; function " + word + "() { }");
407     actual = "no error";
408   }
409   catch (e)
410   {
411     actual = e.name;
412     status +=  ", " + e.name + ": " + e.message + " ";
413   }
414   reportCompare(expectStrict, actual, status);
415
416   actual = "";
417   status = summary + ": " + word + ": function name retroactively strict";
418   try
419   {
420     eval("function " + word + "() { 'use strict'; }");
421     actual = "no error";
422   }
423   catch (e)
424   {
425     actual = e.name;
426     status +=  ", " + e.name + ": " + e.message + " ";
427   }
428   reportCompare(expectStrict, actual, status);
429
430   // USE AS FUNCTION NAME IN FUNCTION EXPRESSION
431
432   if (expectNormal !== "skip")
433   {
434     actual = "";
435     status = summary + ": " + word + ": normal function expression name";
436     try
437     {
438       eval("var s = (function " + word + "() { });");
439       actual = "no error";
440     }
441     catch (e)
442     {
443       actual = e.name;
444       status +=  ", " + e.name + ": " + e.message + " ";
445     }
446     reportCompare(expectNormal, actual, status);
447   }
448
449   actual = "";
450   status = summary + ": " + word + ": strict function expression name";
451   try
452   {
453     eval("'use strict'; var s = (function " + word + "() { });");
454     actual = "no error";
455   }
456   catch (e)
457   {
458     actual = e.name;
459     status +=  ", " + e.name + ": " + e.message + " ";
460   }
461   reportCompare(expectStrict, actual, status);
462
463   actual = "";
464   status = summary + ": " + word + ": function expression name retroactively strict";
465   try
466   {
467     eval("var s = (function " + word + "() { 'use strict'; });");
468     actual = "no error";
469   }
470   catch (e)
471   {
472     actual = e.name;
473     status +=  ", " + e.name + ": " + e.message + " ";
474   }
475   reportCompare(expectStrict, actual, status);
476 }
477
478 function testFutureReservedWord(word)
479 {
480   /*
481    * NON-STANDARD DEVIATION: At one point in history SpiderMonkey unreserved
482    * most of the reserved words in ES3, including those words which are
483    * FutureReservedWords in ES5.  It's too late this release cycle to expect
484    * "SyntaxError" for the normal code cases, so for now we "skip" testing
485    * these words in normal code.  (We don't test for "no error" because that
486    * would be contrary to the spec, and this test is not in an extensions/
487    * directory.)
488    */
489   testWord(word, "skip", "SyntaxError");
490 }
491
492 function testStrictFutureReservedWord(word)
493 {
494   testWord(word, "no error", "SyntaxError");
495 }
496
497 futureReservedWords.forEach(testFutureReservedWord);
498 strictFutureReservedWords.forEach(testStrictFutureReservedWord);
499
500 /******************************************************************************/
501
502 if (typeof reportCompare === "function")
503   reportCompare(true, true);
504
505 print("All tests passed!");