Index: test/mjsunit/harmony/async-await-basic.js |
diff --git a/test/mjsunit/harmony/async-await-basic.js b/test/mjsunit/harmony/async-await-basic.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a5fb86833ad6f9c8c23387a11030d693a7391a79 |
--- /dev/null |
+++ b/test/mjsunit/harmony/async-await-basic.js |
@@ -0,0 +1,153 @@ |
+// Copyright 2016 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Flags: --harmony-async-await |
+ |
+function assertEqualsAsync(expected, run, msg) { |
+ var actual; |
+ var hadValue = false; |
+ var hadError = false; |
+ run().then(function(value) { hadValue = true; actual = value; }, |
+ function(error) { hadError = true; actual = error; }); |
+ flushMicrotasks(); |
+ |
+ if (hadError) |
+ throw actual; |
+ assertTrue( |
+ hadValue, "Expected '" + run.toString() + "' to produce a value"); |
+ |
+ assertEquals(expected, actual, msg); |
+} |
+ |
+function assertThrowsAsync(run, errorType, message) { |
+ let actual; |
+ var hadError = false; |
+ run().then(function(value) { actual = value; }, |
+ function(error) { hadError = true; actual = error; }); |
+ flushMicrotasks(); |
+ |
+ if (!hadError) |
+ throw new Error( |
+ "Expected " + run + "() to throw " + errorType.name + |
+ ", but did not throw."); |
+ if (!(actual instanceof errorType)) |
+ throw new Error( |
+ "Expected " + run + "() to throw " + errorType.name + |
+ ", but threw '" + actual + "'"); |
+ if (message !== void 0 && actual.message !== message) |
+ throw new Error( |
+ "Expected " + run + "() to throw '" + message + "', but threw '" + |
+ actual.message + "'"); |
+} |
+ |
+// TODO(caitp): give async functions a non-constructor map |
+ |
+// Let F be ! FunctionAllocate(functionPrototype, Strict, "non-constructor") |
+//async function asyncNonConstructorDecl() {} |
+//assertThrows( |
+// () => new asyncNonConstructorDecl(), TypeError); |
+//assertThrows( |
+// () => new (async function() {}), TypeError); |
+//assertThrows( |
+// () => new ({ async nonConstructor() {} }).nonConstructor(), TypeError); |
+//assertThrows( |
+// () => new (() => "not a constructor!"), TypeError); |
+ |
+// Normal completion |
+async function asyncDecl() { return "test"; } |
+assertEqualsAsync("test", asyncDecl); |
+assertEqualsAsync("test2", async function() { return "test2"; }); |
+ |
+// TODO(caitp): generate proper code for async arrows |
+ |
+// assertEqualsAsync("test3", async () => "test3"); |
+// assertEqualsAsync("test4", () => ({ async f() { return "test4"; } }).f()); |
+ |
+class MyError extends Error {}; |
+ |
+// TODO(caitp): make catch handler lookup work correctly |
+ |
+// Throw completion |
+// async function asyncDeclThrower(e) { throw new MyError(e); } |
+//assertThrowsAsync( |
+// () => asyncDeclThrower("boom!"), MyError, "boom!"); |
+//assertThrowsAsync( |
+// () => (async function(e) { throw new MyError(e); })("boom!!!"), |
+// MyError, "boom!!!"); |
+//assertThrowsAsync( |
+// () => (async e => { throw new MyError(e) })("boom!!"), MyError, "boom!!"); |
+//assertThrowsAsync( |
+// () => ({ async thrower(e) { throw new MyError(e); } }).thrower("boom!1!"), |
+// MyError, "boom!1!"); |
+ |
+function resolveLater(value) { return Promise.resolve(value); } |
+function rejectLater(error) { return Promise.reject(error); } |
+ |
+// Resume after Normal completion |
+var log = []; |
+async function resumeAfterNormal(value) { |
+ log.push("start:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ return value + 1; |
+} |
+ |
+assertEqualsAsync(4, () => resumeAfterNormal(1)); |
+assertEquals("start:1 resume:2 resume:3", log.join(" ")); |
+ |
+var O = { |
+ async resumeAfterNormal(value) { |
+ log.push("start:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ return value + 1; |
+ } |
+}; |
+log = []; |
+assertEqualsAsync(5, () => O.resumeAfterNormal(2)); |
+assertEquals("start:2 resume:3 resume:4", log.join(" ")); |
+ |
+// Resume after Throw completion |
+async function resumeAfterThrow(value) { |
+ log.push("start:" + value); |
+ try { |
+ value = await rejectLater("throw1"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ try { |
+ value = await rejectLater("throw2"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ return value + 1; |
+} |
+ |
+log = []; |
+assertEqualsAsync(2, () => resumeAfterThrow(1)); |
+assertEquals("start:1 resume:throw1 resume:throw2", log.join(" ")); |
+ |
+var O = { |
+ async resumeAfterThrow(value) { |
+ log.push("start:" + value); |
+ try { |
+ value = await rejectLater("throw1"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ try { |
+ value = await rejectLater("throw2"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ return value + 1; |
+ } |
+} |
+log = []; |
+assertEqualsAsync(3, () => O.resumeAfterThrow(2)); |
+assertEquals("start:2 resume:throw1 resume:throw2", log.join(" ")); |