d19a34a2c38a873375b37e92a26bdd551d8c144f
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / harmony / block-conflicts.js
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // Flags: --harmony-scoping
6
7 // Test for conflicting variable bindings.
8
9 "use strict";
10
11 function CheckException(e) {
12   var string = e.toString();
13   assertTrue(string.indexOf("has already been declared") >= 0 ||
14              string.indexOf("redeclaration") >= 0);
15   return 'Conflict';
16 }
17
18
19 function TestGlobal(s,e) {
20   try {
21     return eval(s + e);
22   } catch (x) {
23     return CheckException(x);
24   }
25 }
26
27
28 function TestFunction(s,e) {
29   try {
30     return eval("(function(){" + s + " return " + e + "})")();
31   } catch (x) {
32     return CheckException(x);
33   }
34 }
35
36
37 function TestBlock(s,e) {
38   try {
39     return eval("(function(){ {" + s + "} return " + e + "})")();
40   } catch (x) {
41     return CheckException(x);
42   }
43 }
44
45 function TestAll(expected,s,opt_e) {
46   var e = "";
47   var msg = s;
48   if (opt_e) { e = opt_e; msg += opt_e; }
49   assertEquals(expected === 'LocalConflict' ? 'NoConflict' : expected,
50       TestGlobal(s,e), "global:'" + msg + "'");
51   assertEquals(expected === 'LocalConflict' ? 'NoConflict' : expected,
52       TestFunction(s,e), "function:'" + msg + "'");
53   assertEquals(expected === 'LocalConflict' ? 'Conflict' : expected,
54       TestBlock(s,e), "block:'" + msg + "'");
55 }
56
57
58 function TestConflict(s) {
59   TestAll('Conflict', s);
60   TestAll('Conflict', 'eval("' + s + '");');
61 }
62
63 function TestNoConflict(s) {
64   TestAll('NoConflict', s, "'NoConflict'");
65   TestAll('NoConflict', 'eval("' + s + '");', "'NoConflict'");
66 }
67
68 function TestLocalConflict(s) {
69   TestAll('LocalConflict', s, "'NoConflict'");
70   TestAll('NoConflict', 'eval("' + s + '");', "'NoConflict'");
71 }
72
73 var letbinds = [ "let x;",
74                  "let x = 0;",
75                  "let x = undefined;",
76                  "let x = function() {};",
77                  "let x, y;",
78                  "let y, x;",
79                  "const x = 0;",
80                  "const x = undefined;",
81                  "const x = function() {};",
82                  "const x = 2, y = 3;",
83                  "const y = 4, x = 5;",
84                  ];
85 var varbinds = [ "var x;",
86                  "var x = 0;",
87                  "var x = undefined;",
88                  "var x = function() {};",
89                  "var x, y;",
90                  "var y, x;",
91                  ];
92 var funbind = "function x() {}";
93
94 for (var l = 0; l < letbinds.length; ++l) {
95   // Test conflicting let/var bindings.
96   for (var v = 0; v < varbinds.length; ++v) {
97     // Same level.
98     TestConflict(letbinds[l] + varbinds[v]);
99     TestConflict(varbinds[v] + letbinds[l]);
100     // Different level.
101     TestConflict(letbinds[l] + '{' + varbinds[v] + '}');
102     TestConflict('{' + varbinds[v] +'}' + letbinds[l]);
103     TestNoConflict(varbinds[v] + '{' + letbinds[l] + '}');
104     TestNoConflict('{' + letbinds[l] + '}' + varbinds[v]);
105     // For loop.
106     TestConflict('for (' + letbinds[l] + '0;) {' + varbinds[v] + '}');
107     TestNoConflict('for (' + varbinds[v] + '0;) {' + letbinds[l] + '}');
108   }
109
110   // Test conflicting let/let bindings.
111   for (var k = 0; k < letbinds.length; ++k) {
112     // Same level.
113     TestConflict(letbinds[l] + letbinds[k]);
114     TestConflict(letbinds[k] + letbinds[l]);
115     // Different level.
116     TestNoConflict(letbinds[l] + '{ ' + letbinds[k] + '}');
117     TestNoConflict('{' + letbinds[k] +'} ' + letbinds[l]);
118     // For loop.
119     TestNoConflict('for (' + letbinds[l] + '0;) {' + letbinds[k] + '}');
120     TestNoConflict('for (' + letbinds[k] + '0;) {' + letbinds[l] + '}');
121   }
122
123   // Test conflicting function/let bindings.
124   // Same level.
125   TestConflict(letbinds[l] + funbind);
126   TestConflict(funbind + letbinds[l]);
127   // Different level.
128   TestNoConflict(letbinds[l] + '{' + funbind + '}');
129   TestNoConflict('{' + funbind + '}' + letbinds[l]);
130   TestNoConflict(funbind + '{' + letbinds[l] + '}');
131   TestNoConflict('{' + letbinds[l] + '}' + funbind);
132   // For loop.
133   TestNoConflict('for (' + letbinds[l] + '0;) {' + funbind + '}');
134
135   // Test conflicting parameter/let bindings.
136   TestConflict('(function(x) {' + letbinds[l] + '})();');
137 }
138
139 // Test conflicting function/var bindings.
140 for (var v = 0; v < varbinds.length; ++v) {
141   // Same level.
142   TestLocalConflict(varbinds[v] + funbind);
143   TestLocalConflict(funbind + varbinds[v]);
144   // Different level.
145   TestLocalConflict(funbind + '{' + varbinds[v] + '}');
146   TestLocalConflict('{' + varbinds[v] +'}' + funbind);
147   TestNoConflict(varbinds[v] + '{' + funbind + '}');
148   TestNoConflict('{' + funbind + '}' + varbinds[v]);
149   // For loop.
150   TestNoConflict('for (' + varbinds[v] + '0;) {' + funbind + '}');
151 }
152
153 // Test conflicting catch/var bindings.
154 for (var v = 0; v < varbinds.length; ++v) {
155   TestNoConflict('try {} catch(x) {' + varbinds[v] + '}');
156 }
157
158 // Test conflicting parameter/var bindings.
159 for (var v = 0; v < varbinds.length; ++v) {
160   TestNoConflict('(function (x) {' + varbinds[v] + '})();');
161 }
162
163 // Test conflicting catch/function bindings.
164 TestNoConflict('try {} catch(x) {' + funbind + '}');
165
166 // Test conflicting parameter/function bindings.
167 TestNoConflict('(function (x) {' + funbind + '})();');