mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add tests for midi.message.decode
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
package processor_test
|
package processor_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
"gitlab.com/gomidi/midi/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMIDIMessageDecodeFromRegistry(t *testing.T) {
|
func TestMIDIMessageDecodeFromRegistry(t *testing.T) {
|
||||||
@@ -25,3 +27,66 @@ func TestMIDIMessageDecodeFromRegistry(t *testing.T) {
|
|||||||
t.Fatalf("midi.message.decode processor has wrong type: %s", processorInstance.Type())
|
t.Fatalf("midi.message.decode processor has wrong type: %s", processorInstance.Type())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGoodMIDIMessageDecode(t *testing.T) {
|
||||||
|
processorInstance := &processor.MIDIMessageDecode{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected any
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "note on message",
|
||||||
|
payload: []byte{0x90, 0x40, 0x7F},
|
||||||
|
expected: midi.NoteOn(0, 64, 127),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("midi.message.decode failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotMessage, ok := got.(midi.Message)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("midi.message.decode returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(gotMessage, test.expected) {
|
||||||
|
t.Fatalf("midi.message.decode got %+v, expected %+v", gotMessage, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadMIDIMessageDecode(t *testing.T) {
|
||||||
|
processorInstance := &processor.MIDIMessageDecode{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "non-byte slice payload",
|
||||||
|
payload: "12345",
|
||||||
|
errorString: "midi.message.decode processor only accepts a []byte",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("midi.message.decode expected to fail but succeeded, got: %v", got)
|
||||||
|
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Fatalf("midi.message.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user