From 295528904f33e21820598de69251bc6fa460a993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Ma=C3=B1osa?= Date: Fri, 14 Apr 2023 20:21:51 +0200 Subject: [PATCH] feat(accidental): add `name` getter (#47) * feat(accidental): add `name` getter * test(accidental): add test cases for `name` * fix(accidental): remove unnecessary empty statement --- lib/src/note/accidental.dart | 27 +++++++++++++++++++++++++-- test/src/note/accidental_test.dart | 28 +++++++++++++++++++++------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/lib/src/note/accidental.dart b/lib/src/note/accidental.dart index d6b4c014..068ff8c0 100644 --- a/lib/src/note/accidental.dart +++ b/lib/src/note/accidental.dart @@ -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; @@ -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) => diff --git a/test/src/note/accidental_test.dart b/test/src/note/accidental_test.dart index f99f4df6..dc5f542f 100644 --- a/test/src/note/accidental_test.dart +++ b/test/src/note/accidental_test.dart @@ -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, '♯𝄪𝄪'); @@ -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)'); });