Skip to content

Commit

Permalink
test: much better example for custom equality tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devmil committed Nov 24, 2024
1 parent 5e1a53c commit d0ac6c4
Showing 1 changed file with 27 additions and 58 deletions.
85 changes: 27 additions & 58 deletions test/equatable_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,31 @@ class Person with EquatableMixin {
}

@immutable
abstract class CustomString {
String get stringValue;
class AnimalName {
const AnimalName(this.name);

final String name;

String get normalized {
return name.replaceAll(' ', '').toLowerCase();
}

@override
bool operator ==(Object other) {
if (other is CustomString) {
return other.stringValue == stringValue;
}
if (other is String) {
return other == stringValue;
if (other is AnimalName) {
return normalized == other.normalized;
}
return false;
}

@override
int get hashCode => stringValue.hashCode;
}

class PlainString extends CustomString {
PlainString(this.stringValue);

@override
final String stringValue;
}

class TranslatableString extends CustomString {
TranslatableString({
required Map<String, String> translations,
required String defaultLanguage,
}) : _translations = translations,
_defaultLanguage = defaultLanguage;

final Map<String, String> _translations;
final String _defaultLanguage;

String? inLanguage(String language) => _translations[language];
@override
String get stringValue => _translations[_defaultLanguage]!;
int get hashCode => normalized.hashCode;
}

class Dog with EquatableMixin {
Dog({required this.name});

final CustomString name;
final AnimalName name;

@override
List<Object?> get props => [name];
Expand All @@ -65,7 +46,7 @@ class Dog with EquatableMixin {
class Cat with EquatableMixin {
Cat({required this.name});

final CustomString name;
final AnimalName name;

@override
List<Object?> get props => [name];
Expand All @@ -75,8 +56,8 @@ void main() {
final bob = Person(name: 'Bob');
final alice = Person(name: 'Alice');
final aliceCopy = Person(name: 'Alice');
final fluffyCat = Cat(name: PlainString('fluffy'));
final fluffyDog = Dog(name: PlainString('fluffy'));
final fluffyCat = Cat(name: const AnimalName('fluffy'));
final fluffyDog = Dog(name: const AnimalName('fluffy'));

group('equals', () {
test('returns true when both are null', () {
Expand Down Expand Up @@ -341,8 +322,8 @@ void main() {
test('returns true for Equatables with custom equality members ', () {
expect(
objectsEquals(
Dog(name: PlainString('fluffy')),
Dog(name: PlainString('fluffy')),
Dog(name: const AnimalName('fluffy')),
Dog(name: const AnimalName('fluffy')),
),
isTrue,
);
Expand All @@ -351,43 +332,31 @@ void main() {
test(
'returns true for Equatables with custom equality members '
'that are equal but have a different runtimeType', () {
final plainName = PlainString('fluffy');
final translatableName = TranslatableString(
translations: const {
'en': 'fluffy',
'de': 'flauschi',
},
defaultLanguage: 'en',
);
const nameLowerCase = AnimalName('fluffy');
const nameNormalCase = AnimalName('Fluffy');
//cross check
expect(plainName == translatableName, isTrue);
expect(nameLowerCase == nameNormalCase, isTrue);
//actual check
expect(
objectsEquals(
Dog(name: plainName),
Dog(name: translatableName),
Dog(name: nameLowerCase),
Dog(name: nameNormalCase),
),
isTrue,
);
});
test(
'returns false for Equatables with custom equality members '
'that are not equal and have a different runtimeType', () {
final plainName = PlainString('fluffy');
final translatableName = TranslatableString(
translations: const {
'en': 'fluffy',
'de': 'flauschi',
},
defaultLanguage: 'de',
);
const nameLowerCase = AnimalName('fluffy');
const differentNameLowerCase = AnimalName('sam');
//cross check
expect(plainName == translatableName, isFalse);
expect(nameLowerCase == differentNameLowerCase, isFalse);
//actual check
expect(
objectsEquals(
Dog(name: plainName),
Dog(name: translatableName),
Dog(name: nameLowerCase),
Dog(name: differentNameLowerCase),
),
isFalse,
);
Expand Down

0 comments on commit d0ac6c4

Please sign in to comment.