1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
describe("correct behavior", () => {
test("iterate through array", () => {
const a = [];
for (const num of [1, 2, 3]) {
a.push(num);
}
expect(a).toEqual([1, 2, 3]);
});
test("iterate through string", () => {
const a = [];
for (const char of "hello") {
a.push(char);
}
expect(a).toEqual(["h", "e", "l", "l", "o"]);
});
test("iterate through string object", () => {
const a = [];
for (const char of new String("hello")) {
a.push(char);
}
expect(a).toEqual(["h", "e", "l", "l", "o"]);
});
test("use already-declared variable", () => {
var char;
for (char of "abc");
expect(char).toBe("c");
});
test("respects custom Symbol.iterator method", () => {
const o = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i++ == 3) {
return { done: true };
}
return { value: this.i, done: false };
},
};
},
};
const a = [];
for (const k of o) {
a.push(k);
}
expect(a).toEqual([1, 2, 3]);
});
test("loops through custom iterator if there is an exception thrown part way through", () => {
// This tests against the way custom iterators used to be implemented, where the values
// were all collected at once before the for-of body was executed, instead of getting
// the values one at a time
const o = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i++ === 3) {
throw new Error();
}
return { value: this.i };
},
};
},
};
const a = [];
try {
for (let k of o) {
a.push(k);
}
expect().fail();
} catch (e) {
expect(a).toEqual([1, 2, 3]);
}
});
});
describe("errors", () => {
test("right hand side is a primitive", () => {
expect(() => {
for (const _ of 123) {
}
}).toThrowWithMessage(TypeError, "123 is not iterable");
});
test("right hand side is an object", () => {
expect(() => {
for (const _ of { foo: 1, bar: 2 }) {
}
}).toThrowWithMessage(TypeError, "[object Object] is not iterable");
});
});
test("allow binding patterns", () => {
expect(`for (let [a, b] of foo) {}`).toEval();
});
|