mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
add tests for osc message encode/decode
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package processor_test
|
package processor_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/osc-go"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
@@ -25,3 +27,67 @@ func TestOSCMessageDecodeFromRegistry(t *testing.T) {
|
|||||||
t.Fatalf("osc.message.decode processor has wrong type: %s", processorInstance.Type())
|
t.Fatalf("osc.message.decode processor has wrong type: %s", processorInstance.Type())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGoodOSCMessageDecode(t *testing.T) {
|
||||||
|
processorInstance := processor.OSCMessageDecode{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload []byte
|
||||||
|
expected osc.OSCMessage
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic OSC message",
|
||||||
|
payload: []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 0, 0, 0},
|
||||||
|
expected: osc.OSCMessage{
|
||||||
|
Address: "/test",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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("osc.message.decode processing failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotMessage, ok := got.(osc.OSCMessage)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("osc.message.decode returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(gotMessage, test.expected) {
|
||||||
|
t.Fatalf("osc.message.decode got %+v, expected %+v", gotMessage, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadOSCMessageDecode(t *testing.T) {
|
||||||
|
processorInstance := processor.OSCMessageDecode{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "non byte slice message input",
|
||||||
|
payload: "test",
|
||||||
|
errorString: "osc.message.decode processor only accepts a []byte payload",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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("osc.message.decode expected to fail but got payload: %s", got)
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Fatalf("osc.message.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package processor_test
|
package processor_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/osc-go"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
@@ -25,3 +27,66 @@ func TestOSCMessageEncodeFromRegistry(t *testing.T) {
|
|||||||
t.Fatalf("osc.message.encode processor has wrong type: %s", processorInstance.Type())
|
t.Fatalf("osc.message.encode processor has wrong type: %s", processorInstance.Type())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGoodOSCMessageEncode(t *testing.T) {
|
||||||
|
processorInstance := processor.OSCMessageEncode{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic OSC message",
|
||||||
|
payload: osc.OSCMessage{
|
||||||
|
Address: "/test",
|
||||||
|
},
|
||||||
|
expected: []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 0, 0, 0},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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("osc.message.encode processing failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotBytes, ok := got.([]byte)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("osc.message.encode returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !slices.Equal(gotBytes, test.expected) {
|
||||||
|
t.Fatalf("osc.message.encode got %+v, expected %+v", got, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadOSCMessageEncode(t *testing.T) {
|
||||||
|
processorInstance := processor.OSCMessageEncode{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "non-osc message input",
|
||||||
|
payload: "test",
|
||||||
|
errorString: "osc.message.encode processor only accepts an OSCMessage",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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("osc.message.encode expected to fail but got payload: %s", got)
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Fatalf("osc.message.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user