Skip to content

Commit

Permalink
Implemented errors in constructor, added associated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroTransactionsMatterToo committed Aug 27, 2017
1 parent 21716c7 commit db2f6ee
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
25 changes: 25 additions & 0 deletions midisnake/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# MIT License
#
# Copyright (c) 27/08/17 Ennis Massey
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

class LengthError(Exception):
"""Raised when data is the wrong length"""
pass
15 changes: 12 additions & 3 deletions midisnake/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from io import BufferedReader
from abc import ABCMeta, abstractmethod

from midisnake.errors import LengthError

__all__ = ["Header", "Event"]


Expand Down Expand Up @@ -63,15 +65,23 @@ class Event(metaclass=ABCMeta): # pramga: no

def __init__(self, data: int) -> None:
self.raw_data = data
self._process(data)
if len(hex(data)[2:]) != 6:
err_msg = "Length of given data is incorrect. The length is {} and it should be 6".format(
len(hex(data)[2:]))
raise LengthError(err_msg)
if self.valid(data):
self._process(data)
else:
err_msg = "{} given invalid data".format(type(self).__name__)
raise ValueError(err_msg)


def __repr__(self) -> str:
return "<MIDIEvent: {}>".format(self.event_name)

def __str__(self) -> str:
return "MIDIEvent: {}".format(self.event_name)


@classmethod
def valid(cls, data: int) -> bool:
"""
Expand Down Expand Up @@ -111,7 +121,6 @@ class Track:
events = None # type: List[Event]



class VariableLengthValue:
"""Parses and stores a MIDI variable length value
Expand Down
44 changes: 43 additions & 1 deletion tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,46 @@
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# SOFTWARE.
from unittest import TestCase
from unittest.mock import MagicMock, call

from midisnake.events import NoteOff, NoteOn, PolyphonicAftertouch, PitchBend
from midisnake.errors import LengthError


class TestNoteOn(TestCase):
def test_validate(self):
test_val = NoteOn.valid(0x900000)
self.assertTrue(test_val, "Generic MIDI NoteOn message failed validation. Value was 0x{:X}".format(test_val))
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)
)

def test_constructor(self):
# Test constructor of generic version
test_val = NoteOn(0x900000)
match_val = {
'channel_number': 0,
'note_name': 'C',
'note_number': 0,
'note_velocity': 0,
'raw_data': 9437184
}
self.assertEqual(vars(test_val),
match_val, "MIDI NoteOn constructed from value 0x{:X} is incorrect".format(0x900000)
)
# Test Length Exceptions
with self.assertRaises(LengthError,
msg="NoteOn did not raise LengthError when given value 0x123001929391923919"
) as exc:
NoteOn(0x123001929391923919)

with self.assertRaises(LengthError,
msg="NoteOn did not raise LengthError when given value 0x1"
) as exc:
NoteOn(0x1)


0 comments on commit db2f6ee

Please sign in to comment.