OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library unittest.throws_matcher; | 5 library unittest.throws_matcher; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:matcher/matcher.dart' hide fail, expect; | 9 import 'package:matcher/matcher.dart' hide fail, expect; |
10 | 10 |
11 import '../unittest.dart'; | 11 import 'expect.dart'; |
| 12 import 'invoker.dart'; |
| 13 import 'utils.dart'; |
12 | 14 |
13 /// This can be used to match two kinds of objects: | 15 /// This can be used to match two kinds of objects: |
14 /// | 16 /// |
15 /// * A [Function] that throws an exception when called. The function cannot | 17 /// * A [Function] that throws an exception when called. The function cannot |
16 /// take any arguments. If you want to test that a function expecting | 18 /// take any arguments. If you want to test that a function expecting |
17 /// arguments throws, wrap it in another zero-argument function that calls | 19 /// arguments throws, wrap it in another zero-argument function that calls |
18 /// the one you want to test. | 20 /// the one you want to test. |
19 /// | 21 /// |
20 /// * A [Future] that completes with an exception. Note that this creates an | 22 /// * A [Future] that completes with an exception. Note that this creates an |
21 /// asynchronous expectation. The call to `expect()` that includes this will | 23 /// asynchronous expectation. The call to `expect()` that includes this will |
(...skipping 19 matching lines...) Expand all Loading... |
41 Matcher throwsA(matcher) => new Throws(wrapMatcher(matcher)); | 43 Matcher throwsA(matcher) => new Throws(wrapMatcher(matcher)); |
42 | 44 |
43 class Throws extends Matcher { | 45 class Throws extends Matcher { |
44 final Matcher _matcher; | 46 final Matcher _matcher; |
45 | 47 |
46 const Throws([Matcher matcher]) : this._matcher = matcher; | 48 const Throws([Matcher matcher]) : this._matcher = matcher; |
47 | 49 |
48 bool matches(item, Map matchState) { | 50 bool matches(item, Map matchState) { |
49 if (item is! Function && item is! Future) return false; | 51 if (item is! Function && item is! Future) return false; |
50 if (item is Future) { | 52 if (item is Future) { |
51 var done = expectAsync((fn) => fn()); | 53 Invoker.current.addOutstandingCallback(); |
52 | |
53 // Queue up an asynchronous expectation that validates when the future | 54 // Queue up an asynchronous expectation that validates when the future |
54 // completes. | 55 // completes. |
55 item.then((value) { | 56 item.then((value) { |
56 done(() { | 57 fail("Expected future to fail, but succeeded with '$value'."); |
57 fail("Expected future to fail, but succeeded with '$value'."); | |
58 }); | |
59 }, onError: (error, trace) { | 58 }, onError: (error, trace) { |
60 done(() { | 59 if (_matcher == null) return; |
61 if (_matcher == null) return; | 60 |
62 var reason; | 61 var reason; |
63 if (trace != null) { | 62 if (trace != null) { |
64 var stackTrace = trace.toString(); | 63 var stackTrace = terseChain(trace).toString(); |
65 stackTrace = " ${stackTrace.replaceAll("\n", "\n ")}"; | 64 stackTrace = " ${stackTrace.replaceAll("\n", "\n ")}"; |
66 reason = "Actual exception trace:\n$stackTrace"; | 65 reason = "Actual exception trace:\n$stackTrace"; |
67 } | 66 } |
68 expect(error, _matcher, reason: reason); | 67 |
69 }); | 68 // Re-run [expect] to get the proper formatting. |
70 }); | 69 expect(() => throw error, this, reason: reason); |
| 70 }).then((_) => Invoker.current.removeOutstandingCallback()); |
71 // It hasn't failed yet. | 71 // It hasn't failed yet. |
72 return true; | 72 return true; |
73 } | 73 } |
74 | 74 |
75 try { | 75 try { |
76 item(); | 76 item(); |
77 return false; | 77 return false; |
78 } catch (e, s) { | 78 } catch (e, s) { |
79 if (_matcher == null || _matcher.matches(e, matchState)) { | 79 if (_matcher == null || _matcher.matches(e, matchState)) { |
80 return true; | 80 return true; |
(...skipping 22 matching lines...) Expand all Loading... |
103 mismatchDescription | 103 mismatchDescription |
104 .add('threw ') | 104 .add('threw ') |
105 .addDescriptionOf(matchState['exception']); | 105 .addDescriptionOf(matchState['exception']); |
106 if (verbose) { | 106 if (verbose) { |
107 mismatchDescription.add(' at ').add(matchState['stack'].toString()); | 107 mismatchDescription.add(' at ').add(matchState['stack'].toString()); |
108 } | 108 } |
109 return mismatchDescription; | 109 return mismatchDescription; |
110 } | 110 } |
111 } | 111 } |
112 } | 112 } |
OLD | NEW |