Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init tests for the midi module #32

Merged
merged 5 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions fretwork/midi/EventDispatcher.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-

from DataTypeConverters import readBew
from DataTypeConverters import toBytes

from constants import *
from .DataTypeConverters import readBew
from .DataTypeConverters import toBytes
from .constants import *


class EventDispatcher:
Expand Down
8 changes: 3 additions & 5 deletions fretwork/midi/MidiFileParser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# -*- coding: utf-8 -*-

# uhh I don't really like this, but there are so many constants to
# import otherwise
from constants import *
from .EventDispatcher import EventDispatcher
from .constants import *

from EventDispatcher import EventDispatcher

class MidiFileParser(object):

Expand Down Expand Up @@ -41,7 +39,7 @@ def parseMThdChunk(self):
header_chunk_zise = raw_in.readBew(4)

# check if it is a proper midi file
if header_chunk_type != 'MThd':
if header_chunk_type != b'MThd':
raise TypeError("It is not a valid midi file!")

# Header values are at fixed locations, so no reason to be clever
Expand Down
6 changes: 2 additions & 4 deletions fretwork/midi/MidiInFile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

from RawInstreamFile import RawInstreamFile
from MidiFileParser import MidiFileParser
from .MidiFileParser import MidiFileParser
from .RawInstreamFile import RawInstreamFile


class MidiInFile(object):
Expand Down Expand Up @@ -38,14 +38,12 @@ def __init__(self, outStream, infile):
self.raw_in = RawInstreamFile(infile)
self.parser = MidiFileParser(self.raw_in, outStream)


def read(self):
"Start parsing the file"
p = self.parser
p.parseMThdChunk()
p.parseMTrkChunks()


def setData(self, data=''):
"Sets the data from a plain string"
self.raw_in.setData(data)
3 changes: 2 additions & 1 deletion fretwork/midi/MidiInStream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from MidiOutStream import MidiOutStream
from .MidiOutStream import MidiOutStream


class MidiInStream(object):

Expand Down
9 changes: 5 additions & 4 deletions fretwork/midi/MidiOutFile.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-

from MidiOutStream import MidiOutStream
from RawOutstreamFile import RawOutstreamFile
from .DataTypeConverters import fromBytes
from .DataTypeConverters import writeVar
from .MidiOutStream import MidiOutStream
from .RawOutstreamFile import RawOutstreamFile
from .constants import *

from constants import *
from DataTypeConverters import fromBytes, writeVar

class MidiOutFile(MidiOutStream):

Expand Down
4 changes: 3 additions & 1 deletion fretwork/midi/MidiToText.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

from MidiOutStream import MidiOutStream
from .MidiOutStream import MidiOutStream


class MidiToText(MidiOutStream):


Expand Down
6 changes: 3 additions & 3 deletions fretwork/midi/RawInstreamFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import os.path

from DataTypeConverters import readBew as readBew_
from DataTypeConverters import readVar
from DataTypeConverters import varLen
from .DataTypeConverters import readBew as readBew_
from .DataTypeConverters import readVar
from .DataTypeConverters import varLen


class RawInstreamFile(object):
Expand Down
14 changes: 9 additions & 5 deletions fretwork/midi/RawOutstreamFile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# -*- coding: utf-8 -*-

# standard library imports
import sys
from types import StringType
from cStringIO import StringIO
try:
from cStringIO import StringIO
from types import StringType
except ImportError:
import io as StringIO
StringType = str

from .DataTypeConverters import writeVar
from .DataTypeConverters import writeBew

# custom import
from DataTypeConverters import writeBew, writeVar

class RawOutstreamFile(object):

Expand Down
10 changes: 5 additions & 5 deletions fretwork/midi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-

from MidiOutStream import MidiOutStream
from MidiOutFile import MidiOutFile
from MidiInStream import MidiInStream
from MidiInFile import MidiInFile
from MidiToText import MidiToText
from .MidiInFile import MidiInFile
from .MidiInStream import MidiInStream
from .MidiOutFile import MidiOutFile
from .MidiOutStream import MidiOutStream
from .MidiToText import MidiToText

__all__ = ['MidiOutStream',
'MidiOutFile',
Expand Down
Empty file added tests/midi/__init__.py
Empty file.
Binary file added tests/midi/drumtest_notes.mid
Binary file not shown.
63 changes: 63 additions & 0 deletions tests/midi/test_datatypeconverters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# FoFiX
# Copyright (C) 2017 FoFiX team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
import unittest

from fretwork.midi.DataTypeConverters import readBew
from fretwork.midi.DataTypeConverters import readVar
from fretwork.midi.DataTypeConverters import toBytes
from fretwork.midi.DataTypeConverters import varLen


class DataTypeConvertersTest(unittest.TestCase):

def test_toBytes_type(self):
if sys.version_info.major == 2:
value = bytes('áâãa')
else:
value = bytes('áâãa', 'utf-8')
result = toBytes(value)
self.assertIs(type(result), tuple)

def test_readBew_keyerror(self):
if sys.version_info.major == 2:
value = bytes('aà')
else:
value = bytes('aà', 'utf-8')
self.assertIs(readBew(value), 0)

def test_readBew_type(self):
value = b'a'
result = readBew(value)
self.assertIs(type(result), int)

def test_readVar(self):
if sys.version_info.major == 2:
value = bytes('áâãa')
else:
value = bytes('áâãa', 'utf-8')
self.assertEqual(readVar(value), 295821045191137)

def test_varLen(self):
self.assertEqual(varLen(97), 1)
self.assertEqual(varLen(256), 2)
self.assertEqual(varLen(20000), 3)
self.assertEqual(varLen(2107151), 4)
37 changes: 37 additions & 0 deletions tests/midi/test_midiinfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# FoFiX
# Copyright (C) 2017 FoFiX team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import unittest

from fretwork.midi.MidiInFile import MidiInFile
from fretwork.midi.MidiToText import MidiToText


class MidiInFileTest(unittest.TestCase):

def setUp(self):
self.test_file = "tests/midi/drumtest_notes.mid"

def test_init(self):
MidiInFile(MidiToText(), self.test_file)

def test_read(self):
midi_in = MidiInFile(MidiToText(), self.test_file)
midi_in.read()