-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathgenerators.spec.ts
More file actions
171 lines (145 loc) · 4.07 KB
/
generators.spec.ts
File metadata and controls
171 lines (145 loc) · 4.07 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { LuaTarget } from "../../../src/CompilerOptions";
import { unsupportedForTarget } from "../../../src/transformation/utils/diagnostics";
import * as util from "../../util";
test("generator parameters", () => {
util.testFunction`
function* generator(value: number) {
yield value;
}
return generator(5).next();
`.expectToMatchJsResult();
});
test(".next()", () => {
util.testFunction`
function* generator() {
yield 1;
yield 2;
return 3;
}
const it = generator();
return [it.next(), it.next(), it.next(), it.next()];
`.expectToMatchJsResult();
});
test(".next() with parameters", () => {
util.testFunction`
function* generator() {
return yield 0;
}
const it = generator();
return [it.next(1), it.next(2), it.next(3)];
`.expectToMatchJsResult();
});
test("for..of", () => {
util.testFunction`
function* generator() {
yield 1;
yield 2;
yield undefined;
return 3;
}
const results = [];
for (const value of generator()) {
results.push({ value });
}
return results;
`.expectToMatchJsResult();
});
describe("yield*", () => {
test("generator", () => {
util.testFunction`
function* subGenerator() {
yield 1;
yield 2;
yield 3;
}
function* generator() {
yield 0;
return yield* subGenerator();
}
const it = generator();
return [it.next(), it.next(), it.next(), it.next(), it.next()];
`.expectToMatchJsResult();
});
test("array", () => {
util.testFunction`
function* generator() {
return yield* [1, 2, 3];
}
const it = generator();
return [it.next(), it.next(), it.next(), it.next()];
`.expectToMatchJsResult();
});
test("string", () => {
util.testFunction`
function* generator() {
return yield* "abc";
}
const it = generator();
return [it.next(), it.next(), it.next(), it.next()];
`.expectToMatchJsResult();
});
test("iterable", () => {
util.testFunction`
function* generator() {
return yield* new Set([1, 2, 3]);
}
const it = generator();
return [it.next(), it.next(), it.next(), it.next()];
`.expectToMatchJsResult();
});
});
test("function expression", () => {
util.testFunction`
const generator = function*() {
return true;
}
return generator().next();
`.expectToMatchJsResult();
});
test("class method", () => {
util.testFunction`
class A {
*generator() {
return true;
}
}
return new A().generator().next();
`.expectToMatchJsResult();
});
test("object member", () => {
util.testFunction`
const a = {
*generator() {
return true;
}
}
return a.generator().next();
`.expectToMatchJsResult();
});
test("hoisting", () => {
util.testFunction`
return generator().next();
function* generator() {
return true;
}
`.expectToMatchJsResult();
});
util.testEachVersion(
"generator yield inside try/catch",
() => util.testFunction`
function* generator() {
try {
yield 4;
} catch {
throw "something went wrong";
}
}
return generator().next();
`,
// Cannot execute LuaJIT with test runner
{
...util.expectEachVersionExceptJit(builder => builder.expectToMatchJsResult()),
[LuaTarget.Lua50]: builder => builder.expectToHaveDiagnostics([unsupportedForTarget.code]),
[LuaTarget.Lua51]: builder => builder.expectToHaveDiagnostics([unsupportedForTarget.code]),
}
);