Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashDTO issue on equal operator #372

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/identity/data/dtos/hash_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class HashDTO extends Equatable {
@override
bool operator ==(Object other) {
if (other is HashDTO) {
if (data.length != other.data.length) {
return false;
}
for (int i = 0; i < data.length; i++) {
if (data[i] != other.data[i]) {
return false;
Expand Down
55 changes: 55 additions & 0 deletions test/identity/data/dtos/hash_dto_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:polygonid_flutter_sdk/identity/data/dtos/hash_dto.dart';
import 'dart:typed_data';

void main() {
group('HashDTO', () {
test('== operator should return true when data fields are equal', () {
final data1 = Uint8List.fromList([1, 2, 3, 4, 5]);
final hashDTO1 = HashDTO(data: data1);

final data2 = Uint8List.fromList([1, 2, 3, 4, 5]);
final hashDTO2 = HashDTO(data: data2);

expect(hashDTO1 == hashDTO2, isTrue);
});

test('== operator should return false when data fields are not equal', () {
final data1 = Uint8List.fromList([1, 2, 3, 4, 5]);
final hashDTO1 = HashDTO(data: data1);

final data2 = Uint8List.fromList([6, 7, 8, 9, 10]);
final hashDTO2 = HashDTO(data: data2);

expect(hashDTO1 == hashDTO2, isFalse);
});

test(
'== operator should return false when data fields have different lengths',
() {
final data1 = Uint8List.fromList([1, 2, 3, 4, 5]);
final hashDTO1 = HashDTO(data: data1);

final data2 = Uint8List.fromList([1, 2, 3, 4, 5, 6]);
final hashDTO2 = HashDTO(data: data2);

Copy link
Contributor

@jrl351 jrl351 Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness you should probably also test when the left side has more items than the right side.

expect(hashDTO1 == hashDTO2, isFalse);
});

test('toBigInt should return correct BigInt value', () {
final data = Uint8List.fromList([1, 2, 3, 4, 5]);
final hashDTO = HashDTO(data: data);

final expectedBigInt = BigInt.parse('21542142465');
expect(hashDTO.toBigInt(), equals(expectedBigInt));
});

test('testBit should return correct bit value', () {
final data = Uint8List.fromList([1, 2, 3, 4, 5]);
final hashDTO = HashDTO(data: data);

expect(hashDTO.testBit(0), isTrue);
expect(hashDTO.testBit(1), isFalse);
});
});
}
Loading