Skip to content

Commit 3f68088

Browse files
committed
fix(built_value_test): respect non comparable fields
Signed-off-by: Nikolas Rimikis <[email protected]>
1 parent 153e6ba commit 3f68088

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

built_value_test/lib/matcher.dart

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import 'package:matcher/matcher.dart';
99
/// Returns a matcher that matches if the value is structurally equal to
1010
/// [expected].
1111
///
12+
/// When [onlyCheckComparable] fields that are not comparable are ignored for
13+
/// the equality check.
14+
///
1215
/// Improves on a simple equality test by offering a detailed mismatch message.
13-
Matcher equalsBuilt(Built expected) => _BuiltValueMatcher(expected);
16+
Matcher equalsBuilt(Built expected, {bool onlyCheckComparable = false}) =>
17+
_BuiltValueMatcher(expected, onlyCheckComparable);
1418

1519
/// Matcher for [Built] instances.
1620
///
@@ -19,8 +23,10 @@ Matcher equalsBuilt(Built expected) => _BuiltValueMatcher(expected);
1923
class _BuiltValueMatcher implements Matcher {
2024
final Built _expected;
2125
final Matcher _delegate;
26+
final bool _onlyCheckComparable;
2227

23-
_BuiltValueMatcher(this._expected) : _delegate = equals(_toMap(_expected));
28+
_BuiltValueMatcher(this._expected, this._onlyCheckComparable)
29+
: _delegate = equals(_toMap(_expected));
2430

2531
@override
2632
Description describe(Description description) =>
@@ -41,6 +47,8 @@ class _BuiltValueMatcher implements Matcher {
4147
bool matches(dynamic item, Map matchState) {
4248
if (_expected.runtimeType != item.runtimeType) return false;
4349

50+
if (_onlyCheckComparable && _expected == item) return true;
51+
4452
return _delegate.matches(_toMap(item), matchState);
4553
}
4654
}

built_value_test/test/matcher_test.dart

+16-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ void main() {
103103
..name = 'foo'
104104
..onChanged = () => 'Change happened!');
105105

106-
expect(value, otherValue);
106+
expect(value, isNot(equalsBuilt(otherValue)));
107+
expect(
108+
value,
109+
equalsBuilt(
110+
otherValue,
111+
onlyCheckComparable: true,
112+
),
113+
);
107114
});
108115

109116
test('compared value matcher with different onChanged outcomes', () {
@@ -114,7 +121,14 @@ void main() {
114121
..name = 'foo'
115122
..onChanged = () => 'Change did not happen!');
116123

117-
expect(value, otherValue);
124+
expect(value, isNot(equalsBuilt(otherValue)));
125+
expect(
126+
value,
127+
equalsBuilt(
128+
otherValue,
129+
onlyCheckComparable: true,
130+
),
131+
);
118132
});
119133

120134
test('compared value matcher with different names', () {

0 commit comments

Comments
 (0)