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_set_matcher; | |
6 | |
7 import 'package:matcher/matcher.dart'; | |
8 | |
9 import 'action.dart'; | |
10 import 'log_entry.dart'; | |
11 | |
12 /** Special values for use with [_ResultSetMatcher] [frequency]. */ | |
13 class _Frequency { | |
14 /** Every call/throw must match */ | |
15 static const ALL = const _Frequency._('ALL'); | |
16 | |
17 /** At least one call/throw must match. */ | |
18 static const SOME = const _Frequency._('SOME'); | |
19 | |
20 /** No calls/throws should match. */ | |
21 static const NONE = const _Frequency._('NONE'); | |
22 | |
23 const _Frequency._(this.name); | |
24 | |
25 final String name; | |
26 } | |
27 | |
28 /** | |
29 * [_ResultSetMatcher]s are used to make assertions about the results | |
30 * of method calls. When filtering an execution log by calling | |
31 * [getLogs], a [LogEntrySet] of matching call logs is returned; | |
32 * [_ResultSetMatcher]s can then assert various things about this | |
33 * (sub)set of logs. | |
34 * | |
35 * We could make this class use _ResultMatcher but it doesn't buy that | |
36 * match and adds some perf hit, so there is some duplication here. | |
37 */ | |
38 class _ResultSetMatcher extends Matcher { | |
39 final Action action; | |
40 final Matcher value; | |
41 final _Frequency frequency; // ALL, SOME, or NONE. | |
42 | |
43 const _ResultSetMatcher(this.action, this.value, this.frequency); | |
44 | |
45 bool matches(logList, Map matchState) { | |
46 for (LogEntry entry in logList) { | |
47 // normalize the action; PROXY is like RETURN. | |
48 Action eaction = entry.action; | |
49 if (eaction == Action.PROXY) { | |
50 eaction = Action.RETURN; | |
51 } | |
52 if (eaction == action && value.matches(entry.value, matchState)) { | |
53 if (frequency == _Frequency.NONE) { | |
54 addStateInfo(matchState, {'entry': entry}); | |
55 return false; | |
56 } else if (frequency == _Frequency.SOME) { | |
57 return true; | |
58 } | |
59 } else { | |
60 // Mismatch. | |
61 if (frequency == _Frequency.ALL) { // We need just one mismatch to fail. | |
62 addStateInfo(matchState, {'entry': entry}); | |
63 return false; | |
64 } | |
65 } | |
66 } | |
67 // If we get here, then if count is _ALL we got all matches and | |
68 // this is success; otherwise we got all mismatched which is | |
69 // success for count == _NONE and failure for count == _SOME. | |
70 return (frequency != _Frequency.SOME); | |
71 } | |
72 | |
73 Description describe(Description description) { | |
74 description.add(' to '); | |
75 description.add(frequency == _Frequency.ALL ? 'alway ' : | |
76 (frequency == _Frequency.NONE ? 'never ' : 'sometimes ')); | |
77 if (action == Action.RETURN || action == Action.PROXY) | |
78 description.add('return '); | |
79 else | |
80 description.add('throw '); | |
81 return description.addDescriptionOf(value); | |
82 } | |
83 | |
84 Description describeMismatch(logList, Description mismatchDescription, | |
85 Map matchState, bool verbose) { | |
86 if (frequency != _Frequency.SOME) { | |
87 LogEntry entry = matchState['entry']; | |
88 if (entry.action == Action.RETURN || entry.action == Action.PROXY) { | |
89 mismatchDescription.add('returned'); | |
90 } else { | |
91 mismatchDescription.add('threw'); | |
92 } | |
93 mismatchDescription.add(' value that '); | |
94 value.describeMismatch(entry.value, mismatchDescription, | |
95 matchState['state'], verbose); | |
96 mismatchDescription.add(' at least once'); | |
97 } else { | |
98 mismatchDescription.add('never did'); | |
99 } | |
100 return mismatchDescription; | |
101 } | |
102 } | |
103 | |
104 /** | |
105 *[alwaysReturned] asserts that all matching calls to a method returned | |
106 * a value that matched [value]. | |
107 */ | |
108 Matcher alwaysReturned(value) => | |
109 new _ResultSetMatcher(Action.RETURN, wrapMatcher(value), _Frequency.ALL); | |
110 | |
111 /** | |
112 *[sometimeReturned] asserts that at least one matching call to a method | |
113 * returned a value that matched [value]. | |
114 */ | |
115 Matcher sometimeReturned(value) => | |
116 new _ResultSetMatcher(Action.RETURN, wrapMatcher(value), _Frequency.SOME); | |
117 | |
118 /** | |
119 *[neverReturned] asserts that no matching calls to a method returned | |
120 * a value that matched [value]. | |
121 */ | |
122 Matcher neverReturned(value) => | |
123 new _ResultSetMatcher(Action.RETURN, wrapMatcher(value), _Frequency.NONE); | |
124 | |
125 /** | |
126 *[alwaysThrew] asserts that all matching calls to a method threw | |
127 * a value that matched [value]. | |
128 */ | |
129 Matcher alwaysThrew(value) => | |
130 new _ResultSetMatcher(Action.THROW, wrapMatcher(value), _Frequency.ALL); | |
131 | |
132 /** | |
133 *[sometimeThrew] asserts that at least one matching call to a method threw | |
134 * a value that matched [value]. | |
135 */ | |
136 Matcher sometimeThrew(value) => | |
137 new _ResultSetMatcher(Action.THROW, wrapMatcher(value), _Frequency.SOME); | |
138 | |
139 /** | |
140 *[neverThrew] asserts that no matching call to a method threw | |
141 * a value that matched [value]. | |
142 */ | |
143 Matcher neverThrew(value) => | |
144 new _ResultSetMatcher(Action.THROW, wrapMatcher(value), _Frequency.NONE); | |
OLD | NEW |