Skip to content

Commit 73c4d83

Browse files
committed
Add tests for async lambdas and throws in async functions
1 parent b487ff1 commit 73c4d83

2 files changed

Lines changed: 124 additions & 29 deletions

File tree

src/lualib/Await.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ function __TS__AsyncAwaiter(this: void, generator: (this: void) => void) {
2525
}
2626
function fulfilled(value) {
2727
try {
28-
const [_, result] = coroutine.resume(asyncCoroutine, value);
29-
step(result);
28+
const [success, resultOrError] = coroutine.resume(asyncCoroutine, value);
29+
if (success) {
30+
step(resultOrError);
31+
} else {
32+
reject(resultOrError);
33+
}
3034
} catch (e) {
3135
reject(e);
3236
}
@@ -38,8 +42,12 @@ function __TS__AsyncAwaiter(this: void, generator: (this: void) => void) {
3842
adopt(result).then(fulfilled, reason => reject(reason));
3943
}
4044
}
41-
const [_, result] = coroutine.resume(asyncCoroutine);
42-
step(result);
45+
const [success, resultOrError] = coroutine.resume(asyncCoroutine);
46+
if (success) {
47+
step(resultOrError);
48+
} else {
49+
reject(resultOrError);
50+
}
4351
});
4452
}
4553

test/unit/builtins/async-await.spec.ts

Lines changed: 112 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,36 @@ test("can await pending promise", () => {
6767
.expectToEqual(["resolving original promise", "resolved data", "resolving awaiting promise", "resolved data"]);
6868
});
6969

70-
test("can return non-promise from async function", () => {
70+
test("can await non-promise values", () => {
7171
util.testFunction`
72+
async function foo() {
73+
return await "foo";
74+
}
75+
76+
async function bar() {
77+
return await { foo: await foo(), bar: "bar" };
78+
}
79+
80+
async function baz() {
81+
return (await bar()).foo + (await bar()).bar;
82+
}
83+
84+
const { state, value } = baz() as any;
85+
return { state, value };
86+
`.expectToEqual({
87+
state: 1, // __TS__PromiseState.Fulfilled
88+
value: "foobar",
89+
});
90+
});
91+
92+
test.each(["async function abc() {", "const abc = async () => {"])(
93+
"can return non-promise from async function (%p)",
94+
functionHeader => {
95+
util.testFunction`
7296
const { promise, resolve } = defer<string>();
7397
promise.then(data => log("resolving original promise", data));
7498
75-
async function abc() {
99+
${functionHeader}
76100
await promise;
77101
return "abc return data"
78102
}
@@ -85,25 +109,28 @@ test("can return non-promise from async function", () => {
85109
return allLogs;
86110
87111
`
88-
.setTsHeader(promiseTestLib)
89-
.expectToEqual([
90-
"resolving original promise",
91-
"resolved data",
92-
"resolving awaiting promise",
93-
"abc return data",
94-
]);
95-
});
96-
97-
test("can have multiple awaits in async function", () => {
98-
util.testFunction`
112+
.setTsHeader(promiseTestLib)
113+
.expectToEqual([
114+
"resolving original promise",
115+
"resolved data",
116+
"resolving awaiting promise",
117+
"abc return data",
118+
]);
119+
}
120+
);
121+
122+
test.each(["async function abc() {", "const abc = async () => {"])(
123+
"can have multiple awaits in async function (%p)",
124+
functionHeader => {
125+
util.testFunction`
99126
const { promise: promise1, resolve: resolve1 } = defer<string>();
100127
const { promise: promise2, resolve: resolve2 } = defer<string>();
101128
const { promise: promise3, resolve: resolve3 } = defer<string>();
102129
promise1.then(data => log("resolving promise1", data));
103130
promise2.then(data => log("resolving promise2", data));
104131
promise3.then(data => log("resolving promise3", data));
105132
106-
async function abc() {
133+
${functionHeader}
107134
const result1 = await promise1;
108135
const result2 = await promise2;
109136
const result3 = await promise3;
@@ -120,17 +147,31 @@ test("can have multiple awaits in async function", () => {
120147
return allLogs;
121148
122149
`
123-
.setTsHeader(promiseTestLib)
124-
.expectToEqual([
125-
"resolving promise1",
126-
"data1",
127-
"resolving promise2",
128-
"data2",
129-
"resolving promise3",
130-
"data3",
131-
"resolving awaiting promise",
132-
["data1", "data2", "data3"],
133-
]);
150+
.setTsHeader(promiseTestLib)
151+
.expectToEqual([
152+
"resolving promise1",
153+
"data1",
154+
"resolving promise2",
155+
"data2",
156+
"resolving promise3",
157+
"data3",
158+
"resolving awaiting promise",
159+
["data1", "data2", "data3"],
160+
]);
161+
}
162+
);
163+
164+
test("can make async lambdas with expression body", () => {
165+
util.testFunction`
166+
const foo = async () => "foo";
167+
const bar = async () => await foo();
168+
169+
const { state, value } = bar() as any;
170+
return { state, value };
171+
`.expectToEqual({
172+
state: 1, // __TS__PromiseState.Fulfilled
173+
value: "foo",
174+
});
134175
});
135176

136177
test("can await async function from async function", () => {
@@ -225,6 +266,52 @@ test("can call async function at top-level", () => {
225266
});
226267
});
227268

269+
test("async function throws error", () => {
270+
util.testFunction`
271+
async function a() {
272+
throw "test throw";
273+
}
274+
275+
const { state, rejectionReason } = a() as any;
276+
return { state, rejectionReason };
277+
`.expectToEqual({
278+
state: 2, // __TS__PromiseState.Rejected
279+
rejectionReason: "test throw",
280+
});
281+
});
282+
283+
test("async lambda throws error", () => {
284+
util.testFunction`
285+
const a = async () => {
286+
throw "test throw";
287+
}
288+
289+
const { state, rejectionReason } = a() as any;
290+
return { state, rejectionReason };
291+
`.expectToEqual({
292+
state: 2, // __TS__PromiseState.Rejected
293+
rejectionReason: "test throw",
294+
});
295+
});
296+
297+
test("async function throws object", () => {
298+
util.testFunction`
299+
async function a() {
300+
throw new Error("test throw");
301+
}
302+
303+
const { state, rejectionReason } = a() as any;
304+
return { state, rejectionReason };
305+
`.expectToEqual({
306+
state: 2, // __TS__PromiseState.Rejected
307+
rejectionReason: {
308+
message: "test throw",
309+
name: "Error",
310+
stack: expect.stringContaining("stack traceback"),
311+
},
312+
});
313+
});
314+
228315
test.each([
229316
"await a();",
230317
"const b = await a();",

0 commit comments

Comments
 (0)