| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library unittest.throws_matcher; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:matcher/matcher.dart' hide fail, expect; | |
| 10 | |
| 11 import 'expect.dart'; | |
| 12 import 'invoker.dart'; | |
| 13 import 'utils.dart'; | |
| 14 | |
| 15 /// This can be used to match two kinds of objects: | |
| 16 /// | |
| 17 /// * A [Function] that throws an exception when called. The function cannot | |
| 18 /// take any arguments. If you want to test that a function expecting | |
| 19 /// arguments throws, wrap it in another zero-argument function that calls | |
| 20 /// the one you want to test. | |
| 21 /// | |
| 22 /// * A [Future] that completes with an exception. Note that this creates an | |
| 23 /// asynchronous expectation. The call to `expect()` that includes this will | |
| 24 /// return immediately and execution will continue. Later, when the future | |
| 25 /// completes, the actual expectation will run. | |
| 26 const Matcher throws = const Throws(); | |
| 27 | |
| 28 /// This can be used to match two kinds of objects: | |
| 29 /// | |
| 30 /// * A [Function] that throws an exception when called. The function cannot | |
| 31 /// take any arguments. If you want to test that a function expecting | |
| 32 /// arguments throws, wrap it in another zero-argument function that calls | |
| 33 /// the one you want to test. | |
| 34 /// | |
| 35 /// * A [Future] that completes with an exception. Note that this creates an | |
| 36 /// asynchronous expectation. The call to `expect()` that includes this will | |
| 37 /// return immediately and execution will continue. Later, when the future | |
| 38 /// completes, the actual expectation will run. | |
| 39 /// | |
| 40 /// In both cases, when an exception is thrown, this will test that the | |
| 41 /// exception object matches [matcher]. If [matcher] is not an instance of | |
| 42 /// [Matcher], it will implicitly be treated as `equals(matcher)`. | |
| 43 Matcher throwsA(matcher) => new Throws(wrapMatcher(matcher)); | |
| 44 | |
| 45 class Throws extends Matcher { | |
| 46 final Matcher _matcher; | |
| 47 | |
| 48 const Throws([Matcher matcher]) : this._matcher = matcher; | |
| 49 | |
| 50 bool matches(item, Map matchState) { | |
| 51 if (item is! Function && item is! Future) return false; | |
| 52 if (item is Future) { | |
| 53 Invoker.current.addOutstandingCallback(); | |
| 54 // Queue up an asynchronous expectation that validates when the future | |
| 55 // completes. | |
| 56 item.then((value) { | |
| 57 fail("Expected future to fail, but succeeded with '$value'."); | |
| 58 }, onError: (error, trace) { | |
| 59 if (_matcher == null) return; | |
| 60 | |
| 61 var reason; | |
| 62 if (trace != null) { | |
| 63 var stackTrace = terseChain(trace).toString(); | |
| 64 stackTrace = " ${stackTrace.replaceAll("\n", "\n ")}"; | |
| 65 reason = "Actual exception trace:\n$stackTrace"; | |
| 66 } | |
| 67 | |
| 68 // Re-run [expect] to get the proper formatting. | |
| 69 expect(() => throw error, this, reason: reason); | |
| 70 }).then((_) => Invoker.current.removeOutstandingCallback()); | |
| 71 // It hasn't failed yet. | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 try { | |
| 76 item(); | |
| 77 return false; | |
| 78 } catch (e, s) { | |
| 79 if (_matcher == null || _matcher.matches(e, matchState)) { | |
| 80 return true; | |
| 81 } else { | |
| 82 addStateInfo(matchState, {'exception': e, 'stack': s}); | |
| 83 return false; | |
| 84 } | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 Description describe(Description description) { | |
| 89 if (_matcher == null) { | |
| 90 return description.add("throws"); | |
| 91 } else { | |
| 92 return description.add('throws ').addDescriptionOf(_matcher); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 Description describeMismatch( | |
| 97 item, Description mismatchDescription, Map matchState, bool verbose) { | |
| 98 if (item is! Function && item is! Future) { | |
| 99 return mismatchDescription.add('is not a Function or Future'); | |
| 100 } else if (_matcher == null || matchState['exception'] == null) { | |
| 101 return mismatchDescription.add('did not throw'); | |
| 102 } else { | |
| 103 mismatchDescription | |
| 104 .add('threw ') | |
| 105 .addDescriptionOf(matchState['exception']); | |
| 106 if (verbose) { | |
| 107 mismatchDescription.add(' at ').add(matchState['stack'].toString()); | |
| 108 } | |
| 109 return mismatchDescription; | |
| 110 } | |
| 111 } | |
| 112 } | |
| OLD | NEW |