diff --git a/lib/src/note/positioned_note.dart b/lib/src/note/positioned_note.dart index 69383d80..a330bae9 100644 --- a/lib/src/note/positioned_note.dart +++ b/lib/src/note/positioned_note.dart @@ -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([ diff --git a/test/src/note/positioned_note_test.dart b/test/src/note/positioned_note_test.dart index d5a4eaa8..8ad03f04 100644 --- a/test/src/note/positioned_note_test.dart +++ b/test/src/note/positioned_note_test.dart @@ -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), ]); });