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