OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, 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 matcher.operator_matchers; | |
6 | |
7 import 'interfaces.dart'; | |
8 import 'util.dart'; | |
9 | |
10 /// This returns a matcher that inverts [matcher] to its logical negation. | |
11 Matcher isNot(matcher) => new _IsNot(wrapMatcher(matcher)); | |
12 | |
13 class _IsNot extends Matcher { | |
14 final Matcher _matcher; | |
15 | |
16 const _IsNot(this._matcher); | |
17 | |
18 bool matches(item, Map matchState) => !_matcher.matches(item, matchState); | |
19 | |
20 Description describe(Description description) => | |
21 description.add('not ').addDescriptionOf(_matcher); | |
22 } | |
23 | |
24 /// This returns a matcher that matches if all of the matchers passed as | |
25 /// arguments (up to 7) match. | |
26 /// | |
27 /// Instead of passing the matchers separately they can be passed as a single | |
28 /// List argument. Any argument that is not a matcher is implicitly wrapped in a | |
29 /// Matcher to check for equality. | |
30 Matcher allOf(arg0, [arg1, arg2, arg3, arg4, arg5, arg6]) { | |
31 return new _AllOf(_wrapArgs(arg0, arg1, arg2, arg3, arg4, arg5, arg6)); | |
32 } | |
33 | |
34 class _AllOf extends Matcher { | |
35 final List<Matcher> _matchers; | |
36 | |
37 const _AllOf(this._matchers); | |
38 | |
39 bool matches(item, Map matchState) { | |
40 for (var matcher in _matchers) { | |
41 if (!matcher.matches(item, matchState)) { | |
42 addStateInfo(matchState, {'matcher': matcher}); | |
43 return false; | |
44 } | |
45 } | |
46 return true; | |
47 } | |
48 | |
49 Description describeMismatch( | |
50 item, Description mismatchDescription, Map matchState, bool verbose) { | |
51 var matcher = matchState['matcher']; | |
52 matcher.describeMismatch( | |
53 item, mismatchDescription, matchState['state'], verbose); | |
54 return mismatchDescription; | |
55 } | |
56 | |
57 Description describe(Description description) => | |
58 description.addAll('(', ' and ', ')', _matchers); | |
59 } | |
60 | |
61 /// Matches if any of the given matchers evaluate to true. | |
62 /// | |
63 /// The arguments can be a set of matchers as separate parameters | |
64 /// (up to 7), or a List of matchers. | |
65 /// | |
66 /// The matchers are evaluated from left to right using short-circuit | |
67 /// evaluation, so evaluation stops as soon as a matcher returns true. | |
68 /// | |
69 /// Any argument that is not a matcher is implicitly wrapped in a | |
70 /// Matcher to check for equality. | |
71 Matcher anyOf(arg0, [arg1, arg2, arg3, arg4, arg5, arg6]) { | |
72 return new _AnyOf(_wrapArgs(arg0, arg1, arg2, arg3, arg4, arg5, arg6)); | |
73 } | |
74 | |
75 class _AnyOf extends Matcher { | |
76 final List<Matcher> _matchers; | |
77 | |
78 const _AnyOf(this._matchers); | |
79 | |
80 bool matches(item, Map matchState) { | |
81 for (var matcher in _matchers) { | |
82 if (matcher.matches(item, matchState)) { | |
83 return true; | |
84 } | |
85 } | |
86 return false; | |
87 } | |
88 | |
89 Description describe(Description description) => | |
90 description.addAll('(', ' or ', ')', _matchers); | |
91 } | |
92 | |
93 List<Matcher> _wrapArgs(arg0, arg1, arg2, arg3, arg4, arg5, arg6) { | |
94 Iterable<Matcher> matchers; | |
95 if (arg0 is List) { | |
96 if (arg1 != null || | |
97 arg2 != null || | |
98 arg3 != null || | |
99 arg4 != null || | |
100 arg5 != null || | |
101 arg6 != null) { | |
102 throw new ArgumentError('If arg0 is a List, all other arguments must be' | |
103 ' null.'); | |
104 } | |
105 | |
106 matchers = arg0; | |
107 } else { | |
108 matchers = | |
109 [arg0, arg1, arg2, arg3, arg4, arg5, arg6].where((e) => e != null); | |
110 } | |
111 | |
112 return matchers.map((e) => wrapMatcher(e)).toList(); | |
113 } | |
OLD | NEW |