OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 test.frontend.expect; | 5 library test.frontend.expect; |
6 | 6 |
7 import 'package:matcher/matcher.dart'; | 7 import 'package:matcher/matcher.dart'; |
8 | 8 |
9 import '../backend/closed_exception.dart'; | 9 import '../backend/closed_exception.dart'; |
10 import '../backend/invoker.dart'; | 10 import '../backend/invoker.dart'; |
(...skipping 21 matching lines...) Expand all Loading... |
32 /// [matcher] can be a value in which case it will be wrapped in an | 32 /// [matcher] can be a value in which case it will be wrapped in an |
33 /// [equals] matcher. | 33 /// [equals] matcher. |
34 /// | 34 /// |
35 /// If the assertion fails a [TestFailure] is thrown. | 35 /// If the assertion fails a [TestFailure] is thrown. |
36 /// | 36 /// |
37 /// In some cases extra diagnostic info can be produced on failure (for | 37 /// In some cases extra diagnostic info can be produced on failure (for |
38 /// example, stack traces on mismatched exceptions). To enable these, | 38 /// example, stack traces on mismatched exceptions). To enable these, |
39 /// [verbose] should be specified as `true`. | 39 /// [verbose] should be specified as `true`. |
40 void expect(actual, matcher, | 40 void expect(actual, matcher, |
41 {String reason, bool verbose: false, ErrorFormatter formatter}) { | 41 {String reason, bool verbose: false, ErrorFormatter formatter}) { |
| 42 if (Invoker.current == null) { |
| 43 throw new StateError("extend() may only be called within a test."); |
| 44 } |
| 45 |
42 if (Invoker.current.closed) throw new ClosedException(); | 46 if (Invoker.current.closed) throw new ClosedException(); |
43 | 47 |
44 matcher = wrapMatcher(matcher); | 48 matcher = wrapMatcher(matcher); |
45 var matchState = {}; | 49 var matchState = {}; |
46 try { | 50 try { |
47 if (matcher.matches(actual, matchState)) return; | 51 if (matcher.matches(actual, matchState)) return; |
48 } catch (e, trace) { | 52 } catch (e, trace) { |
49 if (reason == null) { | 53 if (reason == null) { |
50 reason = '${(e is String) ? e : e.toString()} at $trace'; | 54 reason = '${(e is String) ? e : e.toString()} at $trace'; |
51 } | 55 } |
(...skipping 15 matching lines...) Expand all Loading... |
67 | 71 |
68 var mismatchDescription = new StringDescription(); | 72 var mismatchDescription = new StringDescription(); |
69 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); | 73 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); |
70 | 74 |
71 if (mismatchDescription.length > 0) { | 75 if (mismatchDescription.length > 0) { |
72 description.add(' Which: ${mismatchDescription}\n'); | 76 description.add(' Which: ${mismatchDescription}\n'); |
73 } | 77 } |
74 if (reason != null) description.add(reason).add('\n'); | 78 if (reason != null) description.add(reason).add('\n'); |
75 return description.toString(); | 79 return description.toString(); |
76 } | 80 } |
OLD | NEW |