OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <script src="../../resources/js-test.js"></script> |
| 3 <div id="description"></div> |
| 4 <div id="console"></div> |
| 5 <script> |
| 6 // http://heycam.github.io/webidl/#es-operations |
| 7 description('Operations that return a Promise type should handle exceptions ' + |
| 8 'by returning a rejected Promise with the exception.') |
| 9 |
| 10 window.jsTestIsAsync = true; |
| 11 |
| 12 var reason; |
| 13 |
| 14 function shouldBeRejected(promise, message) { |
| 15 return promise.then(function() { |
| 16 testFailed('Resolved unexpectedly: ' + message); |
| 17 }, function(e) { |
| 18 reason = e; |
| 19 testPassed('Rejected as expected: ' + message); |
| 20 shouldBeTrue('reason instanceof Error'); |
| 21 debug(e); |
| 22 }); |
| 23 } |
| 24 |
| 25 function shouldBeResolved(promise, message) { |
| 26 return promise.then(function() { |
| 27 testPassed('Resolved as expected: ' + message); |
| 28 }, function(e) { |
| 29 testFailed('Rejected unexpectedly: ' + message); |
| 30 reason = e; |
| 31 shouldBeTrue('reason instanceof Error'); |
| 32 debug(e); |
| 33 }); |
| 34 } |
| 35 |
| 36 var check = internals.promiseCheck.bind(internals); |
| 37 var check2 = internals.promiseCheckWithoutExceptionState.bind(internals); |
| 38 var check3 = internals.promiseCheckRange.bind(internals); |
| 39 |
| 40 Promise.resolve().then(function() { |
| 41 return shouldBeRejected(check(), 'no arguments'); |
| 42 }).then(function() { |
| 43 return shouldBeResolved(check(3, true, {}, '', ['']), 'valid arguments'); |
| 44 }).then(function() { |
| 45 return shouldBeResolved(check(null, true, {}, '', []), 'convert(long)'); |
| 46 }).then(function() { |
| 47 return shouldBeResolved(check(3, {}, {}, '', []), 'convert(boolean)'); |
| 48 }).then(function() { |
| 49 return shouldBeRejected(check(3, true, 3, '', []), 'type error(Dictionary)')
; |
| 50 }).then(function() { |
| 51 return shouldBeResolved(check(3, true, {}, {}, []), 'convert(String)'); |
| 52 }).then(function() { |
| 53 var x = { |
| 54 toString: function() { |
| 55 throw Error('Thrown from toString'); |
| 56 } |
| 57 }; |
| 58 return shouldBeRejected(check(3, true, {}, x, []), 'conversion error(toStrin
g)'); |
| 59 }).then(function() { |
| 60 var x = { |
| 61 toString: function() { |
| 62 throw Error('Thrown from toString'); |
| 63 } |
| 64 }; |
| 65 return shouldBeRejected(check(3, true, {}, '', [x]), 'conversion error([Stri
ng])'); |
| 66 }).then(function() { |
| 67 return shouldBeRejected(check(3, false, {}, '', []), 'rejected by the impl')
; |
| 68 }).then(function() { |
| 69 return shouldBeRejected(check2(), 'no arguments'); |
| 70 }).then(function() { |
| 71 return shouldBeResolved(check2({}, '', '', ''), 'valid arguments'); |
| 72 }).then(function() { |
| 73 return shouldBeResolved(check2({}, ''), 'valid arguments'); |
| 74 }).then(function() { |
| 75 return shouldBeRejected(check2(3, ''), 'type error(Dictionary)'); |
| 76 }).then(function() { |
| 77 return shouldBeResolved(check2({}, '', {}, 3), 'convert(String...)'); |
| 78 }).then(function() { |
| 79 var x = { |
| 80 toString: function() { |
| 81 throw Error('Thrown from toString'); |
| 82 } |
| 83 }; |
| 84 return shouldBeRejected(check2({}, '', '', x), 'conversion error(String...)'
); |
| 85 }).then(function() { |
| 86 return shouldBeRejected(check3(-1), 'range error(octet)'); |
| 87 }).then(undefined, function(e) { |
| 88 testFailed('An exception is thrown from a method'); |
| 89 debug(e); |
| 90 }).then(finishJSTest, finishJSTest); |
| 91 </script> |
OLD | NEW |