| import 'package:test/test.dart'; |
| |
| import 'package:equatable/equatable.dart'; |
| |
| class NonEquatable {} |
| |
| class EquatableBase with EquatableMixinBase, EquatableMixin {} |
| |
| class EmptyEquatable extends EquatableBase with EquatableMixin {} |
| |
| class SimpleEquatable<T> extends EquatableBase with EquatableMixin { |
| final T data; |
| |
| SimpleEquatable(this.data); |
| |
| @override |
| List get props => super.props..addAll([data]); |
| } |
| |
| class MultipartEquatable<T> extends EquatableBase with EquatableMixin { |
| final T d1; |
| final T d2; |
| |
| MultipartEquatable(this.d1, this.d2); |
| |
| @override |
| List get props => super.props..addAll([d1, d2]); |
| } |
| |
| class OtherEquatable extends EquatableBase with EquatableMixin { |
| final String data; |
| |
| OtherEquatable(this.data); |
| |
| @override |
| List get props => super.props..addAll([data]); |
| } |
| |
| enum Color { blonde, black, brown } |
| |
| class ComplexEquatable extends EquatableBase with EquatableMixin { |
| final String name; |
| final int age; |
| final Color hairColor; |
| final List<String> children; |
| |
| ComplexEquatable({this.name, this.age, this.hairColor, this.children}); |
| |
| @override |
| List get props => super.props..addAll([name, age, hairColor, children]); |
| } |
| |
| class EquatableData extends EquatableBase with EquatableMixin { |
| final String key; |
| final dynamic value; |
| |
| EquatableData({this.key, this.value}); |
| |
| @override |
| List get props => super.props..addAll([key, value]); |
| } |
| |
| void main() { |
| group('Empty Equatable', () { |
| test('should correct toString', () { |
| final instance = EmptyEquatable(); |
| expect(instance.toString(), "Instance of 'EmptyEquatable'"); |
| }); |
| |
| test('should return true when instance is the same', () { |
| final instance = EmptyEquatable(); |
| expect(instance == instance, true); |
| }); |
| |
| test('should return correct hashCode', () { |
| final instance = EmptyEquatable(); |
| expect(instance.hashCode, instance.runtimeType.hashCode); |
| }); |
| |
| test('should return true when instances are different', () { |
| final instanceA = EmptyEquatable(); |
| final instanceB = EmptyEquatable(); |
| expect(instanceA == instanceB, true); |
| }); |
| |
| test('should return false when compared to non-equatable', () { |
| final instanceA = EmptyEquatable(); |
| final instanceB = NonEquatable(); |
| expect(instanceA == instanceB, false); |
| }); |
| }); |
| |
| group('Simple Equatable (string)', () { |
| test('should correct toString', () { |
| final instance = SimpleEquatable('simple'); |
| expect(instance.toString(), '[simple]'); |
| }); |
| |
| test('should return true when instance is the same', () { |
| final instance = SimpleEquatable('simple'); |
| expect(instance == instance, true); |
| }); |
| |
| test('should return correct hashCode', () { |
| final instance = SimpleEquatable('simple'); |
| expect( |
| instance.hashCode, |
| instance.runtimeType.hashCode ^ instance.data.hashCode, |
| ); |
| }); |
| |
| test('should return true when instances are different', () { |
| final instanceA = SimpleEquatable('simple'); |
| final instanceB = SimpleEquatable('simple'); |
| expect(instanceA == instanceB, true); |
| }); |
| |
| test('should return false when compared to non-equatable', () { |
| final instanceA = SimpleEquatable('simple'); |
| final instanceB = NonEquatable(); |
| expect(instanceA == instanceB, false); |
| }); |
| |
| test('should return false when compared to different equatable', () { |
| final instanceA = SimpleEquatable('simple'); |
| final instanceB = OtherEquatable('simple'); |
| expect(instanceA == instanceB, false); |
| }); |
| |
| test('should return false when values are different', () { |
| final instanceA = SimpleEquatable('simple'); |
| final instanceB = SimpleEquatable('Simple'); |
| expect(instanceA == instanceB, false); |
| }); |
| }); |
| |
| group('Simple Equatable (number)', () { |
| test('should correct toString', () { |
| final instance = SimpleEquatable(0); |
| expect(instance.toString(), '[0]'); |
| }); |
| |
| test('should return true when instance is the same', () { |
| final instance = SimpleEquatable(0); |
| expect(instance == instance, true); |
| }); |
| |
| test('should return correct hashCode', () { |
| final instance = SimpleEquatable(0); |
| expect( |
| instance.hashCode, |
| instance.runtimeType.hashCode ^ instance.data.hashCode, |
| ); |
| }); |
| |
| test('should return true when instances are different', () { |
| final instanceA = SimpleEquatable(0); |
| final instanceB = SimpleEquatable(0); |
| expect(instanceA == instanceB, true); |
| }); |
| |
| test('should return false when compared to non-equatable', () { |
| final instanceA = SimpleEquatable(0); |
| final instanceB = NonEquatable(); |
| expect(instanceA == instanceB, false); |
| }); |
| |
| test('should return false when values are different', () { |
| final instanceA = SimpleEquatable(0); |
| final instanceB = SimpleEquatable(1); |
| expect(instanceA == instanceB, false); |
| }); |
| }); |
| |
| group('Simple Equatable (bool)', () { |
| test('should correct toString', () { |
| final instance = SimpleEquatable(true); |
| expect(instance.toString(), '[true]'); |
| }); |
| |
| test('should return true when instance is the same', () { |
| final instance = SimpleEquatable(true); |
| expect(instance == instance, true); |
| }); |
| |
| test('should return correct hashCode', () { |
| final instance = SimpleEquatable(true); |
| expect( |
| instance.hashCode, |
| instance.runtimeType.hashCode ^ instance.data.hashCode, |
| ); |
| }); |
| |
| test('should return true when instances are different', () { |
| final instanceA = SimpleEquatable(true); |
| final instanceB = SimpleEquatable(true); |
| expect(instanceA == instanceB, true); |
| }); |
| |
| test('should return false when compared to non-equatable', () { |
| final instanceA = SimpleEquatable(true); |
| final instanceB = NonEquatable(); |
| expect(instanceA == instanceB, false); |
| }); |
| |
| test('should return false when values are different', () { |
| final instanceA = SimpleEquatable(true); |
| final instanceB = SimpleEquatable(false); |
| expect(instanceA == instanceB, false); |
| }); |
| }); |
| |
| group('Simple Equatable (Equatable)', () { |
| test('should correct toString', () { |
| final instance = SimpleEquatable(EquatableData( |
| key: 'foo', |
| value: 'bar', |
| )); |
| expect(instance.toString(), '[[foo, bar]]'); |
| }); |
| test('should return true when instance is the same', () { |
| final instance = SimpleEquatable(EquatableData( |
| key: 'foo', |
| value: 'bar', |
| )); |
| expect(instance == instance, true); |
| }); |
| |
| test('should return correct hashCode', () { |
| final instance = SimpleEquatable(EquatableData( |
| key: 'foo', |
| value: 'bar', |
| )); |
| expect( |
| instance.hashCode, |
| instance.runtimeType.hashCode ^ instance.data.hashCode, |
| ); |
| }); |
| |
| test('should return true when instances are different', () { |
| final instanceA = SimpleEquatable(EquatableData( |
| key: 'foo', |
| value: 'bar', |
| )); |
| final instanceB = SimpleEquatable(EquatableData( |
| key: 'foo', |
| value: 'bar', |
| )); |
| expect(instanceA == instanceB, true); |
| }); |
| |
| test('should return false when compared to non-equatable', () { |
| final instanceA = SimpleEquatable(EquatableData( |
| key: 'foo', |
| value: 'bar', |
| )); |
| final instanceB = NonEquatable(); |
| expect(instanceA == instanceB, false); |
| }); |
| |
| test('should return false when values are different', () { |
| final instanceA = SimpleEquatable(EquatableData( |
| key: 'foo', |
| value: 'bar', |
| )); |
| final instanceB = SimpleEquatable(EquatableData( |
| key: 'foo', |
| value: 'barz', |
| )); |
| expect(instanceA == instanceB, false); |
| }); |
| }); |
| |
| group('Multipart Equatable', () { |
| test('should correct toString', () { |
| final instance = MultipartEquatable("s1", "s2"); |
| expect(instance.toString(), '[s1, s2]'); |
| }); |
| test('should return true when instance is the same', () { |
| final instance = MultipartEquatable("s1", "s2"); |
| expect(instance == instance, true); |
| }); |
| |
| test('should return correct hashCode', () { |
| final instance = MultipartEquatable("s1", "s2"); |
| expect( |
| instance.hashCode, |
| instance.runtimeType.hashCode ^ |
| instance.d1.hashCode ^ |
| instance.d2.hashCode, |
| ); |
| }); |
| |
| test('should return true when instances are different', () { |
| final instanceA = MultipartEquatable("s1", "s2"); |
| final instanceB = MultipartEquatable("s1", "s2"); |
| expect(instanceA == instanceB, true); |
| }); |
| |
| test('should return false when compared to non-equatable', () { |
| final instanceA = MultipartEquatable("s1", "s2"); |
| final instanceB = NonEquatable(); |
| expect(instanceA == instanceB, false); |
| }); |
| |
| test('should return false when values are different', () { |
| final instanceA = MultipartEquatable("s1", "s2"); |
| final instanceB = MultipartEquatable("s2", "s1"); |
| expect(instanceA == instanceB, false); |
| |
| final instanceC = MultipartEquatable("s1", "s1"); |
| final instanceD = MultipartEquatable("s2", "s1"); |
| expect(instanceC == instanceD, false); |
| }); |
| }); |
| |
| group('Complex Equatable', () { |
| test('should correct toString', () { |
| final instance = ComplexEquatable( |
| name: 'Joe', |
| age: 40, |
| hairColor: Color.black, |
| children: ['Bob'], |
| ); |
| expect(instance.toString(), '[Joe, 40, Color.black, [Bob]]'); |
| }); |
| test('should return true when instance is the same', () { |
| final instance = ComplexEquatable( |
| name: 'Joe', |
| age: 40, |
| hairColor: Color.black, |
| children: ['Bob'], |
| ); |
| expect(instance == instance, true); |
| }); |
| |
| test('should return correct hashCode', () { |
| final instance = ComplexEquatable( |
| name: 'Joe', |
| age: 40, |
| hairColor: Color.black, |
| children: ['Bob'], |
| ); |
| expect( |
| instance.hashCode, |
| instance.runtimeType.hashCode ^ |
| instance.name.hashCode ^ |
| instance.age.hashCode ^ |
| instance.hairColor.hashCode ^ |
| instance.children.hashCode, |
| ); |
| }); |
| |
| test('should return true when instances are different', () { |
| final instanceA = ComplexEquatable( |
| name: 'Joe', |
| age: 40, |
| hairColor: Color.black, |
| children: ['Bob'], |
| ); |
| final instanceB = ComplexEquatable( |
| name: 'Joe', |
| age: 40, |
| hairColor: Color.black, |
| children: ['Bob'], |
| ); |
| expect(instanceA == instanceB, true); |
| }); |
| |
| test('should return false when compared to non-equatable', () { |
| final instanceA = ComplexEquatable( |
| name: 'Joe', |
| age: 40, |
| hairColor: Color.black, |
| children: ['Bob'], |
| ); |
| final instanceB = NonEquatable(); |
| expect(instanceA == instanceB, false); |
| }); |
| |
| test('should return false when values are different', () { |
| final instanceA = ComplexEquatable( |
| name: 'Joe', |
| age: 40, |
| hairColor: Color.black, |
| children: ['Bob'], |
| ); |
| final instanceB = ComplexEquatable( |
| name: 'John', |
| age: 40, |
| hairColor: Color.brown, |
| children: ['Bobby'], |
| ); |
| expect(instanceA == instanceB, false); |
| }); |
| }); |
| } |