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 0 : Matcher greaterThan(Object value) =>
10 0 : _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 0 : Matcher greaterThanOrEqualTo(Object value) => _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 0 : Matcher lessThan(Object value) =>
20 0 : _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(Object value) =>
25 0 : _OrderingMatcher(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 : _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 : _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 : _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 : _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 : _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 : _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 Object _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 11 : const _OrderingMatcher(this._value, this._equalValue, this._lessThanValue,
73 : this._greaterThanValue, this._comparisonDescription,
74 : [bool valueInDescription = true])
75 : : _valueInDescription = valueInDescription;
76 :
77 0 : @override
78 : bool matches(Object? item, Map matchState) {
79 0 : if (item == _value) {
80 0 : return _equalValue;
81 0 : } else if ((item as dynamic) < _value) {
82 0 : return _lessThanValue;
83 0 : } else if (item > _value) {
84 0 : return _greaterThanValue;
85 : } else {
86 : return false;
87 : }
88 : }
89 :
90 0 : @override
91 : Description describe(Description description) {
92 0 : if (_valueInDescription) {
93 : return description
94 0 : .add(_comparisonDescription)
95 0 : .add(' ')
96 0 : .addDescriptionOf(_value);
97 : } else {
98 0 : return description.add(_comparisonDescription);
99 : }
100 : }
101 :
102 0 : @override
103 : Description describeMismatch(dynamic item, Description mismatchDescription,
104 : Map matchState, bool verbose) {
105 0 : mismatchDescription.add('is not ');
106 0 : return describe(mismatchDescription);
107 : }
108 : }
|