]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/stress/trailing-comma-in-patterns.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / trailing-comma-in-patterns.js
1 function shouldBe(actual, expected) {
2 if (actual !== expected)
3 throw new Error('bad value: ' + actual);
4 }
5
6 function iterator(array) {
7 var nextCount = 0;
8 var returnCount = 0;
9 var original = array.values();
10 return {
11 [Symbol.iterator]() {
12 return this;
13 },
14
15 next() {
16 ++nextCount;
17 return original.next();
18 },
19
20 return() {
21 ++returnCount;
22 return { done: true };
23 },
24
25 reportNext() {
26 return nextCount;
27 },
28
29 reportReturn() {
30 return returnCount;
31 }
32 };
33 };
34
35 (function () {
36 var iter = iterator([1, 2, 3]);
37 var [] = iter;
38 shouldBe(iter.reportNext(), 0);
39 shouldBe(iter.reportReturn(), 1);
40 }());
41
42 (function () {
43 var iter = iterator([1, 2, 3]);
44 var [,] = iter;
45 shouldBe(iter.reportNext(), 1);
46 shouldBe(iter.reportReturn(), 1);
47 }());
48
49 (function () {
50 var iter = iterator([1, 2, 3]);
51 var [,,] = iter;
52 shouldBe(iter.reportNext(), 2);
53 shouldBe(iter.reportReturn(), 1);
54 }());
55
56 (function () {
57 var iter = iterator([1, 2, 3]);
58 var [,,,] = iter;
59 shouldBe(iter.reportNext(), 3);
60 shouldBe(iter.reportReturn(), 1);
61 }());
62
63 (function () {
64 var iter = iterator([1, 2, 3]);
65 var [,,,,] = iter;
66 shouldBe(iter.reportNext(), 4);
67 shouldBe(iter.reportReturn(), 0);
68 }());
69
70 (function () {
71 var iter = iterator([1, 2, 3]);
72 var [,,,,,] = iter;
73 shouldBe(iter.reportNext(), 4);
74 shouldBe(iter.reportReturn(), 0);
75 }());
76
77 (function () {
78 var iter = iterator([1, 2, 3]);
79 var [,a,] = iter;
80 shouldBe(iter.reportNext(), 2);
81 shouldBe(iter.reportReturn(), 1);
82 shouldBe(a, 2);
83 }());
84
85 (function () {
86 var iter = iterator([1, 2, 3]);
87 var [a,] = iter;
88 shouldBe(iter.reportNext(), 1);
89 shouldBe(iter.reportReturn(), 1);
90 shouldBe(a, 1);
91 }());
92
93 (function () {
94 var iter = iterator([1, 2, 3]);
95 var [a,,] = iter;
96 shouldBe(iter.reportNext(), 2);
97 shouldBe(iter.reportReturn(), 1);
98 shouldBe(a, 1);
99 }());
100
101 (function () {
102 var iter = iterator([1, 2, 3]);
103 var [a,b = 42,] = iter;
104 shouldBe(iter.reportNext(), 2);
105 shouldBe(iter.reportReturn(), 1);
106 shouldBe(a, 1);
107 shouldBe(b, 2);
108 }());
109
110 (function () {
111 var {} = { Cocoa: 15, Cappuccino: 13 };
112 }());
113
114 (function () {
115 var {Cocoa,} = { Cocoa: 15, Cappuccino: 13 };
116 shouldBe(Cocoa, 15);
117 }());
118
119 (function () {
120 var {Cocoa = 'Cocoa',} = { Cocoa: 15, Cappuccino: 13 };
121 shouldBe(Cocoa, 15);
122 }());
123
124 (function () {
125 var {Cocoa, Kilimanjaro = 'Coffee'} = { Cocoa: 15, Cappuccino: 13 };
126 shouldBe(Cocoa, 15);
127 shouldBe(Kilimanjaro, 'Coffee');
128 }());
129
130 (function () {
131 var {Cocoa, Kilimanjaro = 'Coffee'} = {};
132 shouldBe(Cocoa, undefined);
133 shouldBe(Kilimanjaro, 'Coffee');
134 }());
135
136 (function () {
137 var {Cocoa, Kilimanjaro = 'Coffee',} = { Cocoa: 15, Cappuccino: 13 };
138 shouldBe(Cocoa, 15);
139 shouldBe(Kilimanjaro, 'Coffee');
140 }());
141
142 function testSyntaxError(script, message) {
143 var error = null;
144 try {
145 eval(script);
146 } catch (e) {
147 error = e;
148 }
149 if (!error)
150 throw new Error("Expected syntax error not thrown");
151
152 if (String(error) !== message)
153 throw new Error("Bad error: " + String(error));
154 }
155
156 testSyntaxError(String.raw`var {,} = {Cocoa: 15}`, String.raw`SyntaxError: Unexpected token ','. Expected a property name.`);
157 testSyntaxError(String.raw`var {,} = {}`, String.raw`SyntaxError: Unexpected token ','. Expected a property name.`);