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 22 matching lines...) Expand all Loading... |
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) { | 42 if (Invoker.current == null) { |
43 throw new StateError("extend() may only be called within a test."); | 43 throw new StateError("expect() may only be called within a test."); |
44 } | 44 } |
45 | 45 |
46 if (Invoker.current.closed) throw new ClosedException(); | 46 if (Invoker.current.closed) throw new ClosedException(); |
47 | 47 |
48 matcher = wrapMatcher(matcher); | 48 matcher = wrapMatcher(matcher); |
49 var matchState = {}; | 49 var matchState = {}; |
50 try { | 50 try { |
51 if (matcher.matches(actual, matchState)) return; | 51 if (matcher.matches(actual, matchState)) return; |
52 } catch (e, trace) { | 52 } catch (e, trace) { |
53 if (reason == null) { | 53 if (reason == null) { |
(...skipping 17 matching lines...) Expand all Loading... |
71 | 71 |
72 var mismatchDescription = new StringDescription(); | 72 var mismatchDescription = new StringDescription(); |
73 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); | 73 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); |
74 | 74 |
75 if (mismatchDescription.length > 0) { | 75 if (mismatchDescription.length > 0) { |
76 description.add(' Which: ${mismatchDescription}\n'); | 76 description.add(' Which: ${mismatchDescription}\n'); |
77 } | 77 } |
78 if (reason != null) description.add(reason).add('\n'); | 78 if (reason != null) description.add(reason).add('\n'); |
79 return description.toString(); | 79 return description.toString(); |
80 } | 80 } |
OLD | NEW |