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 mock.result_matcher; | |
6 | |
7 import 'package:matcher/matcher.dart'; | |
8 | |
9 import 'action.dart'; | |
10 import 'log_entry.dart'; | |
11 | |
12 /** | |
13 * [_ResultMatcher]s are used to make assertions about the results | |
14 * of method calls. These can be used as optional parameters to [getLogs]. | |
15 */ | |
16 class _ResultMatcher extends Matcher { | |
17 final Action action; | |
18 final Matcher value; | |
19 | |
20 const _ResultMatcher(this.action, this.value); | |
21 | |
22 bool matches(item, Map matchState) { | |
23 if (item is! LogEntry) { | |
24 return false; | |
25 } | |
26 // normalize the action; _PROXY is like _RETURN. | |
27 Action eaction = item.action; | |
28 if (eaction == Action.PROXY) { | |
29 eaction = Action.RETURN; | |
30 } | |
31 return (eaction == action && value.matches(item.value, matchState)); | |
32 } | |
33 | |
34 Description describe(Description description) { | |
35 description.add(' to '); | |
36 if (action == Action.RETURN || action == Action.PROXY) { | |
37 description.add('return '); | |
38 } else { | |
39 description.add('throw '); | |
40 } | |
41 return description.addDescriptionOf(value); | |
42 } | |
43 | |
44 Description describeMismatch(item, Description mismatchDescription, | |
45 Map matchState, bool verbose) { | |
46 if (item.action == Action.RETURN || item.action == Action.PROXY) { | |
47 mismatchDescription.add('returned '); | |
48 } else { | |
49 mismatchDescription.add('threw '); | |
50 } | |
51 mismatchDescription.add(item.value); | |
52 return mismatchDescription; | |
53 } | |
54 } | |
55 | |
56 /** | |
57 *[returning] matches log entries where the call to a method returned | |
58 * a value that matched [value]. | |
59 */ | |
60 Matcher returning(value) => | |
61 new _ResultMatcher(Action.RETURN, wrapMatcher(value)); | |
62 | |
63 /** | |
64 *[throwing] matches log entrues where the call to a method threw | |
65 * a value that matched [value]. | |
66 */ | |
67 Matcher throwing(value) => | |
68 new _ResultMatcher(Action.THROW, wrapMatcher(value)); | |
OLD | NEW |