OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --harmony-async-await --allow-natives-syntax |
| 6 |
| 7 function assertEqualsAsync(expected, run, msg) { |
| 8 var actual; |
| 9 var hadValue = false; |
| 10 var hadError = false; |
| 11 var promise = run(); |
| 12 |
| 13 if (typeof promise !== "object" || typeof promise.then !== "function") { |
| 14 throw new MjsUnitAssertionError( |
| 15 "Expected " + run.toString() + |
| 16 " to return a Promise, but it returned " + PrettyPrint(promise)); |
| 17 } |
| 18 |
| 19 promise.then(function(value) { hadValue = true; actual = value; }, |
| 20 function(error) { hadError = true; actual = error; }); |
| 21 |
| 22 assertFalse(hadValue || hadError); |
| 23 |
| 24 %RunMicrotasks(); |
| 25 |
| 26 if (hadError) throw actual; |
| 27 |
| 28 assertTrue( |
| 29 hadValue, "Expected '" + run.toString() + "' to produce a value"); |
| 30 |
| 31 assertEquals(expected, actual, msg); |
| 32 }; |
| 33 |
| 34 function getStack(error) { |
| 35 var stack = error.stack.split('\n'). |
| 36 filter(function(line) { |
| 37 return /^\s*at [a-zA-Z0-9_]/.test(line); |
| 38 }). |
| 39 map(line => line.replace(/^\s*at ([a-zA-Z0-9_\.\[\]]+).*/, "$1")); |
| 40 |
| 41 // remove `Promise.then()` invocation by assertEqualsAsync() |
| 42 if (stack[2] === "assertEqualsAsync") return []; |
| 43 |
| 44 return stack.reverse(); |
| 45 } |
| 46 |
| 47 var log = []; |
| 48 class FakePromise extends Promise { |
| 49 constructor(executor) { |
| 50 var stack = getStack(new Error("Getting Callstack")); |
| 51 if (stack.length) { |
| 52 while (stack[0] === "assertEqualsAsync") stack.shift(); |
| 53 log.push("@@Species: [" + stack.join(" > ") + "]"); |
| 54 } |
| 55 return new Promise(executor); |
| 56 } |
| 57 }; |
| 58 |
| 59 Object.defineProperty(Promise, Symbol.species, { |
| 60 value: FakePromise, |
| 61 configurable: true, |
| 62 enumerable: false, |
| 63 writable: false |
| 64 }); |
| 65 |
| 66 // Internal `AsyncFunctionAwait` only --- no @@species invocations. |
| 67 async function test() { return await "foo"; } |
| 68 assertEqualsAsync("foo", function testInternalOnly() { return test(); }, |
| 69 "should not call Promise[@@Species]"); |
| 70 assertEquals([], log); |
| 71 |
| 72 log.length = 0; |
| 73 assertEqualsAsync( |
| 74 "foo", |
| 75 function testThenOnReturnedPromise() { |
| 76 return test().then(x => (log.push("Then: " + x), x)); |
| 77 }, |
| 78 "should call Promise[@@Species] after non-internal Then"); |
| 79 assertEquals([ |
| 80 "@@Species: [testThenOnReturnedPromise > Promise.then > FakePromise]", |
| 81 "Then: foo" |
| 82 ], log); |
OLD | NEW |