From 3f680884e086974f44b4d1da9e6c0c13f80074b3 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis <leptopoda@users.noreply.github.com> Date: Sun, 18 Aug 2024 19:54:50 +0200 Subject: [PATCH] fix(built_value_test): respect non comparable fields Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com> --- built_value_test/lib/matcher.dart | 12 ++++++++++-- built_value_test/test/matcher_test.dart | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/built_value_test/lib/matcher.dart b/built_value_test/lib/matcher.dart index b0ea76201..c49849034 100644 --- a/built_value_test/lib/matcher.dart +++ b/built_value_test/lib/matcher.dart @@ -9,8 +9,12 @@ import 'package:matcher/matcher.dart'; /// Returns a matcher that matches if the value is structurally equal to /// [expected]. /// +/// When [onlyCheckComparable] fields that are not comparable are ignored for +/// the equality check. +/// /// Improves on a simple equality test by offering a detailed mismatch message. -Matcher equalsBuilt(Built expected) => _BuiltValueMatcher(expected); +Matcher equalsBuilt(Built expected, {bool onlyCheckComparable = false}) => + _BuiltValueMatcher(expected, onlyCheckComparable); /// Matcher for [Built] instances. /// @@ -19,8 +23,10 @@ Matcher equalsBuilt(Built expected) => _BuiltValueMatcher(expected); class _BuiltValueMatcher implements Matcher { final Built _expected; final Matcher _delegate; + final bool _onlyCheckComparable; - _BuiltValueMatcher(this._expected) : _delegate = equals(_toMap(_expected)); + _BuiltValueMatcher(this._expected, this._onlyCheckComparable) + : _delegate = equals(_toMap(_expected)); @override Description describe(Description description) => @@ -41,6 +47,8 @@ class _BuiltValueMatcher implements Matcher { bool matches(dynamic item, Map matchState) { if (_expected.runtimeType != item.runtimeType) return false; + if (_onlyCheckComparable && _expected == item) return true; + return _delegate.matches(_toMap(item), matchState); } } diff --git a/built_value_test/test/matcher_test.dart b/built_value_test/test/matcher_test.dart index df4338a2d..6a8248bc8 100644 --- a/built_value_test/test/matcher_test.dart +++ b/built_value_test/test/matcher_test.dart @@ -103,7 +103,14 @@ void main() { ..name = 'foo' ..onChanged = () => 'Change happened!'); - expect(value, otherValue); + expect(value, isNot(equalsBuilt(otherValue))); + expect( + value, + equalsBuilt( + otherValue, + onlyCheckComparable: true, + ), + ); }); test('compared value matcher with different onChanged outcomes', () { @@ -114,7 +121,14 @@ void main() { ..name = 'foo' ..onChanged = () => 'Change did not happen!'); - expect(value, otherValue); + expect(value, isNot(equalsBuilt(otherValue))); + expect( + value, + equalsBuilt( + otherValue, + onlyCheckComparable: true, + ), + ); }); test('compared value matcher with different names', () {