Skip to content

Commit

Permalink
feat(accidental): add name getter (#47)
Browse files Browse the repository at this point in the history
* feat(accidental): add `name` getter

* test(accidental): add test cases for `name`

* fix(accidental): remove unnecessary empty statement
  • Loading branch information
albertms10 committed Apr 14, 2023
1 parent 4a22409 commit 2955289
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
27 changes: 25 additions & 2 deletions lib/src/note/accidental.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,31 @@ class Accidental implements MusicItem {
static const String _flatSymbol = '♭';
static const String _doubleFlatSymbol = '𝄫';

/// Returns the symbol of this [Accidental].
/// The name of this [Accidental].
///
/// Example:
/// ```dart
/// Accidental.sharp.name == 'Sharp'
/// Accidental.doubleFlat.name == 'Double flat'
/// Accidental.natural.name == 'Natural'
/// ```
String? get name => const {
3: 'Triple sharp',
2: 'Double sharp',
1: 'Sharp',
0: 'Natural',
-1: 'Flat',
-2: 'Double flat',
-3: 'Triple flat',
}[semitones];

/// The symbol of this [Accidental].
///
/// Example:
/// ```dart
/// Accidental.flat.symbol == '♭'
/// Accidental.doubleSharp.symbol == '𝄪'
/// Accidental.natural.symbol == '♮'
/// ```
String get symbol {
if (semitones == 0) return _naturalSymbol;
Expand All @@ -57,7 +76,11 @@ class Accidental implements MusicItem {
Accidental((semitones.abs() + n) * (semitones >= 0 ? 1 : -1));

@override
String toString() => '$symbol (${semitones.toDeltaString()})';
String toString() => [
if (name != null) name,
symbol,
'(${semitones.toDeltaString()})',
].join(' ');

@override
bool operator ==(Object other) =>
Expand Down
28 changes: 21 additions & 7 deletions test/src/note/accidental_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ void main() {
});
});

group('.name', () {
test('should return the name of this Accidental', () {
expect(const Accidental(4).name, isNull);
expect(Accidental.tripleSharp.name, 'Triple sharp');
expect(Accidental.doubleSharp.name, 'Double sharp');
expect(Accidental.sharp.name, 'Sharp');
expect(Accidental.natural.name, 'Natural');
expect(Accidental.flat.name, 'Flat');
expect(Accidental.doubleFlat.name, 'Double flat');
expect(Accidental.tripleFlat.name, 'Triple flat');
expect(const Accidental(-4).name, isNull);
});
});

group('.symbol', () {
test('should return the symbol string of this Accidental', () {
expect(const Accidental(5).symbol, '♯𝄪𝄪');
Expand Down Expand Up @@ -61,13 +75,13 @@ void main() {
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(Accidental.tripleSharp.toString(), 'Triple sharp ♯𝄪 (+3)');
expect(Accidental.doubleSharp.toString(), 'Double sharp 𝄪 (+2)');
expect(Accidental.sharp.toString(), 'Sharp ♯ (+1)');
expect(Accidental.natural.toString(), 'Natural ♮ (+0)');
expect(Accidental.flat.toString(), 'Flat ♭ (-1)');
expect(Accidental.doubleFlat.toString(), 'Double flat 𝄫 (-2)');
expect(Accidental.tripleFlat.toString(), 'Triple flat ♭𝄫 (-3)');
expect(const Accidental(-4).toString(), '𝄫𝄫 (-4)');
expect(const Accidental(-5).toString(), '♭𝄫𝄫 (-5)');
});
Expand Down

0 comments on commit 2955289

Please sign in to comment.