error tests for arg values when encoding

This commit is contained in:
Joel Wetzell
2026-04-13 18:59:43 -05:00
parent 8fa8aa3a55
commit b621944dc5
2 changed files with 64 additions and 5 deletions
+56
View File
@@ -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 {
+8 -5
View File
@@ -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)