Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkgs/matcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
## 0.12.18-wip

* Add `isSorted` and related matchers for iterables.
* Consider `NaN` to be equal to itself in `equals`.
* Remove some dynamic invocations.
* Add explicit casts from `dynamic` values.
* Require Dart 3.7
* Add `isSorted` and related matchers for iterables.

## 0.12.17

Expand Down
10 changes: 8 additions & 2 deletions pkgs/matcher/lib/src/equals_matcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,15 @@ class _DeepMatcher extends Matcher {
}
});
} else {
// Otherwise, test for equality.
// Otherwise, test for equality, or both values NaN
try {
if (expected == actual) return null;
if (expected == actual ||
(expected is num &&
expected.isNaN &&
actual is num &&
actual.isNaN)) {
return null;
}
} catch (e) {
// TODO(gram): Add a test for this case.
return _Mismatch(
Expand Down
26 changes: 26 additions & 0 deletions pkgs/matcher/test/core_matchers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,32 @@ void main() {
);
});

test('equals with NaN', () {
final a = double.nan;
final b = 0;
shouldPass(a, equals(a));
shouldFail(a, equals(b), 'Expected: <0> Actual: <NaN>');
shouldFail(b, equals(a), 'Expected: <NaN> Actual: <0>');
});

test('equals with NaN in a collection', () {
final a = {double.nan};
final b = {0};
shouldPass(a, equals(a));
shouldFail(
a,
equals(b),
'Expected: Set:[0] Actual: Set:[NaN] '
'Which: does not contain <0>',
);
shouldFail(
b,
equals(a),
'Expected: Set:[NaN] Actual: Set:[0] '
'Which: does not contain <NaN>',
);
});

test('anything', () {
var a = <Object?, Object?>{};
shouldPass(0, anything);
Expand Down