def test_chord_progression_add_remove(self): c1 = Chord.from_ident("A1 Major") c2 = Chord.from_ident("A2 Major") c3 = Chord.from_ident("A3 Major") cs1 = ChordProgression(c1, c2, c3) mock = unittest.mock.Mock() self.MEL.add_handler(mock, cs1) self.assertTrue(self.MEL.handlers) self.MEL.clear_handlers() self.assertFalse(self.MEL.handlers)
def test_chord_progression_callback(self): c1 = Chord.from_ident("A1 Major") c2 = Chord.from_ident("A2 Major") c3 = Chord.from_ident("A3 Major") cs1 = ChordProgression(c1, c2, c3) mock = unittest.mock.Mock() self.MEL.add_handler(mock, cs1) press_chord(self.loopback, c1) press_chord(self.loopback, c2) press_chord(self.loopback, c3) time.sleep(TEST_CHORD_DELAY) mock.assert_called()
def test_chord_handlers_add_remove(self): mock1 = unittest.mock.Mock() mock2 = unittest.mock.Mock() c1 = Chord.from_ident("C4 Major") c2 = Chord.from_ident("C5 Minor") self.assertFalse(self.MEL.handlers) # Empty self.MEL.add_handler(mock1, c1) self.assertTrue(self.MEL.handlers) # Not empty self.MEL.clear_handlers() self.assertFalse(self.MEL.handlers) # Now test a single removal self.MEL.add_handler(mock1, c1) self.MEL.add_handler(mock2, c2) self.assertTrue(self.MEL.handlers) # Not empty self.assertIn(c1, self.MEL.handlers) self.assertIn(c2, self.MEL.handlers) self.MEL.clear_handlers(c1) self.assertNotIn(c1, self.MEL.handlers) self.assertIn(c2, self.MEL.handlers)
def test_decorator_string(self): mock = unittest.mock.Mock() self.assertFalse(self.MEL.handlers) # Empty @self.MEL.on_notes("C4 Major") def sub(): # pragma: no cover mock() self.assertTrue(self.MEL.handlers) # Not empty press_chord(self.loopback, Chord.from_ident("C4 Major")) mock.assert_called()
def test_handler_remove_class(self): mock1 = unittest.mock.Mock() mock2 = unittest.mock.Mock() c1 = Chord.from_ident("C4 Major") s1 = Sequence.from_midi_list([1, 2, 3]) self.MEL.add_handler(mock1, c1) self.MEL.add_handler(mock2, s1) self.assertIn(c1, self.MEL.handlers) self.assertIn(s1, self.MEL.handlers) self.MEL.clear_handlers(Chord) self.assertNotIn(c1, self.MEL.handlers) self.assertIn(s1, self.MEL.handlers)
def test_from_ident(self): c1 = Chord(Note(69), Note(73), Note(76)) c2 = Chord.from_ident("A4 Major") self.assertEqual(c1, c2) with self.assertRaises(AssertionError): Chord.from_ident("") with self.assertRaises(ValueError): Chord.from_ident("test test") with self.assertRaises( ValueError ): # This is a different error message in _get_semitones_from_chord_name Chord.from_ident("A4 test")
def test_chord_progression_multiple_callback(self): c1 = Chord.from_ident("A1 Major") c2 = Chord.from_ident("A2 Major") c3 = Chord.from_ident("A3 Major") c4 = Chord.from_ident("C1 Major") c5 = Chord.from_ident("C2 Major") c6 = Chord.from_ident("C3 Major") cs1 = ChordProgression(c1, c2, c3) cs2 = ChordProgression(c4, c5, c6) mock1 = unittest.mock.Mock() mock2 = unittest.mock.Mock() self.MEL.add_handler(mock1, cs1) self.MEL.add_handler(mock2, cs2) press_chord(self.loopback, c1) press_chord(self.loopback, c2) press_chord(self.loopback, c3) time.sleep(TEST_CHORD_DELAY) mock1.assert_called() press_chord(self.loopback, c4) press_chord(self.loopback, c5) press_chord(self.loopback, c6) time.sleep(TEST_CHORD_DELAY) mock2.assert_called()
def test_string(self): self.assertEqual(str(Chord.from_ident("A4 Major")), "Chord(A4, C#5, E5)")
def test_get_semitones(self): c1 = Chord.from_ident("A4 Major") self.assertEqual(c1._get_semitones(), [0, 4, 7])
def test_identify(self): c1 = Chord.from_ident("A4 Major") self.assertIn("A4 Major", c1.identify()) c2 = Chord() self.assertEqual(c2.identify(), None)
def _resolve_notes_obj(notes_obj): if isinstance(notes_obj, str): notes_obj = Chord.from_ident(notes_obj) if not (isinstance(notes_obj, Chord) or isinstance(notes_obj, Sequence) or isinstance(notes_obj, ChordProgression)): raise TypeError("Expected a Sequence or Chord") return notes_obj