const.js revision cfb0617749a64f2e177386b030d46007b8c4b179
1description(
2"This test checks that const declarations in JavaScript work and are readonly."
3);
4
5
6shouldThrow("const redef='a'; const redef='a';");
7
8const x = "RIGHT";
9x = "WRONG";
10shouldBe("x", '"RIGHT"');
11
12const z = "RIGHT", y = "RIGHT";
13y = "WRONG";
14shouldBe("y", '"RIGHT"');
15
16const one = 1;
17
18var a;
19
20// PostIncResolveNode
21a = one++;
22shouldBe("a", "1");
23shouldBe("one", "1");
24
25// PostDecResolveNode
26a = one--;
27shouldBe("a", "1");
28shouldBe("one", "1");
29
30// PreIncResolveNode
31a = ++one;
32shouldBe("a", "2");
33shouldBe("one", "1");
34
35// PreDecResolveNode
36a = --one;
37shouldBe("a", "0");
38shouldBe("one", "1");
39
40// ReadModifyConstNode
41a = one += 2;
42shouldBe("a", "3");
43shouldBe("one", "1");
44
45// AssignConstNode
46a = one = 2;
47shouldBe("a", "2");
48shouldBe("one", "1");
49
50// PostIncResolveNode
51shouldBe("function f() { const one = 1; one++; return one; } f();", "1");
52shouldBe("function f() { const oneString = '1'; return oneString++; } f();", "1");
53shouldBe("function f() { const one = 1; return one++; } f();", "1");
54
55// PostDecResolveNode
56shouldBe("function f() { const one = 1; one--; return one; } f();", "1");
57shouldBe("function f() { const oneString = '1'; return oneString--; } f();", "1");
58shouldBe("function f() { const one = 1; return one--; } f();", "1");
59
60// PreIncResolveNode
61shouldBe("function f() { const one = 1; ++one; return one; } f();", "1");
62shouldBe("function f() { const one = 1; return ++one; } f();", "2");
63
64// PreDecResolveNode
65shouldBe("function f() { const one = 1; --one; return one; } f();", "1");
66shouldBe("function f() { const one = 1; return --one; } f();", "0");
67
68// ReadModifyConstNode
69shouldBe("function f() { const one = 1; one += 2; return one; } f();", "1");
70shouldBe("function f() { const one = 1; return one += 2; } f();", "3");
71
72// AssignConstNode
73shouldBe("function f() { const one = 1; one = 2; return one; } f();", "1");
74shouldBe("function f() { const one = 1; return one = 2; } f();", "2");
75
76// PostIncResolveNode
77shouldBe("one++", "1");
78shouldBe("one", "1");
79
80// PostDecResolveNode
81shouldBe("one--", "1");
82shouldBe("one", "1");
83
84// PreIncResolveNode
85shouldBe("++one", "2");
86shouldBe("one", "1");
87
88// PreDecResolveNode
89shouldBe("--one", "0");
90shouldBe("one", "1");
91
92// ReadModifyConstNode
93shouldBe("one += 1", "2");
94shouldBe("one", "1");
95
96// AssignConstNode
97shouldBe("one = 2", "2");
98shouldBe("one", "1");
99
100var object = { inWith1: "RIGHT", inWith2: ""}
101with (object) {
102    const inWith1 = "WRONG";
103    const inWith2 = "RIGHT";
104    inWith2 = "WRONG";
105}
106shouldBe("object.inWith1", "'RIGHT'");
107shouldBe("inWith2", "'RIGHT'");
108
109shouldBe("(function(){ one = 2; return one; })()", "1")
110var f = function g() { g="FAIL"; return g; };
111shouldBe("f()", "f");
112
113shouldBe("const a;", "undefined");
114
115// Make sure we don't override properties placed on the global object
116var ranConstInitialiser = false;
117const bodyId = (ranConstInitialiser = true, "Const initialiser overwrote existing property");
118shouldBe("bodyId", "document.getElementById('bodyId')");
119shouldBeTrue("ranConstInitialiser");
120var successfullyParsed = true;
121