From 34a22558d11b45be5733bc82fb0370f74591276d Mon Sep 17 00:00:00 2001 From: Ennis Massey Date: Tue, 29 Aug 2017 11:43:09 +1200 Subject: [PATCH] Add tests for NoteOff --- tests/__init__.py | 2 +- tests/test_events.py | 152 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 149 insertions(+), 5 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index ee32c42..241a21e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -39,7 +39,7 @@ class ExceptionFilter(logging.Filter): def filter(self, record: logging.LogRecord): - if record.msg.__repr__().__contains__("_AssertRaisesContext"): + if record.exc_info == (None, None, None): return False else: return True diff --git a/tests/test_events.py b/tests/test_events.py index 9e31bf9..14b19d8 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -36,11 +36,11 @@ class TestNoteOn(TestCase): def test_validate(self): logger.info("Starting NoteOn .valid function tests") test_val = NoteOn.valid(0x900000) - self.assertTrue(test_val, "Generic MIDI NoteOn message failed validation. Value was 0x{:X}".format(test_val)) + self.assertTrue(test_val, "Generic MIDI NoteOn message failed validation. Value was 0x{:X}".format(0x900000)) test_val = NoteOn.valid(0x800000) self.assertFalse(test_val, "Generic MIDI NoteOn message shouldn't have validated, but did. Value was 0x{:x}".format( - test_val) + 0x800000) ) logger.info("NoteOn .valid test passed") def test_constructor(self): @@ -95,6 +95,150 @@ def test_constructor(self): 'note_velocity': 0, 'raw_data': channel_num } - self.assertEqual(vars(obj), match_val, "When testing constructor of NoteOn with channel value: {}, " - "parsing failed".format(channel >> 16)) + try: + self.assertEqual(vars(obj), match_val, "When testing constructor of NoteOn with channel value: {}, " + "parsing failed".format(channel)) + except Exception as exc: + logger.exception(exc) + + for note_number in range(0,128): + note_names = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] + event_data = 0x900000 + (note_number << 8) + logger.debug("Testing constructor with value {:x}".format(event_data)) + match_val = { + 'channel_number': 0, + 'note_name': note_names[note_number % 12], + 'note_number': note_number, + 'note_velocity': 0, + 'raw_data': event_data + } + obj = NoteOn(event_data) + try: + self.assertEqual(vars(obj), match_val, "When testing constructor of NoteOn with note value: {}, " + "parsing failed".format(note_number)) + except Exception as exc: + logger.exception(exc) + + for velocity in range(0,128): + event_data = 0x900000 + (velocity) + logger.debug("Testing constructor with value {:x}".format(event_data)) + match_val = { + 'channel_number': 0, + 'note_name': 'C', + 'note_number': 0, + 'note_velocity': velocity, + 'raw_data': event_data + } + obj = NoteOn(event_data) + try: + self.assertEqual(vars(obj), match_val, "When testing constructor of NoteOn with velocity value: {}, " + "parsing failed".format(velocity)) + except Exception as exc: + logger.exception(exc) + + +class TestNoteOff(TestCase): + def test_validate(self): + logger.info("Starting NoteOff .valid function tests") + test_val = NoteOff.valid(0x800000) + self.assertTrue(test_val, "Generic MIDI NoteOff message failed validation. Value was 0x{:X}".format(0x800000)) + test_val = NoteOff.valid(0x900000) + self.assertFalse(test_val, + "Generic MIDI NoteOff message shouldn't have validated, but did. Value was 0x{:x}".format( + 0x900000) + ) + logger.info("NoteOn .valid test passed") + def test_constructor(self): + logger.info("Starting NoteOff constructor tests") + # Test constructor of generic version + test_val = NoteOff(0x800000) + match_val = { + 'channel_number': 0, + 'note_name': 'C', + 'note_number': 0, + 'note_velocity': 0, + 'raw_data': 8388608 + } + self.assertEqual(vars(test_val), + match_val, "MIDI NoteOff constructed from value 0x{:X} is incorrect".format(0x800000) + ) + # --- Exception Testing --- # + logger.info("Starting NoteOn constructor exception tests") + # Test Length Exceptions + with self.assertRaises(LengthError, + msg="NoteOff did not raise LengthError when given value 0x123001929391923919" + ) as exc: + NoteOff(0x123001929391923919) + logger.exception(exc) + with self.assertRaises(LengthError, + msg="NoteOff did not raise LengthError when given value 0x1" + ) as exc: + NoteOff(0x1) + logger.exception(exc) + # Test validation exceptions + with self.assertRaises(ValueError, + msg="NoteOff given invalid data did not raise ValueError. Value was 0x290011" + ) as exc: + NoteOff(0x290011) + logger.exception(exc) + with self.assertRaises(ValueError, + msg="NoteOff given invalid data but did not raise ValueError. Value was 0x999999" + ) as exc: + NoteOff(0x999999) + logger.exception(exc) + + # --- Parsing Testing --- # + logger.info("Starting NoteOff constructor parsing test") + for channel in range(0, 9): + channel_num = 0x800000 + (channel << 16) + logger.debug("Testing constructor with value {:x}".format(channel_num)) + obj = NoteOff(channel_num) + match_val = { + 'channel_number': channel, + 'note_name': 'C', + 'note_number': 0, + 'note_velocity': 0, + 'raw_data': channel_num + } + try: + self.assertEqual(vars(obj), match_val, "When testing constructor of NoteOff with channel value: {}, " + "parsing failed".format(channel)) + except Exception as exc: + logger.exception(exc) + + for note_number in range(0,128): + note_names = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] + event_data = 0x800000 + (note_number << 8) + logger.debug("Testing constructor with value {:x}".format(event_data)) + match_val = { + 'channel_number': 0, + 'note_name': note_names[note_number % 12], + 'note_number': note_number, + 'note_velocity': 0, + 'raw_data': event_data + } + obj = NoteOff(event_data) + try: + self.assertEqual(vars(obj), match_val, "When testing constructor of NoteOff with note value: {}, " + "parsing failed".format(note_number)) + except Exception as exc: + logger.exception(exc) + + for velocity in range(0,128): + event_data = 0x800000 + (velocity) + logger.debug("Testing constructor with value {:x}".format(event_data)) + match_val = { + 'channel_number': 0, + 'note_name': 'C', + 'note_number': 0, + 'note_velocity': velocity, + 'raw_data': event_data + } + obj = NoteOff(event_data) + try: + self.assertEqual(vars(obj), match_val, "When testing constructor of NoteOff with velocity value: {}, " + "parsing failed".format(velocity)) + except Exception as exc: + logger.exception(exc) +