wasi-module-sdk-go/protocol/marshal_test.go

221 lines
4.7 KiB
Go
Raw Normal View History

2023-08-15 19:46:02 +00:00
package protocol_test
import (
"testing"
2023-08-17 15:04:58 +00:00
rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1"
2023-08-15 19:46:02 +00:00
"code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol"
)
func TestMarshal_Bool_Success(t *testing.T) {
input := struct {
IsDeleted bool
}{
IsDeleted: true,
}
spec, err := protocol.Marshal(input)
if err != nil {
t.Fatal(err)
}
nameVal, ok := spec.Values["IsDeleted"]
if !ok {
t.Fatal("IsDeleted not found")
}
if nameVal.Type != rpcv1.ModuleSpec_ValueTypeSingle {
t.Fatalf("Expected single value type, got %v", nameVal.Type)
}
if s, ok := nameVal.SingleValue.(*rpcv1.ModuleSpec_Value_BoolValue); !ok {
t.Fatalf("Expected string value, got %v", nameVal.SingleValue)
} else if !s.BoolValue {
t.Errorf("Expected bool value to be true, got %t", s.BoolValue)
}
}
func TestMarshal_BoolSlice_Success(t *testing.T) {
input := struct {
IsDeleted []bool
}{
IsDeleted: []bool{true},
}
spec, err := protocol.Marshal(input)
if err != nil {
t.Fatal(err)
}
nameVal, ok := spec.Values["IsDeleted"]
if !ok {
t.Fatal("IsDeleted not found")
}
if nameVal.Type != rpcv1.ModuleSpec_ValueTypeBoolSlice {
t.Fatalf("Expected bool slice value type, got %v", nameVal.Type)
}
if len(nameVal.BoolValues) < 1 {
t.Fatalf("Expected at least one bool value, got %d", len(nameVal.BoolValues))
}
if !nameVal.BoolValues[0] {
t.Errorf("Expected bool value to be true, got %t", nameVal.BoolValues[0])
}
}
func TestMarshal_Int_Success(t *testing.T) {
input := struct {
Age int
}{
Age: 42,
}
spec, err := protocol.Marshal(input)
if err != nil {
t.Fatal(err)
}
nameVal, ok := spec.Values["Age"]
if !ok {
t.Fatal("Age not found")
}
if nameVal.Type != rpcv1.ModuleSpec_ValueTypeSingle {
t.Fatalf("Expected single value type, got %v", nameVal.Type)
}
if s, ok := nameVal.SingleValue.(*rpcv1.ModuleSpec_Value_IntValue); !ok {
t.Fatalf("Expected string value, got %v", nameVal.SingleValue)
} else if s.IntValue != 42 {
t.Errorf("Expected int value to be 42, got %d", s.IntValue)
}
}
func TestMarshal_IntSlice_Success(t *testing.T) {
input := struct {
Ages []int
}{
Ages: []int{42},
}
spec, err := protocol.Marshal(input)
if err != nil {
t.Fatal(err)
}
nameVal, ok := spec.Values["Ages"]
if !ok {
t.Fatal("Ages not found")
}
if nameVal.Type != rpcv1.ModuleSpec_ValueTypeIntSlice {
t.Fatalf("Expected int slice value type, got %v", nameVal.Type)
}
if len(nameVal.IntValues) < 1 {
t.Fatalf("Expected at least one bool value, got %d", len(nameVal.BoolValues))
}
if nameVal.IntValues[0] != 42 {
t.Errorf("Expected int value to be 52, got %d", nameVal.IntValues[0])
}
}
func TestMarshal_StringField_Success(t *testing.T) {
input := struct {
Name string
}{
Name: "John Doe",
}
spec, err := protocol.Marshal(input)
if err != nil {
t.Fatal(err)
}
nameVal, ok := spec.Values["Name"]
if !ok {
t.Fatal("Name not found")
}
if nameVal.Type != rpcv1.ModuleSpec_ValueTypeSingle {
t.Fatalf("Expected single value type, got %v", nameVal.Type)
}
if s, ok := nameVal.SingleValue.(*rpcv1.ModuleSpec_Value_StringValue); !ok {
t.Fatalf("Expected string value, got %v", nameVal.SingleValue)
} else if s.StringValue != "John Doe" {
t.Errorf("Expected string value to be John Doe, got %s", s.StringValue)
}
}
func TestMarshal_Float64_Success(t *testing.T) {
input := struct {
Pi float64
}{
Pi: 3.14,
}
spec, err := protocol.Marshal(input)
if err != nil {
t.Fatal(err)
}
nameVal, ok := spec.Values["Pi"]
if !ok {
t.Fatal("Pi not found")
}
if nameVal.Type != rpcv1.ModuleSpec_ValueTypeSingle {
t.Fatalf("Expected single value type, got %v", nameVal.Type)
}
if s, ok := nameVal.SingleValue.(*rpcv1.ModuleSpec_Value_DoubleValue); !ok {
t.Fatalf("Expected double value, got %v", nameVal.SingleValue)
} else if s.DoubleValue-3.14 > 0.000001 {
t.Errorf("Expected double value to be 3.14, got %f", s.DoubleValue)
}
}
func TestMarshal_NestedStruct_Success(t *testing.T) {
type Address struct {
City string
}
input := struct {
Address Address
}{
Address: Address{
City: "New York",
},
}
spec, err := protocol.Marshal(input)
if err != nil {
t.Fatal(err)
}
addressVal, ok := spec.Values["Address"]
if !ok {
t.Fatal("Address not found")
}
if addressVal.Type != rpcv1.ModuleSpec_ValueTypeObject {
t.Fatalf("Expected object value type, got %v", addressVal.Type)
}
cityVal, ok := addressVal.ComplexValue["City"]
if !ok {
t.Fatal("City not found")
}
if cityVal.Type != rpcv1.ModuleSpec_ValueTypeSingle {
t.Fatalf("Expected single value type, got %v", cityVal.Type)
}
if cityVal.SingleValue.(*rpcv1.ModuleSpec_Value_StringValue).StringValue != "New York" {
t.Errorf("Expected string value to be New York, got %s", cityVal.SingleValue.(*rpcv1.ModuleSpec_Value_StringValue).StringValue)
}
}