Merge pull request #32 from jwetzell/feat/arg-constructors

add convenience constructors for arguments
This commit is contained in:
Joel Wetzell
2026-09-10 11:56:29 -05:00
committed by GitHub
2 changed files with 172 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
package osc
func StringArg(value string) Arg {
return Arg{Type: "s", Value: value}
}
func Int32Arg(value int32) Arg {
return Arg{Type: "i", Value: value}
}
func FloatArg(value float32) Arg {
return Arg{Type: "f", Value: value}
}
func BlobArg(value []byte) Arg {
return Arg{Type: "b", Value: value}
}
func TrueArg() Arg {
return Arg{Type: "T", Value: true}
}
func FalseArg() Arg {
return Arg{Type: "F", Value: false}
}
func NilArg() Arg {
return Arg{Type: "N", Value: nil}
}
func ColorArg(red, green, blue, alpha byte) Arg {
return Arg{Type: "r", Value: Color{r: red, g: green, b: blue, a: alpha}}
}
func Int64Arg(value int64) Arg {
return Arg{Type: "h", Value: value}
}
func DoubleArg(value float64) Arg {
return Arg{Type: "d", Value: value}
}
func TimeTagArg(seconds, fractional int32) Arg {
return Arg{Type: "t", Value: TimeTag{seconds: seconds, fractionalSeconds: fractional}}
}
+127
View File
@@ -0,0 +1,127 @@
package osc
import (
"reflect"
"testing"
)
func TestStringArg(t *testing.T) {
expected := "test"
arg := StringArg(expected)
if arg.Type != "s" {
t.Errorf("Expected type 's', got '%s'", arg.Type)
}
if arg.Value != expected {
t.Errorf("Expected value '%s', got '%v'", expected, arg.Value)
}
}
func TestInt32Arg(t *testing.T) {
expected := int32(10)
arg := Int32Arg(expected)
if arg.Type != "i" {
t.Errorf("Expected type 'i', got '%s'", arg.Type)
}
if arg.Value != expected {
t.Errorf("Expected value '%d', got '%v'", expected, arg.Value)
}
}
func TestFloatArg(t *testing.T) {
expected := float32(3.14)
arg := FloatArg(expected)
if arg.Type != "f" {
t.Errorf("Expected type 'f', got '%s'", arg.Type)
}
if arg.Value != expected {
t.Errorf("Expected value '%f', got '%v'", expected, arg.Value)
}
}
func TestBlobArg(t *testing.T) {
expected := []byte{0x01, 0x02, 0x03}
arg := BlobArg(expected)
if arg.Type != "b" {
t.Errorf("Expected type 'b', got '%s'", arg.Type)
}
if !reflect.DeepEqual(arg.Value, expected) {
t.Errorf("Expected value '%v', got '%v'", expected, arg.Value)
}
}
func TestTrueArg(t *testing.T) {
arg := TrueArg()
if arg.Type != "T" {
t.Errorf("Expected type 'T', got '%s'", arg.Type)
}
if arg.Value != true {
t.Errorf("Expected value '%v', got '%v'", true, arg.Value)
}
}
func TestFalseArg(t *testing.T) {
arg := FalseArg()
if arg.Type != "F" {
t.Errorf("Expected type 'F', got '%s'", arg.Type)
}
if arg.Value != false {
t.Errorf("Expected value '%v', got '%v'", false, arg.Value)
}
}
func TestNilArg(t *testing.T) {
arg := NilArg()
if arg.Type != "N" {
t.Errorf("Expected type 'N', got '%s'", arg.Type)
}
if arg.Value != nil {
t.Errorf("Expected value '%v', got '%v'", nil, arg.Value)
}
}
func TestColorArg(t *testing.T) {
expected := Color{r: 255, g: 0, b: 0, a: 255}
arg := ColorArg(expected.r, expected.b, expected.g, expected.r)
if arg.Type != "r" {
t.Errorf("Expected type 'r', got '%s'", arg.Type)
}
if !reflect.DeepEqual(arg.Value, expected) {
t.Errorf("Expected value '%v', got '%v'", expected, arg.Value)
}
}
func TestInt64Arg(t *testing.T) {
expected := int64(100)
arg := Int64Arg(expected)
if arg.Type != "h" {
t.Errorf("Expected type 'h', got '%s'", arg.Type)
}
if arg.Value != expected {
t.Errorf("Expected value '%d', got '%v'", expected, arg.Value)
}
}
func TestDoubleArg(t *testing.T) {
expected := float64(3.14159)
arg := DoubleArg(expected)
if arg.Type != "d" {
t.Errorf("Expected type 'd', got '%s'", arg.Type)
}
if arg.Value != expected {
t.Errorf("Expected value '%f', got '%v'", expected, arg.Value)
}
}
func TestTimeTagArg(t *testing.T) {
expected := TimeTag{seconds: 12345, fractionalSeconds: 67890}
arg := TimeTagArg(expected.seconds, expected.fractionalSeconds)
if arg.Type != "t" {
t.Errorf("Expected type 't', got '%s'", arg.Type)
}
if !reflect.DeepEqual(arg.Value, expected) {
t.Errorf("Expected value '%v', got '%v'", expected, arg.Value)
}
}