Skip to content

Commit

Permalink
Add tests for NoteOff
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroTransactionsMatterToo committed Aug 28, 2017
1 parent 368821a commit 34a2255
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tests/__init__.py
Expand Up @@ -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
Expand Down
152 changes: 148 additions & 4 deletions tests/test_events.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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)


0 comments on commit 34a2255

Please sign in to comment.