Skip to content

Commit

Permalink
fix(positioned_note): use correct hashCode values (#155)
Browse files Browse the repository at this point in the history
* fix(positioned_note): use correct `hashCode` values

Closes #106

* test(positioned_note): add explicit `hashCode` cases
  • Loading branch information
albertms10 committed May 27, 2023
1 parent 50535fa commit 334bcde
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/src/note/positioned_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ final class PositionedNote
other is PositionedNote && note == other.note && octave == other.octave;

@override
int get hashCode => Object.hash(super.hashCode, octave);
int get hashCode => Object.hash(note, octave);

@override
int compareTo(PositionedNote other) => compareMultiple([
Expand Down
28 changes: 28 additions & 0 deletions test/src/note/positioned_note_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -690,16 +690,44 @@ void main() {
});

group('.hashCode', () {
test('should return the same hashCode for equal PositionedNotes', () {
expect(Note.c.inOctave(4).hashCode, Note.c.inOctave(4).hashCode);
expect(
const PositionedNote(Note.a, 3).hashCode,
const PositionedNote(Note.a, 3).hashCode,
);
});

test(
'should return different hashCodes for different PositionedNotes',
() {
expect(
Note.c.inOctave(4).hashCode,
isNot(equals(Note.c.inOctave(5).hashCode)),
);
expect(
const PositionedNote(Note.a, 3).hashCode,
isNot(equals(const PositionedNote(Note.b, 3).hashCode)),
);
expect(
Note.d.inOctave(6).hashCode,
isNot(equals(Note.c.inOctave(5).hashCode)),
);
},
);

test('should ignore equal PositionedNote instances in a Set', () {
final collection = {
Note.c.inOctave(4),
Note.a.flat.inOctave(2),
Note.d.inOctave(4),
Note.g.sharp.inOctave(5),
};
collection.addAll(collection);
expect(collection.toList(), [
Note.c.inOctave(4),
Note.a.flat.inOctave(2),
Note.d.inOctave(4),
Note.g.sharp.inOctave(5),
]);
});
Expand Down

0 comments on commit 334bcde

Please sign in to comment.