Skip to content

Commit

Permalink
fix(enharmonic_note): address edge cases for items getter (#23)
Browse files Browse the repository at this point in the history
* fix(enharmonic_note): address edge cases for `items` getter

* test(enharmonic_note): add test cases for `items` getter

* feat(accidental): override `toString` method and add test cases
  • Loading branch information
albertms10 committed Apr 8, 2023
1 parent 8d3ff21 commit 4ab2a7c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lib/src/note/accidental.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class Accidental {
(semitones.abs() ~/ 2);
}

@override
String toString() => '$symbol ($semitones)';

@override
bool operator ==(Object other) =>
other is Accidental && semitones == other.semitones;
Expand Down
22 changes: 13 additions & 9 deletions lib/src/note/enharmonic_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ class EnharmonicNote extends Enharmonic<Note> {
final note = Notes.fromValue(semitones);

if (note != null) {
final noteBelow = Notes.fromOrdinal(Notes.values.indexOf(note));
final noteAbove = Notes.fromOrdinal(Notes.values.indexOf(note) + 2);
final noteBelow = Notes.fromOrdinal(note.ordinal - 1);
final noteAbove = Notes.fromOrdinal(note.ordinal + 1);

return SplayTreeSet<Note>.from({
return {
Note(
noteBelow,
Accidental(note.value - noteBelow.value),
Accidental((note.value - noteBelow.value).chromaticModExcludeZero),
),
Note(note),
Note(
noteAbove,
Accidental(note.value - noteAbove.value),
Accidental(
note.value -
noteAbove.value -
(note.value > noteAbove.value ? chromaticDivisions : 0),
),
),
});
};
}

return SplayTreeSet<Note>.from({
return {
Note(
Notes.fromValue(semitones - 1)!,
Accidental.sharp,
Expand All @@ -46,7 +50,7 @@ class EnharmonicNote extends Enharmonic<Note> {
Notes.fromValue(semitones + 1)!,
Accidental.flat,
),
});
};
}

/// Returns the [Note] from [semitones] and a [preferredAccidental].
Expand Down Expand Up @@ -75,7 +79,7 @@ class EnharmonicNote extends Enharmonic<Note> {
/// ```
@override
EnharmonicNote transposeBy(int semitones) =>
EnharmonicNote(this.semitones + semitones);
EnharmonicNote((this.semitones + semitones).chromaticModExcludeZero);

/// Returns the shortest fifths distance between this and [other].
///
Expand Down
16 changes: 16 additions & 0 deletions test/src/note/accidental_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ void main() {
});
});

group('.toString()', () {
test('should return the string representation of this Accidental', () {
expect(const Accidental(5).toString(), '♯𝄪𝄪 (5)');
expect(const Accidental(4).toString(), '𝄪𝄪 (4)');
expect(Accidental.tripleSharp.toString(), '♯𝄪 (3)');
expect(Accidental.doubleSharp.toString(), '𝄪 (2)');
expect(Accidental.sharp.toString(), '♯ (1)');
expect(Accidental.natural.toString(), '♮ (0)');
expect(Accidental.flat.toString(), '♭ (-1)');
expect(Accidental.doubleFlat.toString(), '𝄫 (-2)');
expect(Accidental.tripleFlat.toString(), '♭𝄫 (-3)');
expect(const Accidental(-4).toString(), '𝄫𝄫 (-4)');
expect(const Accidental(-5).toString(), '♭𝄫𝄫 (-5)');
});
});

group('.hashCode', () {
test('should ignore equal Accidental instances in a Set', () {
final collection = {
Expand Down
45 changes: 45 additions & 0 deletions test/src/note/enharmonic_note_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,51 @@ import 'package:test/test.dart';

void main() {
group('EnharmonicNote', () {
group('.items', () {
test('should return the correct notes for this EnharmonicNote', () {
expect(EnharmonicNote.c.items, {
const Note(Notes.b, Accidental.sharp),
Note.c,
const Note(Notes.d, Accidental.doubleFlat),
});
expect(EnharmonicNote.cSharp.items, {Note.cSharp, Note.dFlat});
expect(EnharmonicNote.d.items, {
const Note(Notes.c, Accidental.doubleSharp),
Note.d,
const Note(Notes.e, Accidental.doubleFlat),
});
expect(EnharmonicNote.dSharp.items, {Note.dSharp, Note.eFlat});
expect(EnharmonicNote.e.items, {
const Note(Notes.d, Accidental.doubleSharp),
Note.e,
const Note(Notes.f, Accidental.flat),
});
expect(EnharmonicNote.f.items, {
const Note(Notes.e, Accidental.sharp),
Note.f,
const Note(Notes.g, Accidental.doubleFlat),
});
expect(EnharmonicNote.fSharp.items, {Note.fSharp, Note.gFlat});
expect(EnharmonicNote.g.items, {
const Note(Notes.f, Accidental.doubleSharp),
Note.g,
const Note(Notes.a, Accidental.doubleFlat),
});
expect(EnharmonicNote.gSharp.items, {Note.gSharp, Note.aFlat});
expect(EnharmonicNote.a.items, {
const Note(Notes.g, Accidental.doubleSharp),
Note.a,
const Note(Notes.b, Accidental.doubleFlat),
});
expect(EnharmonicNote.aSharp.items, {Note.aSharp, Note.bFlat});
expect(EnharmonicNote.b.items, {
const Note(Notes.a, Accidental.doubleSharp),
Note.b,
const Note(Notes.c, Accidental.flat),
});
});
});

group('.shortestFifthsDistance()', () {
test('should return the shortest fifths distance from other', () {
expect(EnharmonicNote.c.shortestFifthsDistance(EnharmonicNote.c), 0);
Expand Down

0 comments on commit 4ab2a7c

Please sign in to comment.