Added the basic implementation of tests

This commit is contained in:
Maximilien Rigaut 2019-05-01 23:49:44 +02:00
parent 65aee17e60
commit 1680b9f1d6
5 changed files with 133 additions and 9 deletions

View File

@ -1,5 +1,7 @@
# gmaPy
Get/send midi commands to a GrandMA will be configured to remap all custom messages in a near future
Get/send midi commands to a GrandMA but can be configured to bind any custom SysEx message
To do so add a custom patch (or use a currently defined one, but chances are patches made for one SysEx won't work on another) and add a parser in the mapping directory
# Depends
Mostly mido (pip install mido) blessing for the testing (pip install belssings)
* Mostly mido (pip install mido) for the midi binding and blessings for the testing (pip install belssings)
* test.py depends on the submodule minitest witch also depends on blessings but is not necessary

View File

@ -1,18 +1,43 @@
from mapping import BasicMessageParse,BasicAction
import mido
import re
commandlist={1:"go",2:"stop",3:"resume",4:"timed_go",6:"set",7:"fire",11:"go_off"}
#A MessageParse object MUST be included in the file, the rest is implementation Specific
class MessageParse(BasicMessageParse):
"""Parsing of a gma SyEx message"""
"""Parsing of a gma SyEx message this parser is used once and will return a different Action object every time it is called"""
def __init__(self, arg):
def __init__(self):
#super(Parse, self).__init__() #The init here is irrelevant
self._mapping = arg
#The data is converted from hex to decimal for readability and usability
self._mapping = {"deviceid":127, #Will usually be 127 for ALL
"commandformat":127, #1 is general lighting format, 2 is moving light format and 127 is ALL
"command":None, #from 1 to 7 plus 11 (off)
"data":None #The actual data
}
self.pattern=re.compile("240 127 (?P<deviceid>\d+) 0?2 (?P<commandformat>\d+) (?P<command>\d+) (?P<data>[\d+ ]+) 247")
self._commandlist=commandlist
def __call__(self,message):
"Parse the message and depending on the type, return a different action"
match=re.match(self.pattern,message)
if match:
# Matched
return match
else:
raise MatchError
return
class Action(BasicAction):
"""The list of action to perform when a message is read"""
def __init__(self):
pass
def resolve(self,config):
def __call__(self,config):
pass
class MatchError(Exception):
pass
#def __init__(self):
# super(MatchError, self).__init__()

1
mapping/__init__.py Normal file
View File

@ -0,0 +1 @@
__all__ = ["mapping","GrandMA2"] #Never import * as every parse is supposed to be named MessageParse

View File

@ -1,3 +1,5 @@
# This is the basic mapping of a midi message, all subsequent messages must either be children of this or implement those methods
class BasicMessageParse(object):
"""Classic System Specific Midi Message as Hex (BasicSyHexParse).
Uses one method, parseMidi to parse the message and return a BasicMapping object with the mapping of the message inside"""
@ -6,7 +8,7 @@ class BasicMessageParse(object):
'''Basic init'''
self._mapping={"value":None,"type":None} # A barebone initialisation, unusable, it will need to be overridden to work
def parseMidi(self,message):
def __call__(self,message):
"""Take a midi SyxEx message and return a BasicAction (there can be many children of BasicAction to take more properly care of the cases)"""
return BasicMapping(self._mapping)
@ -18,6 +20,6 @@ class BasicAction(object):
'Barebone init'
self._mapping=dictmapping
def resolve(self,config):
"Resolve the current action, all actions will be performed depending on what's inside the mapping and the config"
def __call__(self,config):
"""Resolve the current action, all actions will be performed depending on what's inside the mapping and the config"""
pass

94
test.py Normal file
View File

@ -0,0 +1,94 @@
from minitest import minitest
#from mapping import GrandMA2
allTests=minitest.testGroup("Nominal test units",verbose=True,align=50)
class gmaPyTest(minitest.simpleTestUnit):
"""Testing the General interface"""
def __init__(self):
super(gmaPyTest, self).__init__("gmaPy")
def _testCustom(self):
self.currentTest("testing true")
if True:
self.addSuccess()
else:
self.addFailure("True is False")
class mappingTest(minitest.simpleTestUnit):
"""Testing the parsing/mapping"""
def __init__(self):
super(mappingTest, self).__init__("parsing/mapping")
def _testParsing(self):
self.currentTest("Loading default mappings")
from mapping import GrandMA2
self.gm=GrandMA2
self.addSuccess()
self.currentTest("Creating Parser")
self.parser=GrandMA2.MessageParse()
self.addSuccess()
self.currentTest("Testing hex2dec")
testX=["22","19 22","3 4 5 6 22 FF"]
testD=["34","25 34","3 4 5 6 34 255"]
sucess=1
for X,D in zip(testX,testD):
#print("{} / {} : {} [{}]".format(X,D,self.toDec(X),D==self.toDec(X)))
if self.toDec(X)!=D:
self.addFailure("Can't convert Hex to Dec")
sucess=0
break
if sucess:
self.addSuccess()
#I'm ifnoring the None case on purpose because I can't be bothered with, if it fails, it might as well fail big
self.currentTest("Test with ill-formed message")
if self.testParse("22 12"):
self.addFailure("The message is not supposed to be parsed")
else:
self.addSuccess()
self.currentTest("Test with well-formed message")
msg=self.toDec("F0 7F 7F 02 7F 01 32 31 2E 35 30 30 F7")
if self.testParse(msg):
self.addSuccess()
else:
self.addFailure("Can't parse the message {}".format(msg))
self.currentTest("Checking sanity of parse")
di=(self.parser(msg).groupdict())
inc=""
if di["command"]!="1":
inc="command "
if di["data"]!="50 49 46 53 48 48":
inc+="data "
if di["deviceid"]!="127":
inc+="deviceid "
if di["commandformat"]!="127":
inc+="commandformat"
if inc=="":
self.addSuccess()
else:
self.addFailure("Incorrect fields: "+inc[:-1])
def testParse(self,message):
try:
parsed=self.parser(message)
return True
except self.gm.MatchError:
return False
except:
self.addCritical("Can't parse (not supposed to happen)")
def toDec(self,message):
"Taken care of by mido, but testing needs to be done otherwise"
res=None
for hx in message.split(" "):
if res==None:
res=str(int(hx,16))
else:
res+=" "+str(int(hx,16))
return res
allTests.addTest(mappingTest())
allTests.test()