| import 'dart:convert'; |
| |
| import 'package:test/test.dart'; |
| |
| import 'package:equatable/equatable.dart'; |
| |
| class NonEquatable {} |
| |
| class EmptyEquatable extends Equatable {} |
| |
| class SimpleEquatable<T> extends Equatable { |
| final T data; |
| |
| SimpleEquatable(this.data) : super([data]); |
| } |
| |
| class MultipartEquatable<T> extends Equatable { |
| final T d1; |
| final T d2; |
| |
| MultipartEquatable(this.d1, this.d2) : super([d1, d2]); |
| } |
| |
| class OtherEquatable extends Equatable { |
| final String data; |
| |
| OtherEquatable(this.data) : super([data]); |
| } |
| |
| enum Color { blonde, black, brown } |
| |
| class ComplexEquatable extends Equatable { |
| final String name; |
| final int age; |
| final Color hairColor; |
| final List<String> children; |
| |
| ComplexEquatable({this.name, this.age, this.hairColor, this.children}) |
| : super([name, age, hairColor, children]); |
| } |
| |
| class EquatableData extends Equatable { |
| final String key; |
| final dynamic value; |
| |
| EquatableData({this.key, this.value}) : super([key, value]); |
| } |
| |
| class Credentials extends Equatable { |
| String username; |
| String password; |
| |
| Credentials({this.username, this.password}) : super([username, password]); |
| |
| Credentials.fromJson(Map<String, dynamic> json) : super([json]) { |
| username = json['username'].toString(); |
| password = json['password'].toString(); |
| } |
| |
| Map<String, dynamic> toJson() { |
| final Map<String, dynamic> data = new Map<String, dynamic>(); |
| data['username'] = this.username; |
| data['password'] = this.password; |
| return data; |
| } |
| } |
| |
| 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); |
| expect(instanceA.hashCode == instanceB.hashCode, 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); |
| expect(instanceA.hashCode == instanceB.hashCode, 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); |
| expect(instanceA.hashCode == instanceB.hashCode, 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); |
| expect(instanceA.hashCode == instanceB.hashCode, 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); |
| expect(instanceA.hashCode == instanceB.hashCode, 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); |
| expect(instanceA.hashCode == instanceB.hashCode, 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[0].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); |
| expect(instanceA.hashCode == instanceB.hashCode, 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); |
| }); |
| }); |
| |
| group('From Json Equatable', () { |
| test('should correct json', () { |
| final credentialsA = Credentials.fromJson(json.decode( |
| """ |
| { |
| "username":"Admin", |
| "password":"admin" |
| } |
| """, |
| ) as Map<String, dynamic>); |
| final credentialsB = Credentials.fromJson(json.decode( |
| """ |
| { |
| "username":"Guest", |
| "password":"guest" |
| } |
| """, |
| ) as Map<String, dynamic>); |
| expect(credentialsA == credentialsA, true); |
| expect(credentialsB, credentialsB); |
| expect(credentialsA == credentialsB, false); |
| }); |
| }); |
| } |