split out order matchers from numeric matchers lib
R=nweiz@google.com
Review URL: https://codereview.chromium.org//1956483003 .
diff --git a/lib/matcher.dart b/lib/matcher.dart
index f4ebf08..b4b9d22 100644
--- a/lib/matcher.dart
+++ b/lib/matcher.dart
@@ -11,5 +11,6 @@
export 'src/map_matchers.dart';
export 'src/numeric_matchers.dart';
export 'src/operator_matchers.dart';
+export 'src/order_matchers.dart';
export 'src/string_matchers.dart';
export 'src/util.dart';
diff --git a/lib/src/numeric_matchers.dart b/lib/src/numeric_matchers.dart
index 0cdb98c..909c118 100644
--- a/lib/src/numeric_matchers.dart
+++ b/lib/src/numeric_matchers.dart
@@ -4,103 +4,6 @@
import 'interfaces.dart';
-/// Returns a matcher which matches if the match argument is greater
-/// than the given [value].
-Matcher greaterThan(value) =>
- new _OrderingComparison(value, false, false, true, 'a value greater than');
-
-/// Returns a matcher which matches if the match argument is greater
-/// than or equal to the given [value].
-Matcher greaterThanOrEqualTo(value) => new _OrderingComparison(
- value, true, false, true, 'a value greater than or equal to');
-
-/// Returns a matcher which matches if the match argument is less
-/// than the given [value].
-Matcher lessThan(value) =>
- new _OrderingComparison(value, false, true, false, 'a value less than');
-
-/// Returns a matcher which matches if the match argument is less
-/// than or equal to the given [value].
-Matcher lessThanOrEqualTo(value) => new _OrderingComparison(
- value, true, true, false, 'a value less than or equal to');
-
-/// A matcher which matches if the match argument is zero.
-const Matcher isZero =
- const _OrderingComparison(0, true, false, false, 'a value equal to');
-
-/// A matcher which matches if the match argument is non-zero.
-const Matcher isNonZero =
- const _OrderingComparison(0, false, true, true, 'a value not equal to');
-
-/// A matcher which matches if the match argument is positive.
-const Matcher isPositive =
- const _OrderingComparison(0, false, false, true, 'a positive value', false);
-
-/// A matcher which matches if the match argument is zero or negative.
-const Matcher isNonPositive = const _OrderingComparison(
- 0, true, true, false, 'a non-positive value', false);
-
-/// A matcher which matches if the match argument is negative.
-const Matcher isNegative =
- const _OrderingComparison(0, false, true, false, 'a negative value', false);
-
-/// A matcher which matches if the match argument is zero or positive.
-const Matcher isNonNegative = const _OrderingComparison(
- 0, true, false, true, 'a non-negative value', false);
-
-bool _isNumeric(value) {
- return value is num;
-}
-
-// TODO(kevmoo) Note that matchers that use _OrderingComparison only use
-// `==` and `<` operators to evaluate the match. Or change the matcher.
-class _OrderingComparison extends Matcher {
- /// Expected value.
- final _value;
- /// What to return if actual == expected
- final bool _equalValue;
- /// What to return if actual < expected
- final bool _lessThanValue;
- /// What to return if actual > expected
- final bool _greaterThanValue;
- /// Textual name of the inequality
- final String _comparisonDescription;
- /// Whether to include the expected value in the description
- final bool _valueInDescription;
-
- const _OrderingComparison(this._value, this._equalValue, this._lessThanValue,
- this._greaterThanValue, this._comparisonDescription,
- [bool valueInDescription = true])
- : this._valueInDescription = valueInDescription;
-
- bool matches(item, Map matchState) {
- if (item == _value) {
- return _equalValue;
- } else if (item < _value) {
- return _lessThanValue;
- } else {
- return _greaterThanValue;
- }
- }
-
- Description describe(Description description) {
- if (_valueInDescription) {
- return description
- .add(_comparisonDescription)
- .add(' ')
- .addDescriptionOf(_value);
- } else {
- return description.add(_comparisonDescription);
- }
- }
-
- Description describeMismatch(
- item, Description mismatchDescription, Map matchState, bool verbose) {
- mismatchDescription.add('is not ');
- return describe(mismatchDescription);
- }
-}
-
/// Returns a matcher which matches if the match argument is within [delta]
/// of some [value].
///
@@ -114,9 +17,8 @@
const _IsCloseTo(this._value, this._delta);
bool matches(item, Map matchState) {
- if (!_isNumeric(item)) {
- return false;
- }
+ if (item is! num) return false;
+
var diff = item - _value;
if (diff < 0) diff = -diff;
return (diff <= _delta);
diff --git a/lib/src/order_matchers.dart b/lib/src/order_matchers.dart
new file mode 100644
index 0000000..b6079e9
--- /dev/null
+++ b/lib/src/order_matchers.dart
@@ -0,0 +1,103 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'interfaces.dart';
+
+/// Returns a matcher which matches if the match argument is greater
+/// than the given [value].
+Matcher greaterThan(value) =>
+ new _OrderingMatcher(value, false, false, true, 'a value greater than');
+
+/// Returns a matcher which matches if the match argument is greater
+/// than or equal to the given [value].
+Matcher greaterThanOrEqualTo(value) => new _OrderingMatcher(
+ value, true, false, true, 'a value greater than or equal to');
+
+/// Returns a matcher which matches if the match argument is less
+/// than the given [value].
+Matcher lessThan(value) =>
+ new _OrderingMatcher(value, false, true, false, 'a value less than');
+
+/// Returns a matcher which matches if the match argument is less
+/// than or equal to the given [value].
+Matcher lessThanOrEqualTo(value) => new _OrderingMatcher(
+ value, true, true, false, 'a value less than or equal to');
+
+/// A matcher which matches if the match argument is zero.
+const Matcher isZero =
+ const _OrderingMatcher(0, true, false, false, 'a value equal to');
+
+/// A matcher which matches if the match argument is non-zero.
+const Matcher isNonZero =
+ const _OrderingMatcher(0, false, true, true, 'a value not equal to');
+
+/// A matcher which matches if the match argument is positive.
+const Matcher isPositive =
+ const _OrderingMatcher(0, false, false, true, 'a positive value', false);
+
+/// A matcher which matches if the match argument is zero or negative.
+const Matcher isNonPositive =
+ const _OrderingMatcher(0, true, true, false, 'a non-positive value', false);
+
+/// A matcher which matches if the match argument is negative.
+const Matcher isNegative =
+ const _OrderingMatcher(0, false, true, false, 'a negative value', false);
+
+/// A matcher which matches if the match argument is zero or positive.
+const Matcher isNonNegative =
+ const _OrderingMatcher(0, true, false, true, 'a non-negative value', false);
+
+// TODO(kevmoo) Note that matchers that use _OrderingComparison only use
+// `==` and `<` operators to evaluate the match. Or change the matcher.
+class _OrderingMatcher extends Matcher {
+ /// Expected value.
+ final _value;
+
+ /// What to return if actual == expected
+ final bool _equalValue;
+
+ /// What to return if actual < expected
+ final bool _lessThanValue;
+
+ /// What to return if actual > expected
+ final bool _greaterThanValue;
+
+ /// Textual name of the inequality
+ final String _comparisonDescription;
+
+ /// Whether to include the expected value in the description
+ final bool _valueInDescription;
+
+ const _OrderingMatcher(this._value, this._equalValue, this._lessThanValue,
+ this._greaterThanValue, this._comparisonDescription,
+ [bool valueInDescription = true])
+ : this._valueInDescription = valueInDescription;
+
+ bool matches(item, Map matchState) {
+ if (item == _value) {
+ return _equalValue;
+ } else if (item < _value) {
+ return _lessThanValue;
+ } else {
+ return _greaterThanValue;
+ }
+ }
+
+ Description describe(Description description) {
+ if (_valueInDescription) {
+ return description
+ .add(_comparisonDescription)
+ .add(' ')
+ .addDescriptionOf(_value);
+ } else {
+ return description.add(_comparisonDescription);
+ }
+ }
+
+ Description describeMismatch(
+ item, Description mismatchDescription, Map matchState, bool verbose) {
+ mismatchDescription.add('is not ');
+ return describe(mismatchDescription);
+ }
+}
diff --git a/test/numeric_matchers_test.dart b/test/numeric_matchers_test.dart
index 922f303..3a5eb2c 100644
--- a/test/numeric_matchers_test.dart
+++ b/test/numeric_matchers_test.dart
@@ -8,83 +8,6 @@
import 'test_utils.dart';
void main() {
- test('greaterThan', () {
- shouldPass(10, greaterThan(9));
- shouldFail(9, greaterThan(10), "Expected: a value greater than <10> "
- "Actual: <9> "
- "Which: is not a value greater than <10>");
- });
-
- test('greaterThanOrEqualTo', () {
- shouldPass(10, greaterThanOrEqualTo(10));
- shouldFail(9, greaterThanOrEqualTo(10),
- "Expected: a value greater than or equal to <10> "
- "Actual: <9> "
- "Which: is not a value greater than or equal to <10>");
- });
-
- test('lessThan', () {
- shouldFail(10, lessThan(9), "Expected: a value less than <9> "
- "Actual: <10> "
- "Which: is not a value less than <9>");
- shouldPass(9, lessThan(10));
- });
-
- test('lessThanOrEqualTo', () {
- shouldPass(10, lessThanOrEqualTo(10));
- shouldFail(11, lessThanOrEqualTo(10),
- "Expected: a value less than or equal to <10> "
- "Actual: <11> "
- "Which: is not a value less than or equal to <10>");
- });
-
- test('isZero', () {
- shouldPass(0, isZero);
- shouldFail(1, isZero, "Expected: a value equal to <0> "
- "Actual: <1> "
- "Which: is not a value equal to <0>");
- });
-
- test('isNonZero', () {
- shouldFail(0, isNonZero, "Expected: a value not equal to <0> "
- "Actual: <0> "
- "Which: is not a value not equal to <0>");
- shouldPass(1, isNonZero);
- });
-
- test('isPositive', () {
- shouldFail(-1, isPositive, "Expected: a positive value "
- "Actual: <-1> "
- "Which: is not a positive value");
- shouldFail(0, isPositive, "Expected: a positive value "
- "Actual: <0> "
- "Which: is not a positive value");
- shouldPass(1, isPositive);
- });
-
- test('isNegative', () {
- shouldPass(-1, isNegative);
- shouldFail(0, isNegative, "Expected: a negative value "
- "Actual: <0> "
- "Which: is not a negative value");
- });
-
- test('isNonPositive', () {
- shouldPass(-1, isNonPositive);
- shouldPass(0, isNonPositive);
- shouldFail(1, isNonPositive, "Expected: a non-positive value "
- "Actual: <1> "
- "Which: is not a non-positive value");
- });
-
- test('isNonNegative', () {
- shouldPass(1, isNonNegative);
- shouldPass(0, isNonNegative);
- shouldFail(-1, isNonNegative, "Expected: a non-negative value "
- "Actual: <-1> "
- "Which: is not a non-negative value");
- });
-
test('closeTo', () {
shouldPass(0, closeTo(0, 1));
shouldPass(-1, closeTo(0, 1));
diff --git a/test/order_matchers_test.dart b/test/order_matchers_test.dart
new file mode 100644
index 0000000..47ce3c5
--- /dev/null
+++ b/test/order_matchers_test.dart
@@ -0,0 +1,87 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:matcher/matcher.dart';
+import 'package:test/test.dart' show test;
+
+import 'test_utils.dart';
+
+void main() {
+ test('greaterThan', () {
+ shouldPass(10, greaterThan(9));
+ shouldFail(9, greaterThan(10), "Expected: a value greater than <10> "
+ "Actual: <9> "
+ "Which: is not a value greater than <10>");
+ });
+
+ test('greaterThanOrEqualTo', () {
+ shouldPass(10, greaterThanOrEqualTo(10));
+ shouldFail(9, greaterThanOrEqualTo(10),
+ "Expected: a value greater than or equal to <10> "
+ "Actual: <9> "
+ "Which: is not a value greater than or equal to <10>");
+ });
+
+ test('lessThan', () {
+ shouldFail(10, lessThan(9), "Expected: a value less than <9> "
+ "Actual: <10> "
+ "Which: is not a value less than <9>");
+ shouldPass(9, lessThan(10));
+ });
+
+ test('lessThanOrEqualTo', () {
+ shouldPass(10, lessThanOrEqualTo(10));
+ shouldFail(11, lessThanOrEqualTo(10),
+ "Expected: a value less than or equal to <10> "
+ "Actual: <11> "
+ "Which: is not a value less than or equal to <10>");
+ });
+
+ test('isZero', () {
+ shouldPass(0, isZero);
+ shouldFail(1, isZero, "Expected: a value equal to <0> "
+ "Actual: <1> "
+ "Which: is not a value equal to <0>");
+ });
+
+ test('isNonZero', () {
+ shouldFail(0, isNonZero, "Expected: a value not equal to <0> "
+ "Actual: <0> "
+ "Which: is not a value not equal to <0>");
+ shouldPass(1, isNonZero);
+ });
+
+ test('isPositive', () {
+ shouldFail(-1, isPositive, "Expected: a positive value "
+ "Actual: <-1> "
+ "Which: is not a positive value");
+ shouldFail(0, isPositive, "Expected: a positive value "
+ "Actual: <0> "
+ "Which: is not a positive value");
+ shouldPass(1, isPositive);
+ });
+
+ test('isNegative', () {
+ shouldPass(-1, isNegative);
+ shouldFail(0, isNegative, "Expected: a negative value "
+ "Actual: <0> "
+ "Which: is not a negative value");
+ });
+
+ test('isNonPositive', () {
+ shouldPass(-1, isNonPositive);
+ shouldPass(0, isNonPositive);
+ shouldFail(1, isNonPositive, "Expected: a non-positive value "
+ "Actual: <1> "
+ "Which: is not a non-positive value");
+ });
+
+ test('isNonNegative', () {
+ shouldPass(1, isNonNegative);
+ shouldPass(0, isNonNegative);
+ shouldFail(-1, isNonNegative, "Expected: a non-negative value "
+ "Actual: <-1> "
+ "Which: is not a non-negative value");
+ });
+}