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