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.
This commit is contained in:
Till Friebe 2020-06-08 10:11:20 +02:00
parent 32cae11aa2
commit 503f2e023e
1 changed files with 11 additions and 0 deletions

View File

@ -125,6 +125,17 @@ class Value<T> {
@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.