From b621944dc5f3c3e8f2080d640420e273717dd3ce Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Mon, 13 Apr 2026 18:59:43 -0500 Subject: [PATCH] error tests for arg values when encoding --- message_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ osc.go | 13 +++++++----- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/message_test.go b/message_test.go index 5000488..c64ae05 100644 --- a/message_test.go +++ b/message_test.go @@ -166,6 +166,62 @@ func TestBadOSCMessageEncoding(t *testing.T) { }, errorString: "unsupported OSC argument type: x", }, + { + name: "string arg that is not a string", + message: &OSCMessage{ + Address: "/hello", + Args: []OSCArg{{Type: "s", Value: 123}}, + }, + errorString: "OSC arg had string type but non-string value", + }, + { + name: "int32 arg that is not a number", + message: &OSCMessage{ + Address: "/hello", + Args: []OSCArg{{Type: "i", Value: "not an int"}}, + }, + errorString: "OSC arg had int32 type but non-number value", + }, + { + name: "float32 arg that is not a number", + message: &OSCMessage{ + Address: "/hello", + Args: []OSCArg{{Type: "f", Value: "not a float"}}, + }, + errorString: "OSC arg had float32 type but non-number value", + }, + { + name: "int64 arg that is not a number", + message: &OSCMessage{ + Address: "/hello", + Args: []OSCArg{{Type: "h", Value: "not an int"}}, + }, + errorString: "OSC arg had int64 type but non-number value", + }, + { + name: "float64 arg that is not a number", + message: &OSCMessage{ + Address: "/hello", + Args: []OSCArg{{Type: "d", Value: "not a float"}}, + }, + errorString: "OSC arg had float64 type but non-number value", + }, + { + name: "blob arg that is not a byte array", + message: &OSCMessage{ + Address: "/hello", + Args: []OSCArg{{Type: "b", Value: "not a blob"}}, + }, + errorString: "OSC arg had blob type but non-blob value", + }, + { + name: "color arg that is not an OSCColor", + message: &OSCMessage{ + Address: "/hello", + Args: []OSCArg{{Type: "r", Value: "not a color"}}, + }, + errorString: "OSC arg had color type but non-color value", + }, } for _, testCase := range testCases { diff --git a/osc.go b/osc.go index fc81414..7b7bbbf 100644 --- a/osc.go +++ b/osc.go @@ -123,7 +123,7 @@ func argsToBuffer(args []OSCArg) ([]byte, error) { } argBuffers = append(argBuffers, valueBytes...) } else { - return nil, errors.New("OSC arg had integer type but non-integer value") + return nil, errors.New("OSC arg had int32 type but non-number value") } case "f": if value, ok := arg.Value.(float32); ok { @@ -157,7 +157,7 @@ func argsToBuffer(args []OSCArg) ([]byte, error) { } argBuffers = append(argBuffers, valueBytes...) } else { - return nil, errors.New("OSC arg had float type but non-float value.") + return nil, errors.New("OSC arg had float32 type but non-number value") } case "b": if value, ok := arg.Value.([]byte); ok { @@ -167,7 +167,7 @@ func argsToBuffer(args []OSCArg) ([]byte, error) { } argBuffers = append(argBuffers, valueBytes...) } else { - return nil, errors.New("OSC arg had blob type but non-blob value.") + return nil, errors.New("OSC arg had blob type but non-blob value") } case "T": argBuffers = append(argBuffers, make([]byte, 0)...) @@ -179,6 +179,9 @@ func argsToBuffer(args []OSCArg) ([]byte, error) { argBuffers = append(argBuffers, make([]byte, 0)...) case "r": color, ok := arg.Value.(OSCColor) + if !ok { + return nil, errors.New("OSC arg had color type but non-color value") + } if ok { colorBytes := []byte{color.r, color.g, color.b, color.a} argBuffers = append(argBuffers, colorBytes...) @@ -203,7 +206,7 @@ func argsToBuffer(args []OSCArg) ([]byte, error) { } argBuffers = append(argBuffers, valueBytes...) } else { - return nil, errors.New("OSC arg had integer type but non-integer value.") + return nil, errors.New("OSC arg had int64 type but non-number value") } case "d": if value, ok := arg.Value.(float32); ok { @@ -237,7 +240,7 @@ func argsToBuffer(args []OSCArg) ([]byte, error) { } argBuffers = append(argBuffers, valueBytes...) } else { - return nil, errors.New("OSC arg had float type but non-float value.") + return nil, errors.New("OSC arg had float64 type but non-number value") } default: return nil, fmt.Errorf("unsupported OSC argument type: %s", oscType)