cleanup message_test layout

This commit is contained in:
Joel Wetzell
2026-04-13 16:12:19 -05:00
parent 8bb3f3fc97
commit baa140297f
+129 -106
View File
@@ -1,30 +1,29 @@
package osc package osc
import ( import (
"fmt"
"math" "math"
"reflect" "reflect"
"testing" "testing"
) )
func TestOSCMessageEncoding(t *testing.T) { func TestGoodOSCMessageEncoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string name string
message *OSCMessage message *OSCMessage
expected []byte expected []byte
}{ }{
{ {
"simple hello", name: "simple hello",
&OSCMessage{ message: &OSCMessage{
Address: "/hello", Address: "/hello",
Args: []OSCArg{}, Args: []OSCArg{},
}, },
[]byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0},
}, },
{ {
"simple address string arg", name: "simple address string arg",
&OSCMessage{ message: &OSCMessage{
Address: "/hello", Address: "/hello",
Args: []OSCArg{ Args: []OSCArg{
{ {
@@ -33,58 +32,58 @@ func TestOSCMessageEncoding(t *testing.T) {
}, },
}, },
}, },
[]byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0},
}, },
{ {
description: "simple address integer arg", name: "simple address integer arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35},
}, },
{ {
description: "simple address float arg", name: "simple address float arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0},
}, },
{ {
description: "simple address blob arg", name: "simple address blob arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98},
}, },
{ {
description: "simple address True arg", name: "simple address True arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0},
}, },
{ {
description: "simple address False arg", name: "simple address False arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0},
}, },
{ {
description: "simple address color arg", name: "simple address color arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10},
}, },
{ {
description: "simple address nil arg", name: "simple address nil arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0},
}, },
{ {
description: "simple address int64 arg", name: "simple address int64 arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255},
}, },
{ {
description: "simple address float64 arg", name: "simple address float64 arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}},
expected: []byte{ expected: []byte{
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, 47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
}, },
}, },
// TODO(jwetzell): get array args working working // TODO(jwetzell): get array args working working
// { // {
// description: "simple address array arg", // name: "simple address array arg",
// message: OSCMessage{ // message: OSCMessage{
// Address: "/hello", // Address: "/hello",
// Args: []OSCArg{ // Args: []OSCArg{
@@ -100,15 +99,15 @@ func TestOSCMessageEncoding(t *testing.T) {
// }, // },
// }, // },
{ {
description: "osc 1.0 spec example 1", name: "osc 1.0 spec example 1",
message: &OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}}, message: &OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}},
expected: []byte{ expected: []byte{
47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44, 47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44,
102, 0, 0, 67, 220, 0, 0, 102, 0, 0, 67, 220, 0, 0,
}, },
}, },
{ {
description: "osc 1.0 spec example 2", name: "osc 1.0 spec example 2",
message: &OSCMessage{ message: &OSCMessage{
Address: "/foo", Address: "/foo",
Args: []OSCArg{ Args: []OSCArg{
@@ -128,81 +127,80 @@ func TestOSCMessageEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actual := testCase.message.ToBytes()
actual := testCase.message.ToBytes() if !reflect.DeepEqual(actual, testCase.expected) {
t.Fatalf("failed to encode properly got '%v', expected '%v'", actual, testCase.expected)
if !reflect.DeepEqual(actual, testCase.expected) { }
t.Errorf("Test '%s' failed to encode properly", testCase.description) })
fmt.Printf("expected: %v\n", testCase.expected)
fmt.Printf("actual: %v\n", actual)
}
} }
} }
func TestOSCMessageDecoding(t *testing.T) { func TestGoodOSCMessageDecoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string name string
bytes []byte bytes []byte
expected OSCMessage expected OSCMessage
}{ }{
{ {
description: "simple address no args", name: "simple address no args",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{}},
}, },
{ {
description: "simple address string arg", name: "simple address string arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "s", Value: "arg1"}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "s", Value: "arg1"}}},
}, },
{ {
description: "simple address integer arg", name: "simple address integer arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: int32(35)}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: int32(35)}}},
}, },
{ {
description: "simple address float arg", name: "simple address float arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: float32(34.5)}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: float32(34.5)}}},
}, },
{ {
description: "simple address blob arg", name: "simple address blob arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
}, },
{ {
description: "simple address True arg", name: "simple address True arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}},
}, },
{ {
description: "simple address False arg", name: "simple address False arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
}, },
{ {
description: "simple address color arg", name: "simple address color arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
}, },
{ {
description: "simple address nil arg", name: "simple address nil arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
}, },
{ {
description: "simple address Inifinitum arg", name: "simple address Inifinitum arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 73, 0, 0}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 73, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "I", Value: math.MaxInt32}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "I", Value: math.MaxInt32}}},
}, },
{ {
description: "simple address int64 arg", name: "simple address int64 arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: int64(281474976710655)}}}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: int64(281474976710655)}}},
}, },
{ {
description: "simple address float64 arg", name: "simple address float64 arg",
bytes: []byte{ bytes: []byte{
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, 47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
}, },
@@ -210,7 +208,7 @@ func TestOSCMessageDecoding(t *testing.T) {
}, },
// TODO(jwetzell): support OSC array // TODO(jwetzell): support OSC array
// { // {
// description: "simple address array arg", // name: "simple address array arg",
// bytes: []byte{ // bytes: []byte{
// 47, 104, 101, 108, 108, 111, 0, 0, 44, 91, 100, 105, 93, 0, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, // 47, 104, 101, 108, 108, 111, 0, 0, 44, 91, 100, 105, 93, 0, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
// 0, 0, 3, 232, // 0, 0, 3, 232,
@@ -226,15 +224,15 @@ func TestOSCMessageDecoding(t *testing.T) {
// }, // },
// }, // },
{ {
description: "simple address no type string", name: "simple address no type string",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0}, bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0},
expected: OSCMessage{ expected: OSCMessage{
Address: "/hello", Address: "/hello",
Args: []OSCArg{}, Args: []OSCArg{},
}, },
}, },
{ {
description: "osc 1.0 spec example 1", name: "osc 1.0 spec example 1",
bytes: []byte{ bytes: []byte{
47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44, 47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44,
102, 0, 0, 67, 220, 0, 0, 102, 0, 0, 67, 220, 0, 0,
@@ -242,7 +240,7 @@ func TestOSCMessageDecoding(t *testing.T) {
expected: OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}, expected: OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}},
}, },
{ {
description: "osc 1.0 spec example 2", name: "osc 1.0 spec example 2",
bytes: []byte{ bytes: []byte{
47, 102, 111, 111, 0, 0, 0, 0, 44, 105, 105, 115, 102, 102, 0, 0, 0, 0, 3, 232, 255, 255, 255, 255, 104, 101, 108, 47, 102, 111, 111, 0, 0, 0, 0, 44, 105, 105, 115, 102, 102, 0, 0, 0, 0, 3, 232, 255, 255, 255, 255, 104, 101, 108,
108, 111, 0, 0, 0, 63, 157, 243, 182, 64, 181, 178, 45, 108, 111, 0, 0, 0, 63, 157, 243, 182, 64, 181, 178, 45,
@@ -262,24 +260,49 @@ func TestOSCMessageDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actual, error := MessageFromBytes(testCase.bytes) actual, err := MessageFromBytes(testCase.bytes)
if error != nil { if err != nil {
fmt.Println(error) t.Fatalf("failed to encode properly: %s", err.Error())
t.Errorf("Test '%s' failed to encode properly", testCase.description) }
}
if !reflect.DeepEqual(actual.Address, testCase.expected.Address) { if !reflect.DeepEqual(actual.Address, testCase.expected.Address) {
t.Errorf("Test '%s' failed to encode address properly", testCase.description) t.Fatalf("failed to encode address propertly got '%s', expected '%s'", actual.Address, testCase.expected.Address)
fmt.Printf("expected: %v\n", testCase.expected.Address) }
fmt.Printf("actual: %v\n", actual.Address)
}
if !reflect.DeepEqual(actual.Args, testCase.expected.Args) { if !reflect.DeepEqual(actual.Args, testCase.expected.Args) {
t.Errorf("Test '%s' failed to encode args properly", testCase.description) t.Fatalf("failed to encode args properly got '%+v', expected '%+v'", actual.Args, testCase.expected.Args)
fmt.Printf("expected: %v\n", testCase.expected.Args) }
fmt.Printf("actual: %v\n", actual.Args) })
} }
}
func TestBadOSCMessageDecoding(t *testing.T) {
testCases := []struct {
name string
bytes []byte
errorString string
}{
{
name: "empty byte array",
bytes: []byte{},
errorString: "cannot create OSC Message from empty byte array",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got, err := MessageFromBytes(testCase.bytes)
if err == nil {
t.Fatalf("MessageFromBytes expected to fail but got: %+v", got)
}
if err.Error() != testCase.errorString {
t.Fatalf("MessageFromBytes got error '%s', expected '%s'", err.Error(), testCase.errorString)
}
})
} }
} }