OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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: --allow-natives-syntax |
| 6 |
| 7 function assertThrowsAsync(run, errorType, message) { |
| 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) { |
| 27 throw new MjsUnitAssertionError( |
| 28 "Expected " + run + "() to throw " + errorType.name + |
| 29 ", but did not throw."); |
| 30 } |
| 31 if (!(actual instanceof errorType)) |
| 32 throw new MjsUnitAssertionError( |
| 33 "Expected " + run + "() to throw " + errorType.name + |
| 34 ", but threw '" + actual + "'"); |
| 35 if (message !== void 0 && actual.message !== message) |
| 36 throw new MjsUnitAssertionError( |
| 37 "Expected " + run + "() to throw '" + message + "', but threw '" + |
| 38 actual.message + "'"); |
| 39 }; |
| 40 |
| 41 function assertEqualsAsync(expected, run, msg) { |
| 42 var actual; |
| 43 var hadValue = false; |
| 44 var hadError = false; |
| 45 var promise = run(); |
| 46 |
| 47 if (typeof promise !== "object" || typeof promise.then !== "function") { |
| 48 throw new MjsUnitAssertionError( |
| 49 "Expected " + run.toString() + |
| 50 " to return a Promise, but it returned " + PrettyPrint(promise)); |
| 51 } |
| 52 |
| 53 promise.then(function(value) { hadValue = true; actual = value; }, |
| 54 function(error) { hadError = true; actual = error; }); |
| 55 |
| 56 assertFalse(hadValue || hadError); |
| 57 |
| 58 %RunMicrotasks(); |
| 59 |
| 60 if (hadError) throw actual; |
| 61 |
| 62 assertTrue( |
| 63 hadValue, "Expected '" + run.toString() + "' to produce a value"); |
| 64 |
| 65 assertEquals(expected, actual, msg); |
| 66 }; |
| 67 |
| 68 function resolveLater(value) { |
| 69 return new Promise(function(resolve) { |
| 70 resolve(value); |
| 71 }); |
| 72 } |
| 73 |
| 74 function rejectLater(reason) { |
| 75 return new Promise(function(resolve, reject) { |
| 76 reject(reason); |
| 77 }); |
| 78 } |
| 79 |
| 80 class MyError extends Error {}; |
| 81 var AsyncFunction = async function() {}.constructor; |
| 82 |
| 83 assertEqualsAsync("finally-return (func-expr)", async function() { |
| 84 try { |
| 85 return "early-return (func-expr)"; |
| 86 } finally { |
| 87 return "finally-return (func-expr)"; |
| 88 } |
| 89 }); |
| 90 |
| 91 assertEqualsAsync("finally-return (arrow)", async() => { |
| 92 try { |
| 93 return "early-return (arrow)"; |
| 94 } finally { |
| 95 return "finally-return (arrow)"; |
| 96 } |
| 97 }); |
| 98 |
| 99 assertEqualsAsync("finally-return (eval)", AsyncFunction(` |
| 100 try { |
| 101 return "early-return (eval)"; |
| 102 } finally { |
| 103 return "finally-return (eval)"; |
| 104 } |
| 105 `)); |
| 106 |
| 107 assertEqualsAsync("await-finally-return (func-expr)", async function() { |
| 108 try { |
| 109 return "early-return (func-expr)"; |
| 110 } finally { |
| 111 return await resolveLater("await-finally-return (func-expr)"); |
| 112 } |
| 113 }); |
| 114 |
| 115 assertEqualsAsync("await-finally-return (arrow)", async() => { |
| 116 try { |
| 117 return "early-return (arrow)"; |
| 118 } finally { |
| 119 return await resolveLater("await-finally-return (arrow)"); |
| 120 } |
| 121 }); |
| 122 |
| 123 assertEqualsAsync("await-finally-return (eval)", AsyncFunction(` |
| 124 try { |
| 125 return "early-return (eval)"; |
| 126 } finally { |
| 127 return await resolveLater("await-finally-return (eval)"); |
| 128 } |
| 129 `)); |
| 130 |
| 131 assertThrowsAsync(async function() { |
| 132 try { |
| 133 return "early-return (func-expr)"; |
| 134 } finally { |
| 135 throw new MyError("finally-throw (func-expr)"); |
| 136 } |
| 137 }, MyError, "finally-throw (func-expr)"); |
| 138 |
| 139 assertThrowsAsync(async() => { |
| 140 try { |
| 141 return "early-return (arrow)"; |
| 142 } finally { |
| 143 throw new MyError("finally-throw (arrow)"); |
| 144 } |
| 145 }, MyError, "finally-throw (arrow)"); |
| 146 |
| 147 assertThrowsAsync(AsyncFunction(` |
| 148 try { |
| 149 return "early-return (eval)"; |
| 150 } finally { |
| 151 throw new MyError("finally-throw (eval)"); |
| 152 } |
| 153 `), MyError, "finally-throw (eval)"); |
| 154 |
| 155 assertThrowsAsync(async function() { |
| 156 try { |
| 157 return "early-return (func-expr)"; |
| 158 } finally { |
| 159 await rejectLater(new MyError("await-finally-throw (func-expr)")); |
| 160 } |
| 161 }, MyError, "await-finally-throw (func-expr)"); |
| 162 |
| 163 assertThrowsAsync(async() => { |
| 164 try { |
| 165 return "early-return (arrow)"; |
| 166 } finally { |
| 167 await rejectLater(new MyError("await-finally-throw (arrow)")); |
| 168 } |
| 169 }, MyError, "await-finally-throw (arrow)"); |
| 170 |
| 171 assertThrowsAsync(AsyncFunction(` |
| 172 try { |
| 173 return "early-return (eval)"; |
| 174 } finally { |
| 175 await rejectLater(new MyError("await-finally-throw (eval)")); |
| 176 } |
| 177 `), MyError, "await-finally-throw (eval)"); |
OLD | NEW |