Line data Source code
1 : // Copyright (c) 2016, 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 :
7 : /// Returns a matcher which matches if the match argument is greater
8 : /// than the given [value].
9 : Matcher greaterThan(value) =>
10 2 : new _OrderingMatcher(value, false, false, true, 'a value greater than');
11 :
12 : /// Returns a matcher which matches if the match argument is greater
13 : /// than or equal to the given [value].
14 1 : Matcher greaterThanOrEqualTo(value) => new _OrderingMatcher(
15 : value, true, false, true, 'a value greater than or equal to');
16 :
17 : /// Returns a matcher which matches if the match argument is less
18 : /// than the given [value].
19 : Matcher lessThan(value) =>
20 1 : new _OrderingMatcher(value, false, true, false, 'a value less than');
21 :
22 : /// Returns a matcher which matches if the match argument is less
23 : /// than or equal to the given [value].
24 0 : Matcher lessThanOrEqualTo(value) => new _OrderingMatcher(
25 : value, true, true, false, 'a value less than or equal to');
26 :
27 : /// A matcher which matches if the match argument is zero.
28 : const Matcher isZero =
29 : const _OrderingMatcher(0, true, false, false, 'a value equal to');
30 :
31 : /// A matcher which matches if the match argument is non-zero.
32 : const Matcher isNonZero =
33 : const _OrderingMatcher(0, false, true, true, 'a value not equal to');
34 :
35 : /// A matcher which matches if the match argument is positive.
36 : const Matcher isPositive =
37 : const _OrderingMatcher(0, false, false, true, 'a positive value', false);
38 :
39 : /// A matcher which matches if the match argument is zero or negative.
40 : const Matcher isNonPositive =
41 : const _OrderingMatcher(0, true, true, false, 'a non-positive value', false);
42 :
43 : /// A matcher which matches if the match argument is negative.
44 : const Matcher isNegative =
45 : const _OrderingMatcher(0, false, true, false, 'a negative value', false);
46 :
47 : /// A matcher which matches if the match argument is zero or positive.
48 : const Matcher isNonNegative =
49 : const _OrderingMatcher(0, true, false, true, 'a non-negative value', false);
50 :
51 : // TODO(kevmoo) Note that matchers that use _OrderingComparison only use
52 : // `==` and `<` operators to evaluate the match. Or change the matcher.
53 : class _OrderingMatcher extends Matcher {
54 : /// Expected value.
55 : final _value;
56 :
57 : /// What to return if actual == expected
58 : final bool _equalValue;
59 :
60 : /// What to return if actual < expected
61 : final bool _lessThanValue;
62 :
63 : /// What to return if actual > expected
64 : final bool _greaterThanValue;
65 :
66 : /// Textual name of the inequality
67 : final String _comparisonDescription;
68 :
69 : /// Whether to include the expected value in the description
70 : final bool _valueInDescription;
71 :
72 : const _OrderingMatcher(this._value, this._equalValue, this._lessThanValue,
73 : this._greaterThanValue, this._comparisonDescription,
74 : [bool valueInDescription = true])
75 3 : : this._valueInDescription = valueInDescription;
76 :
77 : bool matches(item, Map matchState) {
78 6 : if (item == _value) {
79 1 : return _equalValue;
80 6 : } else if (item < _value) {
81 1 : return _lessThanValue;
82 : } else {
83 3 : return _greaterThanValue;
84 : }
85 : }
86 :
87 : Description describe(Description description) {
88 0 : if (_valueInDescription) {
89 : return description
90 0 : .add(_comparisonDescription)
91 0 : .add(' ')
92 0 : .addDescriptionOf(_value);
93 : } else {
94 0 : return description.add(_comparisonDescription);
95 : }
96 : }
97 :
98 : Description describeMismatch(
99 : item, Description mismatchDescription, Map matchState, bool verbose) {
100 0 : mismatchDescription.add('is not ');
101 0 : return describe(mismatchDescription);
102 : }
103 : }
|