2 "This test checks that const declarations in JavaScript work and are readonly."
6 shouldThrow("const redef='a'; const redef='a';");
10 shouldBe("x", '"RIGHT"');
12 const z = "RIGHT", y = "RIGHT";
14 shouldBe("y", '"RIGHT"');
40 // ReadModifyConstNode
51 shouldBe("function f() { const one = 1; one++; return one; } f();", "1");
52 shouldBe("function f() { const oneString = '1'; return oneString++; } f();", "1");
53 shouldBe("function f() { const one = 1; return one++; } f();", "1");
56 shouldBe("function f() { const one = 1; one--; return one; } f();", "1");
57 shouldBe("function f() { const oneString = '1'; return oneString--; } f();", "1");
58 shouldBe("function f() { const one = 1; return one--; } f();", "1");
61 shouldBe("function f() { const one = 1; ++one; return one; } f();", "1");
62 shouldBe("function f() { const one = 1; return ++one; } f();", "2");
65 shouldBe("function f() { const one = 1; --one; return one; } f();", "1");
66 shouldBe("function f() { const one = 1; return --one; } f();", "0");
68 // ReadModifyConstNode
69 shouldBe("function f() { const one = 1; one += 2; return one; } f();", "1");
70 shouldBe("function f() { const one = 1; return one += 2; } f();", "3");
73 shouldBe("function f() { const one = 1; one = 2; return one; } f();", "1");
74 shouldBe("function f() { const one = 1; return one = 2; } f();", "2");
77 shouldBe("one++", "1");
81 shouldBe("one--", "1");
85 shouldBe("++one", "2");
89 shouldBe("--one", "0");
92 // ReadModifyConstNode
93 shouldBe("one += 1", "2");
97 shouldBe("one = 2", "2");
100 var object = { inWith1: "RIGHT", inWith2: ""}
102 const inWith1 = "WRONG";
103 const inWith2 = "RIGHT";
106 shouldBe("object.inWith1", "'RIGHT'");
107 shouldBe("inWith2", "'RIGHT'");
109 shouldBe("(function(){ one = 2; return one; })()", "1")
110 var f = function g() { g="FAIL"; return g; };
111 shouldBe("f()", "f");
113 shouldBe("const a;", "undefined");
115 // Make sure we don't override properties placed on the global object
116 var ranConstInitialiser = false;
117 const bodyId = (ranConstInitialiser = true, "Const initialiser overwrote existing property");
118 shouldBe("bodyId", "document.getElementById('bodyId')");
119 shouldBeTrue("ranConstInitialiser");
121 // Make sure that dynamic scopes (catch, with) don't break const declarations
122 function tryCatch1() {
132 function tryCatch2() {
142 tryCatch1Result = tryCatch1();
143 shouldBe("tryCatch1Result", "5");
144 tryCatch2Result = tryCatch2();
145 shouldBe("tryCatch2Result", "5");
149 eval("with({foo:42}) const bar = 5;");
155 with({foo:42}) const bar = 5;
159 with1Result = with1();
160 shouldBe("with1Result", "5");
161 with2Result = with2();
162 shouldBe("with2Result", "5");