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 within [delta] | 7 /// Returns a matcher which matches if the match argument is within [delta] |
8 /// of some [value]. | 8 /// of some [value]. |
9 /// | 9 /// |
10 /// 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 |
11 /// 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]. |
12 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta); | 12 Matcher closeTo(num value, num delta) => new _IsCloseTo(value, delta); |
13 | 13 |
14 class _IsCloseTo extends Matcher { | 14 class _IsCloseTo extends Matcher { |
15 final num _value, _delta; | 15 final num _value, _delta; |
16 | 16 |
17 const _IsCloseTo(this._value, this._delta); | 17 const _IsCloseTo(this._value, this._delta); |
18 | 18 |
19 bool matches(item, Map matchState) { | 19 bool matches(item, Map matchState) { |
20 if (item is! num) return false; | 20 if (item is num) { |
21 | 21 var diff = item - _value; |
22 var diff = item - _value; | 22 if (diff < 0) diff = -diff; |
23 if (diff < 0) diff = -diff; | 23 return (diff <= _delta); |
24 return (diff <= _delta); | 24 } else { |
| 25 return false; |
| 26 } |
25 } | 27 } |
26 | 28 |
27 Description describe(Description description) => description | 29 Description describe(Description description) => description |
28 .add('a numeric value within ') | 30 .add('a numeric value within ') |
29 .addDescriptionOf(_delta) | 31 .addDescriptionOf(_delta) |
30 .add(' of ') | 32 .add(' of ') |
31 .addDescriptionOf(_value); | 33 .addDescriptionOf(_value); |
32 | 34 |
33 Description describeMismatch( | 35 Description describeMismatch( |
34 item, Description mismatchDescription, Map matchState, bool verbose) { | 36 item, Description mismatchDescription, Map matchState, bool verbose) { |
35 if (item is! num) { | 37 if (item is num) { |
36 return mismatchDescription.add(' not numeric'); | |
37 } else { | |
38 var diff = item - _value; | 38 var diff = item - _value; |
39 if (diff < 0) diff = -diff; | 39 if (diff < 0) diff = -diff; |
40 return mismatchDescription.add(' differs by ').addDescriptionOf(diff); | 40 return mismatchDescription.add(' differs by ').addDescriptionOf(diff); |
| 41 } else { |
| 42 return mismatchDescription.add(' not numeric'); |
41 } | 43 } |
42 } | 44 } |
43 } | 45 } |
44 | 46 |
45 /// Returns a matcher which matches if the match argument is greater | 47 /// Returns a matcher which matches if the match argument is greater |
46 /// than or equal to [low] and less than or equal to [high]. | 48 /// than or equal to [low] and less than or equal to [high]. |
47 Matcher inInclusiveRange(num low, num high) => | 49 Matcher inInclusiveRange(num low, num high) => |
48 new _InRange(low, high, true, true); | 50 new _InRange(low, high, true, true); |
49 | 51 |
50 /// Returns a matcher which matches if the match argument is greater | 52 /// Returns a matcher which matches if the match argument is greater |
(...skipping 12 matching lines...) Expand all Loading... |
63 new _InRange(low, high, true, false); | 65 new _InRange(low, high, true, false); |
64 | 66 |
65 class _InRange extends Matcher { | 67 class _InRange extends Matcher { |
66 final num _low, _high; | 68 final num _low, _high; |
67 final bool _lowMatchValue, _highMatchValue; | 69 final bool _lowMatchValue, _highMatchValue; |
68 | 70 |
69 const _InRange( | 71 const _InRange( |
70 this._low, this._high, this._lowMatchValue, this._highMatchValue); | 72 this._low, this._high, this._lowMatchValue, this._highMatchValue); |
71 | 73 |
72 bool matches(value, Map matchState) { | 74 bool matches(value, Map matchState) { |
73 if (value is! num) { | 75 if (value is num) { |
| 76 if (value < _low || value > _high) { |
| 77 return false; |
| 78 } |
| 79 if (value == _low) { |
| 80 return _lowMatchValue; |
| 81 } |
| 82 if (value == _high) { |
| 83 return _highMatchValue; |
| 84 } |
| 85 return true; |
| 86 } else { |
74 return false; | 87 return false; |
75 } | 88 } |
76 if (value < _low || value > _high) { | |
77 return false; | |
78 } | |
79 if (value == _low) { | |
80 return _lowMatchValue; | |
81 } | |
82 if (value == _high) { | |
83 return _highMatchValue; | |
84 } | |
85 return true; | |
86 } | 89 } |
87 | 90 |
88 Description describe(Description description) => | 91 Description describe(Description description) => |
89 description.add("be in range from " | 92 description.add("be in range from " |
90 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " | 93 "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " |
91 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); | 94 "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); |
92 | 95 |
93 Description describeMismatch( | 96 Description describeMismatch( |
94 item, Description mismatchDescription, Map matchState, bool verbose) { | 97 item, Description mismatchDescription, Map matchState, bool verbose) { |
95 if (item is! num) { | 98 if (item is! num) { |
96 return mismatchDescription.addDescriptionOf(item).add(' not numeric'); | 99 return mismatchDescription.addDescriptionOf(item).add(' not numeric'); |
97 } else { | 100 } else { |
98 return super | 101 return super |
99 .describeMismatch(item, mismatchDescription, matchState, verbose); | 102 .describeMismatch(item, mismatchDescription, matchState, verbose); |
100 } | 103 } |
101 } | 104 } |
102 } | 105 } |
OLD | NEW |