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.
5 // Test for conflicting variable bindings.
9 function CheckException(e) {
10 var string = e.toString();
11 assertTrue(string.indexOf("has already been declared") >= 0 ||
12 string.indexOf("redeclaration") >= 0);
17 function TestGlobal(s,e) {
21 return CheckException(x);
26 function TestFunction(s,e) {
28 return eval("(function(){" + s + " return " + e + "})")();
30 return CheckException(x);
35 function TestBlock(s,e) {
37 return eval("(function(){ {" + s + "} return " + e + "})")();
39 return CheckException(x);
43 function TestAll(expected,s,opt_e) {
46 if (opt_e) { e = opt_e; msg += opt_e; }
47 assertEquals(expected === 'LocalConflict' ? 'NoConflict' : expected,
48 TestGlobal(s,e), "global:'" + msg + "'");
49 assertEquals(expected === 'LocalConflict' ? 'NoConflict' : expected,
50 TestFunction(s,e), "function:'" + msg + "'");
51 assertEquals(expected === 'LocalConflict' ? 'Conflict' : expected,
52 TestBlock(s,e), "block:'" + msg + "'");
56 function TestConflict(s) {
57 TestAll('Conflict', s);
58 TestAll('Conflict', 'eval("' + s + '");');
61 function TestNoConflict(s) {
62 TestAll('NoConflict', s, "'NoConflict'");
63 TestAll('NoConflict', 'eval("' + s + '");', "'NoConflict'");
66 function TestLocalConflict(s) {
67 TestAll('LocalConflict', s, "'NoConflict'");
68 TestAll('NoConflict', 'eval("' + s + '");', "'NoConflict'");
71 var letbinds = [ "let x;",
74 "let x = function() {};",
78 "const x = undefined;",
79 "const x = function() {};",
80 "const x = 2, y = 3;",
81 "const y = 4, x = 5;",
83 var varbinds = [ "var x;",
86 "var x = function() {};",
90 var funbind = "function x() {}";
92 for (var l = 0; l < letbinds.length; ++l) {
93 // Test conflicting let/var bindings.
94 for (var v = 0; v < varbinds.length; ++v) {
96 TestConflict(letbinds[l] + varbinds[v]);
97 TestConflict(varbinds[v] + letbinds[l]);
99 TestConflict(letbinds[l] + '{' + varbinds[v] + '}');
100 TestConflict('{' + varbinds[v] +'}' + letbinds[l]);
101 TestNoConflict(varbinds[v] + '{' + letbinds[l] + '}');
102 TestNoConflict('{' + letbinds[l] + '}' + varbinds[v]);
104 TestConflict('for (' + letbinds[l] + '0;) {' + varbinds[v] + '}');
105 TestNoConflict('for (' + varbinds[v] + '0;) {' + letbinds[l] + '}');
108 // Test conflicting let/let bindings.
109 for (var k = 0; k < letbinds.length; ++k) {
111 TestConflict(letbinds[l] + letbinds[k]);
112 TestConflict(letbinds[k] + letbinds[l]);
114 TestNoConflict(letbinds[l] + '{ ' + letbinds[k] + '}');
115 TestNoConflict('{' + letbinds[k] +'} ' + letbinds[l]);
117 TestNoConflict('for (' + letbinds[l] + '0;) {' + letbinds[k] + '}');
118 TestNoConflict('for (' + letbinds[k] + '0;) {' + letbinds[l] + '}');
121 // Test conflicting function/let bindings.
123 TestConflict(letbinds[l] + funbind);
124 TestConflict(funbind + letbinds[l]);
126 TestNoConflict(letbinds[l] + '{' + funbind + '}');
127 TestNoConflict('{' + funbind + '}' + letbinds[l]);
128 TestNoConflict(funbind + '{' + letbinds[l] + '}');
129 TestNoConflict('{' + letbinds[l] + '}' + funbind);
131 TestNoConflict('for (' + letbinds[l] + '0;) {' + funbind + '}');
133 // Test conflicting parameter/let bindings.
134 TestConflict('(function(x) {' + letbinds[l] + '})();');
137 // Test conflicting function/var bindings.
138 for (var v = 0; v < varbinds.length; ++v) {
140 TestLocalConflict(varbinds[v] + funbind);
141 TestLocalConflict(funbind + varbinds[v]);
143 TestLocalConflict(funbind + '{' + varbinds[v] + '}');
144 TestLocalConflict('{' + varbinds[v] +'}' + funbind);
145 TestNoConflict(varbinds[v] + '{' + funbind + '}');
146 TestNoConflict('{' + funbind + '}' + varbinds[v]);
148 TestNoConflict('for (' + varbinds[v] + '0;) {' + funbind + '}');
151 // Test conflicting catch/var bindings.
152 for (var v = 0; v < varbinds.length; ++v) {
153 TestNoConflict('try {} catch(x) {' + varbinds[v] + '}');
156 // Test conflicting parameter/var bindings.
157 for (var v = 0; v < varbinds.length; ++v) {
158 TestNoConflict('(function (x) {' + varbinds[v] + '})();');
161 // Test conflicting catch/function bindings.
162 TestNoConflict('try {} catch(x) {' + funbind + '}');
164 // Test conflicting parameter/function bindings.
165 TestNoConflict('(function (x) {' + funbind + '})();');