Skip to content
This repository has been archived by the owner on Dec 6, 2017. It is now read-only.

fix(Key): made key equality asymmetrical #96

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion lib/key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Key {
final int id;

factory Key(Type type, [Type annotation]) {
var _hashCode = type.hashCode + annotation.hashCode;
var _hashCode = type.hashCode + 2339 * annotation.hashCode;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can still have the same hash code for different keys with this code.
The solution is to never assume that hash code eq means object eq

var _id = _hashToKey.putIfAbsent(_hashCode, () => _lastKeyId++);
return new Key._newKey(type, annotation, _hashCode, _id);
}
Expand Down
21 changes: 1 addition & 20 deletions test/fixed-unittest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,7 @@ Matcher toThrow(Type exceptionClass, [message]) => message == null
instanceOf(exceptionClass),
message is Matcher ? message : toContain(message)));

Matcher not(Matcher matcher) => new NegateMatcher(matcher);


class NegateMatcher extends Matcher {
final Matcher _matcher;

const NegateMatcher(this._matcher);

bool matches(obj, Map ms) => !_matcher.matches(obj, ms);

Description describe(Description description) =>
_matcher.describe(description.add('NOT'));

Description describeMismatch(item, Description mismatchDescription,
Map matchState, bool verbose) {
return _matcher.describeMismatch(
item, mismatchDescription, matchState, verbose);
}
}

Matcher not(Matcher matcher) => isNot(matcher);

class ThrowsMatcher extends Throws {
final Matcher _matcher;
Expand Down
7 changes: 7 additions & 0 deletions test/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -856,5 +856,12 @@ createKeySpec() {
expect(k1, not(equals(k2)));
expect(k1.hashCode, not(equals(k2.hashCode)));
});

it('should not be symmetrically equal', () {
Key k1 = new Key(Engine, Old);
Key k2 = new Key(Old, Engine);
expect(k1, not(equals(k2)));
expect(k1.hashCode, not(equals(k2.hashCode)));
});
});
}