Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: lib/src/order_matchers.dart

Issue 1956483003: pkg/matcher: split out order matchers from numeric matchers lib (Closed) Base URL: https://github.com/dart-lang/matcher.git@master
Patch Set: nits Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/numeric_matchers.dart ('k') | test/numeric_matchers_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 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 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 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 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 : this._valueInDescription = valueInDescription;
76
77 bool matches(item, Map matchState) {
78 if (item == _value) {
79 return _equalValue;
80 } else if (item < _value) {
81 return _lessThanValue;
82 } else {
83 return _greaterThanValue;
84 }
85 }
86
87 Description describe(Description description) {
88 if (_valueInDescription) {
89 return description
90 .add(_comparisonDescription)
91 .add(' ')
92 .addDescriptionOf(_value);
93 } else {
94 return description.add(_comparisonDescription);
95 }
96 }
97
98 Description describeMismatch(
99 item, Description mismatchDescription, Map matchState, bool verbose) {
100 mismatchDescription.add('is not ');
101 return describe(mismatchDescription);
102 }
103 }
OLDNEW
« no previous file with comments | « lib/src/numeric_matchers.dart ('k') | test/numeric_matchers_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698