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

fix: use SplayTreeSet to resolve inconsistent hashCode #142

Merged
merged 5 commits into from Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/src/equatable_utils.dart
@@ -1,3 +1,5 @@
import 'dart:collection';

import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';

Expand Down Expand Up @@ -46,6 +48,11 @@ int _combine(int hash, dynamic object) {
});
return hash;
}
if(object is Set) {
felangel marked this conversation as resolved.
Show resolved Hide resolved
// this is needed to have a consistent iteration order so that the produced
// hash is consistent independently of the Set insertion order
object = SplayTreeSet<dynamic>.from(object);
}
if (object is Iterable) {
for (final value in object) {
hash = hash ^ _combine(hash, value);
Expand Down
11 changes: 11 additions & 0 deletions test/equatable_test.dart
Expand Up @@ -968,6 +968,17 @@ void main() {
expect(instanceA.hashCode == instanceB.hashCode, true);
});

test('should return when Set values are same but in different order', () {
final instanceA = SimpleEquatable<Set<String>>(
Set.from(<String>['A', 'B']),
);
final instanceB = SimpleEquatable<Set<String>>(
Set.from(<String>['B', 'A']),
);
expect(instanceA == instanceB, true);
expect(instanceA.hashCode == instanceB.hashCode, true);
});

test('should return when values are different', () {
final instanceA = SimpleEquatable<Set<String>>(
Set.from(<String>['A', 'B']),
Expand Down