Merge branch 'soru/equal-operator-on-receipt' into 'main'

feat: Add == operator to the Receipt, User and Room objects

See merge request famedly/famedlysdk!571
This commit is contained in:
Sorunome 2020-12-19 11:18:49 +00:00
commit 155b47428a
4 changed files with 44 additions and 0 deletions

View File

@ -1730,4 +1730,7 @@ class Room {
TombstoneContent get extinctInformations => isExtinct
? getState(EventTypes.RoomTombstone).parsedTombstoneContent
: null;
@override
bool operator ==(dynamic other) => (other is Room && other.id == id);
}

View File

@ -175,4 +175,10 @@ class User extends Event {
/// Please be aware that you can only set the power level to at least your own!
bool get canChangePowerLevel =>
room.canChangePowerLevel && powerLevel < room.ownPowerLevel;
@override
bool operator ==(dynamic other) => (other is User &&
other.id == id &&
other.room == room &&
other.membership == membership);
}

View File

@ -7,4 +7,9 @@ class Receipt {
final DateTime time;
const Receipt(this.user, this.time);
@override
bool operator ==(dynamic other) => (other is Receipt &&
other.user == user &&
other.time.microsecondsSinceEpoch == time.microsecondsSinceEpoch);
}

View File

@ -453,6 +453,36 @@ void main() {
await client.dispose(closeDatabase: true);
});
test('object equality', () async {
final time1 = DateTime.fromMillisecondsSinceEpoch(1);
final time2 = DateTime.fromMillisecondsSinceEpoch(0);
final user1 = User('@user1:example.org', room: Room(id: '!room1'));
final user2 = User('@user2:example.org', room: Room(id: '!room1'));
// receipts
expect(Receipt(user1, time1) == Receipt(user1, time1), true);
expect(Receipt(user1, time1) == Receipt(user1, time2), false);
expect(Receipt(user1, time1) == Receipt(user2, time1), false);
// ignore: unrelated_type_equality_checks
expect(Receipt(user1, time1) == 'beep', false);
// users
expect(user1 == user1, true);
expect(user1 == user2, false);
expect(
user1 == User('@user1:example.org', room: Room(id: '!room2')), false);
expect(
user1 ==
User('@user1:example.org',
room: Room(id: '!room1'), membership: 'leave'),
false);
// ignore: unrelated_type_equality_checks
expect(user1 == 'beep', false);
// rooms
expect(Room(id: '!room1') == Room(id: '!room1'), true);
expect(Room(id: '!room1') == Room(id: '!room2'), false);
// ignore: unrelated_type_equality_checks
expect(Room(id: '!room1') == 'beep', false);
});
test('dispose', () async {
await matrix.dispose(closeDatabase: true);
});