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