OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'interfaces.dart'; | 5 import 'interfaces.dart'; |
6 | 6 |
7 /// Returns a matcher which matches if the match argument is greater | |
8 /// than the given [value]. | |
9 Matcher greaterThan(value) => | |
10 new _OrderingComparison(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 Matcher greaterThanOrEqualTo(value) => new _OrderingComparison( | |
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 new _OrderingComparison(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 Matcher lessThanOrEqualTo(value) => new _OrderingComparison( | |
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 _OrderingComparison(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 _OrderingComparison(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 _OrderingComparison(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 = const _OrderingComparison( | |
41 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 _OrderingComparison(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 = const _OrderingComparison( | |
49 0, true, false, true, 'a non-negative value', false); | |
50 | |
51 bool _isNumeric(value) { | |
52 return value is num; | |
53 } | |
54 | |
55 // TODO(kevmoo) Note that matchers that use _OrderingComparison only use | |
56 // `==` and `<` operators to evaluate the match. Or change the matcher. | |
57 class _OrderingComparison extends Matcher { | |
58 /// Expected value. | |
59 final _value; | |
60 /// What to return if actual == expected | |
61 final bool _equalValue; | |
62 /// What to return if actual < expected | |
63 final bool _lessThanValue; | |
64 /// What to return if actual > expected | |
65 final bool _greaterThanValue; | |
66 /// Textual name of the inequality | |
67 final String _comparisonDescription; | |
68 /// Whether to include the expected value in the description | |
69 final bool _valueInDescription; | |
70 | |
71 const _OrderingComparison(this._value, this._equalValue, this._lessThanValue, | |
72 this._greaterThanValue, this._comparisonDescription, | |
73 [bool valueInDescription = true]) | |
74 : this._valueInDescription = valueInDescription; | |
75 | |
76 bool matches(item, Map matchState) { | |
77 if (item == _value) { | |
78 return _equalValue; | |
79 } else if (item < _value) { | |
80 return _lessThanValue; | |
81 } else { | |
82 return _greaterThanValue; | |
83 } | |
84 } | |
85 | |
86 Description describe(Description description) { | |
87 if (_valueInDescription) { | |
88 return description | |
89 .add(_comparisonDescription) | |
90 .add(' ') | |
91 .addDescriptionOf(_value); | |
92 } else { | |
93 return description.add(_comparisonDescription); | |
94 } | |
95 } | |
96 | |
97 Description describeMismatch( | |
98 item, Description mismatchDescription, Map matchState, bool verbose) { | |
99 mismatchDescription.add('is not '); | |
100 return describe(mismatchDescription); | |
101 } | |
102 } | |
103 | |
104 /// Returns a matcher which matches if the match argument is within [delta] | 7 /// Returns a matcher which matches if the match argument is within [delta] |
105 /// of some [value]. | 8 /// of some [value]. |
106 /// | 9 /// |
107 /// In other words, this matches if the match argument is greater than | 10 /// In other words, this matches if the match argument is greater than |
108 /// than or equal [value]-[delta] and less than or equal to [value]+[delta]. | 11 /// than or equal [value]-[delta] and less than or equal to [value]+[delta]. |
109 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta); | 12 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta); |
110 | 13 |
111 class _IsCloseTo extends Matcher { | 14 class _IsCloseTo extends Matcher { |
112 final num _value, _delta; | 15 final num _value, _delta; |
113 | 16 |
114 const _IsCloseTo(this._value, this._delta); | 17 const _IsCloseTo(this._value, this._delta); |
115 | 18 |
116 bool matches(item, Map matchState) { | 19 bool matches(item, Map matchState) { |
117 if (!_isNumeric(item)) { | 20 if (item is! num) return false; |
118 return false; | 21 |
119 } | |
120 var diff = item - _value; | 22 var diff = item - _value; |
121 if (diff < 0) diff = -diff; | 23 if (diff < 0) diff = -diff; |
122 return (diff <= _delta); | 24 return (diff <= _delta); |
123 } | 25 } |
124 | 26 |
125 Description describe(Description description) => description | 27 Description describe(Description description) => description |
126 .add('a numeric value within ') | 28 .add('a numeric value within ') |
127 .addDescriptionOf(_delta) | 29 .addDescriptionOf(_delta) |
128 .add(' of ') | 30 .add(' of ') |
129 .addDescriptionOf(_value); | 31 .addDescriptionOf(_value); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 Description describeMismatch( | 93 Description describeMismatch( |
192 item, Description mismatchDescription, Map matchState, bool verbose) { | 94 item, Description mismatchDescription, Map matchState, bool verbose) { |
193 if (item is! num) { | 95 if (item is! num) { |
194 return mismatchDescription.addDescriptionOf(item).add(' not numeric'); | 96 return mismatchDescription.addDescriptionOf(item).add(' not numeric'); |
195 } else { | 97 } else { |
196 return super.describeMismatch( | 98 return super.describeMismatch( |
197 item, mismatchDescription, matchState, verbose); | 99 item, mismatchDescription, matchState, verbose); |
198 } | 100 } |
199 } | 101 } |
200 } | 102 } |
OLD | NEW |