Skip to content

Commit

Permalink
refactor(enharmonic_note): rename toNoteresolveSpelling
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed May 7, 2023
1 parent b1ae042 commit 8f19be5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 65 deletions.
25 changes: 13 additions & 12 deletions lib/src/note/enharmonic_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ class EnharmonicNote extends Enharmonic<Note>
///
/// Example:
/// ```dart
/// EnharmonicNote.d.toNote() == Note.d
/// EnharmonicNote.fSharp.toNote(Accidental.flat) == Note.gFlat
/// EnharmonicNote.cSharp.toNote(Accidental.natural) // throws
/// EnharmonicNote.d.resolveSpelling() == Note.d
/// EnharmonicNote.fSharp.resolveSpelling(Accidental.flat) == Note.gFlat
/// EnharmonicNote.cSharp.resolveSpelling(Accidental.natural) // throws
/// ```
Note toNote([Accidental? withAccidental]) {
Note resolveSpelling([Accidental? withAccidental]) {
final matchedNote = spellings.firstWhereOrNull(
(note) => note.accidental == withAccidental,
);
Expand All @@ -97,21 +97,22 @@ class EnharmonicNote extends Enharmonic<Note>
/// Returns the [Note] that matches with [preferredAccidental] from this
/// [EnharmonicNote].
///
/// Like [toNote] except that this function returns the closest note where a
/// similar call to [toNote] would throw an [ArgumentError].
/// Like [resolveSpelling] except that this function returns the closest note
/// where a similar call to [resolveSpelling] would throw an [ArgumentError].
///
/// Example:
/// ```dart
/// EnharmonicNote.d.toClosestNote() == Note.d
/// EnharmonicNote.gSharp.toClosestNote(Accidental.flat) == Note.aFlat
/// EnharmonicNote.cSharp.toClosestNote(Accidental.natural) == null
/// EnharmonicNote.d.resolveClosestSpelling() == Note.d
/// EnharmonicNote.gSharp.resolveClosestSpelling(Accidental.flat)
/// == Note.aFlat
/// EnharmonicNote.cSharp.resolveClosestSpelling(Accidental.natural) == null
/// ```
Note toClosestNote([Accidental? preferredAccidental]) {
Note resolveClosestSpelling([Accidental? preferredAccidental]) {
try {
return toNote(preferredAccidental);
return resolveSpelling(preferredAccidental);
// ignore: avoid_catching_errors
} on ArgumentError {
return toNote();
return resolveSpelling();
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/note/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ class Note implements MusicItem, Transposable<Note> {
var distance = 0;
var currentPitch = this.semitones;

var tempNote =
EnharmonicNote(currentPitch).toClosestNote(preferredAccidental);
var tempNote = EnharmonicNote(currentPitch)
.resolveClosestSpelling(preferredAccidental);

while (tempNote != other && distance < chromaticDivisions) {
distance++;
currentPitch += semitones;
tempNote = EnharmonicNote(currentPitch.chromaticModExcludeZero)
.toClosestNote(preferredAccidental);
.resolveClosestSpelling(preferredAccidental);
}

return distance;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tonality/key_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class KeySignature implements Comparable<KeySignature> {

return EnharmonicNote(
(fifthInterval.semitones * accidentals + 1).chromaticModExcludeZero,
).toClosestNote(accidental.increment(accidentals ~/ 9));
).resolveClosestSpelling(accidental.increment(accidentals ~/ 9));
}

/// Returns the [Tonality] that corresponds to this [KeySignature] from
Expand Down
116 changes: 67 additions & 49 deletions test/src/note/enharmonic_note_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,92 +63,107 @@ void main() {
);
});

group('.toNote()', () {
group('.resolveSpelling()', () {
test(
'should return the Note that matches with the accidental',
() {
expect(EnharmonicNote.c.toNote(), Note.c);
expect(EnharmonicNote.c.resolveSpelling(), Note.c);
expect(
EnharmonicNote.c.toNote(Accidental.sharp),
EnharmonicNote.c.resolveSpelling(Accidental.sharp),
const Note(Notes.b, Accidental.sharp),
);
expect(
EnharmonicNote.c.toNote(Accidental.doubleFlat),
EnharmonicNote.c.resolveSpelling(Accidental.doubleFlat),
const Note(Notes.d, Accidental.doubleFlat),
);

expect(EnharmonicNote.cSharp.toNote(), Note.cSharp);
expect(EnharmonicNote.cSharp.toNote(Accidental.flat), Note.dFlat);
expect(EnharmonicNote.cSharp.resolveSpelling(), Note.cSharp);
expect(
EnharmonicNote.cSharp.resolveSpelling(Accidental.flat),
Note.dFlat,
);

expect(EnharmonicNote.d.toNote(), Note.d);
expect(EnharmonicNote.d.resolveSpelling(), Note.d);
expect(
EnharmonicNote.d.toNote(Accidental.doubleSharp),
EnharmonicNote.d.resolveSpelling(Accidental.doubleSharp),
const Note(Notes.c, Accidental.doubleSharp),
);
expect(
EnharmonicNote.d.toNote(Accidental.doubleFlat),
EnharmonicNote.d.resolveSpelling(Accidental.doubleFlat),
const Note(Notes.e, Accidental.doubleFlat),
);

expect(EnharmonicNote.dSharp.toNote(), Note.dSharp);
expect(EnharmonicNote.dSharp.toNote(Accidental.flat), Note.eFlat);
expect(EnharmonicNote.dSharp.resolveSpelling(), Note.dSharp);
expect(
EnharmonicNote.dSharp.resolveSpelling(Accidental.flat),
Note.eFlat,
);

expect(EnharmonicNote.e.toNote(), Note.e);
expect(EnharmonicNote.e.resolveSpelling(), Note.e);
expect(
EnharmonicNote.e.toNote(Accidental.doubleSharp),
EnharmonicNote.e.resolveSpelling(Accidental.doubleSharp),
const Note(Notes.d, Accidental.doubleSharp),
);
expect(
EnharmonicNote.e.toNote(Accidental.flat),
EnharmonicNote.e.resolveSpelling(Accidental.flat),
const Note(Notes.f, Accidental.flat),
);

expect(EnharmonicNote.f.toNote(), Note.f);
expect(EnharmonicNote.f.resolveSpelling(), Note.f);
expect(
EnharmonicNote.f.toNote(Accidental.sharp),
EnharmonicNote.f.resolveSpelling(Accidental.sharp),
const Note(Notes.e, Accidental.sharp),
);
expect(
EnharmonicNote.f.toNote(Accidental.doubleFlat),
EnharmonicNote.f.resolveSpelling(Accidental.doubleFlat),
const Note(Notes.g, Accidental.doubleFlat),
);

expect(EnharmonicNote.fSharp.toNote(), Note.fSharp);
expect(EnharmonicNote.fSharp.toNote(Accidental.flat), Note.gFlat);
expect(EnharmonicNote.fSharp.resolveSpelling(), Note.fSharp);
expect(
EnharmonicNote.fSharp.resolveSpelling(Accidental.flat),
Note.gFlat,
);

expect(EnharmonicNote.g.toNote(), Note.g);
expect(EnharmonicNote.g.resolveSpelling(), Note.g);
expect(
EnharmonicNote.g.toNote(Accidental.doubleSharp),
EnharmonicNote.g.resolveSpelling(Accidental.doubleSharp),
const Note(Notes.f, Accidental.doubleSharp),
);
expect(
EnharmonicNote.g.toNote(Accidental.doubleFlat),
EnharmonicNote.g.resolveSpelling(Accidental.doubleFlat),
const Note(Notes.a, Accidental.doubleFlat),
);

expect(EnharmonicNote.gSharp.toNote(), Note.gSharp);
expect(EnharmonicNote.gSharp.toNote(Accidental.flat), Note.aFlat);
expect(EnharmonicNote.gSharp.resolveSpelling(), Note.gSharp);
expect(
EnharmonicNote.gSharp.resolveSpelling(Accidental.flat),
Note.aFlat,
);

expect(EnharmonicNote.a.toNote(), Note.a);
expect(EnharmonicNote.a.resolveSpelling(), Note.a);
expect(
EnharmonicNote.a.toNote(Accidental.doubleSharp),
EnharmonicNote.a.resolveSpelling(Accidental.doubleSharp),
const Note(Notes.g, Accidental.doubleSharp),
);
expect(
EnharmonicNote.a.toNote(Accidental.doubleFlat),
EnharmonicNote.a.resolveSpelling(Accidental.doubleFlat),
const Note(Notes.b, Accidental.doubleFlat),
);

expect(EnharmonicNote.aSharp.toNote(), Note.aSharp);
expect(EnharmonicNote.aSharp.toNote(Accidental.flat), Note.bFlat);
expect(EnharmonicNote.aSharp.resolveSpelling(), Note.aSharp);
expect(
EnharmonicNote.aSharp.resolveSpelling(Accidental.flat),
Note.bFlat,
);

expect(EnharmonicNote.b.toNote(), Note.b);
expect(EnharmonicNote.b.resolveSpelling(), Note.b);
expect(
EnharmonicNote.b.toNote(Accidental.doubleSharp),
EnharmonicNote.b.resolveSpelling(Accidental.doubleSharp),
const Note(Notes.a, Accidental.doubleSharp),
);
expect(
EnharmonicNote.b.toNote(Accidental.flat),
EnharmonicNote.b.resolveSpelling(Accidental.flat),
const Note(Notes.c, Accidental.flat),
);
},
Expand All @@ -159,64 +174,67 @@ void main() {
'any Note',
() {
expect(
() => EnharmonicNote.cSharp.toNote(Accidental.natural),
() => EnharmonicNote.cSharp.resolveSpelling(Accidental.natural),
throwsArgumentError,
);
expect(
() => EnharmonicNote.c.toNote(Accidental.flat),
() => EnharmonicNote.c.resolveSpelling(Accidental.flat),
throwsArgumentError,
);
expect(
() => EnharmonicNote.d.toNote(Accidental.sharp),
() => EnharmonicNote.d.resolveSpelling(Accidental.sharp),
throwsArgumentError,
);
expect(
() => EnharmonicNote.a.toNote(Accidental.tripleFlat),
() => EnharmonicNote.a.resolveSpelling(Accidental.tripleFlat),
throwsArgumentError,
);
},
);
});

group('.toClosestNote()', () {
group('.resolveClosestSpelling()', () {
test(
'should return the Note that matches with the preferred accidental',
() {
expect(EnharmonicNote.c.toClosestNote(), Note.c);
expect(EnharmonicNote.c.resolveClosestSpelling(), Note.c);
expect(
EnharmonicNote.c.toClosestNote(Accidental.sharp),
EnharmonicNote.c.resolveClosestSpelling(Accidental.sharp),
const Note(Notes.b, Accidental.sharp),
);
expect(
EnharmonicNote.c.toClosestNote(Accidental.doubleFlat),
EnharmonicNote.c.resolveClosestSpelling(Accidental.doubleFlat),
const Note(Notes.d, Accidental.doubleFlat),
);

expect(EnharmonicNote.cSharp.toClosestNote(), Note.cSharp);
expect(EnharmonicNote.cSharp.resolveClosestSpelling(), Note.cSharp);
expect(
EnharmonicNote.cSharp.toClosestNote(Accidental.flat),
EnharmonicNote.cSharp.resolveClosestSpelling(Accidental.flat),
Note.dFlat,
);

// ... Similar to `.toNote()`.
// ... Similar to `.resolveSpelling()`.
},
);

test(
'should return the closest Note where a similar call to .toNote() '
'would throw',
'should return the closest Note where a similar call to '
'.resolveSpelling() would throw',
() {
expect(
EnharmonicNote.cSharp.toClosestNote(Accidental.natural),
EnharmonicNote.cSharp.resolveClosestSpelling(Accidental.natural),
Note.cSharp,
);
expect(EnharmonicNote.c.toClosestNote(Accidental.flat), Note.c);
expect(
EnharmonicNote.d.toClosestNote(Accidental.sharp),
EnharmonicNote.c.resolveClosestSpelling(Accidental.flat),
Note.c,
);
expect(
EnharmonicNote.d.resolveClosestSpelling(Accidental.sharp),
Note.d,
);
expect(
EnharmonicNote.a.toClosestNote(Accidental.tripleFlat),
EnharmonicNote.a.resolveClosestSpelling(Accidental.tripleFlat),
Note.a,
);
},
Expand Down

0 comments on commit 8f19be5

Please sign in to comment.