From 503f2e023e720374f92b49f4cdccc99ce88e5eef Mon Sep 17 00:00:00 2001 From: Till Friebe Date: Mon, 8 Jun 2020 10:11:20 +0200 Subject: [PATCH] Add hashcode and equals to value This simplifies testing, as now one can compare like this: ```dart expect(Value(1), Value(1)); ``` Or a realistic example: ```dart final capturedArgument = verify(fooDao.insert(captureAny)).captured.first.createdAt; expect(capturedArgument, Value(DateTime(0))); ``` A test is still missing which would look something like this: ``` test('values support hash and equals', () { const first = Value(0); final equalToFirst = Value(0); const different = Values.absent()); expect(first.hashCode, equalToFirst.hashCode); expect(first, equals(equalToFirst)); expect(first, isNot(equals(different))); expect(first, equals(first)); }); ``` I'm not sure where the test is supposed to be. --- moor/lib/src/runtime/data_class.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/moor/lib/src/runtime/data_class.dart b/moor/lib/src/runtime/data_class.dart index 1dcd45b6..17d6999b 100644 --- a/moor/lib/src/runtime/data_class.dart +++ b/moor/lib/src/runtime/data_class.dart @@ -125,6 +125,17 @@ class Value { @override String toString() => present ? 'Value($value)' : 'Value.absent()'; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is Value && + runtimeType == other.runtimeType && + present == other.present && + value == other.value; + + @override + int get hashCode => present.hashCode ^ value.hashCode; } /// Serializer responsible for mapping atomic types from and to json.