Don't consider NaN ordered against any number (#107)
Fixes #106
This is technically breaking - however it will only cause tests that are already buggy to fail.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a1a5a4b..0c98e4b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,11 @@
## 0.12.5
- Add `isA()` to create `TypeMatcher` instances in a more fluent way.
+- **Potentially breaking bug fix**. Ordering matchers no longer treat objects
+ with a partial ordering (such as NaN for double values) as if they had a
+ complete ordering. For instance `greaterThan` now compares with the `>`
+ operator rather not `<` and not `=`. This could cause tests which relied on
+ this bug to start failing.
## 0.12.4
diff --git a/lib/src/numeric_matchers.dart b/lib/src/numeric_matchers.dart
index c84a2f3..62ca3fd 100644
--- a/lib/src/numeric_matchers.dart
+++ b/lib/src/numeric_matchers.dart
@@ -77,7 +77,8 @@
if (value == _high) {
return _highMatchValue;
}
- return true;
+ // Value may still be outside if range if it can't be compared.
+ return value > _low && value < _high;
}
@override
diff --git a/lib/src/order_matchers.dart b/lib/src/order_matchers.dart
index b94231f..c492595 100644
--- a/lib/src/order_matchers.dart
+++ b/lib/src/order_matchers.dart
@@ -80,8 +80,10 @@
return _equalValue;
} else if (item < _value) {
return _lessThanValue;
- } else {
+ } else if (item > _value) {
return _greaterThanValue;
+ } else {
+ return false;
}
}
diff --git a/pubspec.yaml b/pubspec.yaml
index ecd7d3c..f109db1 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: matcher
-version: 0.12.5-dev
+version: 0.12.5
description: >-
Support for specifying test expectations via an extensible Matcher class.
diff --git a/test/numeric_matchers_test.dart b/test/numeric_matchers_test.dart
index a85f13b..c85a0e6 100644
--- a/test/numeric_matchers_test.dart
+++ b/test/numeric_matchers_test.dart
@@ -85,4 +85,15 @@
shouldFail('not a num', inClosedOpenRange(0, 1),
endsWith('not an <Instance of \'num\'>'));
});
+
+ group('NaN', () {
+ test('inInclusiveRange', () {
+ shouldFail(
+ double.nan,
+ inExclusiveRange(double.negativeInfinity, double.infinity),
+ "Expected: be in range from "
+ "-Infinity (exclusive) to Infinity (exclusive) "
+ "Actual: <NaN>");
+ });
+ });
}
diff --git a/test/order_matchers_test.dart b/test/order_matchers_test.dart
index ff09b6c..221b06c 100644
--- a/test/order_matchers_test.dart
+++ b/test/order_matchers_test.dart
@@ -115,4 +115,36 @@
"Actual: <-1> "
"Which: is not a non-negative value");
});
+
+ group('NaN', () {
+ test('greaterThan', () {
+ shouldFail(
+ double.nan,
+ greaterThan(10),
+ "Expected: a value greater than <10> "
+ "Actual: <NaN> "
+ "Which: is not a value greater than <10>");
+ shouldFail(
+ 10,
+ greaterThan(double.nan),
+ "Expected: a value greater than <NaN> "
+ "Actual: <10> "
+ "Which: is not a value greater than <NaN>");
+ });
+
+ test('lessThanOrEqualTo', () {
+ shouldFail(
+ double.nan,
+ lessThanOrEqualTo(10),
+ "Expected: a value less than or equal to <10> "
+ "Actual: <NaN> "
+ "Which: is not a value less than or equal to <10>");
+ shouldFail(
+ 10,
+ lessThanOrEqualTo(double.nan),
+ "Expected: a value less than or equal to <NaN> "
+ "Actual: <10> "
+ "Which: is not a value less than or equal to <NaN>");
+ });
+ });
}