From: Scott Gasch Date: Fri, 22 Apr 2022 16:42:08 +0000 (-0700) Subject: Add a chord parser test. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=711a9fd699f7c724f0aab7c1c43511cfbf7b2281;p=python_utils.git Add a chord parser test. --- diff --git a/music/chords.py b/music/chords.py index fc2bd5f..ff4b540 100755 --- a/music/chords.py +++ b/music/chords.py @@ -43,7 +43,6 @@ def degree_of_note(root_note: str, target_note: str) -> Optional[int]: root_note = root_note.upper() target_note = target_note.upper() for degree, note in enumerate(itertools.islice(generate_scale(root_note), 24)): - print(f'"{target_note}", "{note}", {degree}') if note == target_note: return degree - 1 return None @@ -98,6 +97,9 @@ class Chord: self.root_note = root_note.upper() self.other_notes = other_notes + def __repr__(self): + return f'root={self.root_note}, others={self.other_notes}' + @decorator_utils.decorate_matching_methods_with( debug_parse, @@ -213,8 +215,6 @@ class ChordParser(chordsListener): def exitParse(self, ctx: chordsParser.ParseContext) -> None: """Populate self.chord""" - print(f'Root note is a {self.rootNote}') - scale = list(itertools.islice(generate_scale(self.rootNote), 24)) chord_types_with_perfect_5th = set( [ @@ -257,12 +257,8 @@ class ChordParser(chordsListener): other_notes['min7'] = 10 other_notes[expression] = semitone_count - for expression, semitone_count in other_notes.items(): - note_name = scale[semitone_count] - print(f'Contains: {expression} ({semitone_count} semitones) => {note_name}') if self.bassNote: degree = degree_of_note(self.rootNote, self.bassNote) - print(f'Add a {self.bassNote} ({degree}) in the bass') other_notes[self.bassNote] = degree self.chord = Chord(self.rootNote, other_notes) diff --git a/tests/chords_test.py b/tests/chords_test.py new file mode 100755 index 0000000..6776f3f --- /dev/null +++ b/tests/chords_test.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# © Copyright 2021-2022, Scott Gasch + +"""chord parser unittest""" + +import unittest + +from music.chords import ChordParser + +import bootstrap +import unittest_utils as uu + + +class TestChordParder(unittest.TestCase): + def test_with_known_correct_answers(self): + expected_answers = { + 'D': "root=D, others={'maj3': 4, 'perfect5th': 7}", + 'Dmaj': "root=D, others={'maj3': 4, 'perfect5th': 7}", + 'D major': "root=D, others={'maj3': 4, 'perfect5th': 7}", + 'DM': "root=D, others={'maj3': 4, 'perfect5th': 7}", + 'Dm': "root=D, others={'min3': 3, 'perfect5th': 7}", + 'Dmin': "root=D, others={'min3': 3, 'perfect5th': 7}", + 'D minor': "root=D, others={'min3': 3, 'perfect5th': 7}", + 'Asus2': "root=A, others={'maj2': 2, 'perfect5th': 7}", + 'Bsus4': "root=B, others={'perfect4': 5, 'perfect5th': 7}", + 'F5': "root=F, others={'perfect5th': 7}", + 'G/B': "root=G, others={'maj3': 4, 'perfect5th': 7, 'B': 3}", + } + + cp = ChordParser() + for chord_name, expected_answer in expected_answers.items(): + self.assertEqual( + expected_answer, cp.parse(chord_name).__repr__(), f'Failed for {chord_name}' + ) + + +if __name__ == '__main__': + bootstrap.initialize(unittest.main)()