package protocol_test import ( "testing" rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1" "code.icb4dc0.de/buildr/common/protocol" ) func TestUnmarshal_Bool_Success(t *testing.T) { target := struct { Delete bool }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "delete": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_BoolValue{ BoolValue: true, }, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if !target.Delete { t.Errorf("Expected Delete to be true") } } func TestUnmarshal_Bool_Err(t *testing.T) { target := struct { Delete string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "delete": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_BoolValue{ BoolValue: true, }, }, }, } if err := protocol.Unmarshal(spec, &target); err == nil { t.Errorf("Expected error") } else { t.Log(err.Error()) } } func TestUnmarshal_Bool_Slice_Success(t *testing.T) { target := struct { Delete []bool }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "delete": { Type: rpcv1.ModuleSpec_ValueTypeBoolSlice, BoolValues: []bool{true}, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if len(target.Delete) < 1 { t.Errorf("Expected Delete to have at least one element") } else if !target.Delete[0] { t.Errorf("Expected Delete[0] to be true") } } func TestUnmarshal_Bool_Slice_Err(t *testing.T) { target := struct { Delete []string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "delete": { Type: rpcv1.ModuleSpec_ValueTypeBoolSlice, BoolValues: []bool{true}, }, }, } if err := protocol.Unmarshal(spec, &target); err == nil { t.Errorf("Expected error") } else { t.Log(err.Error()) } } func TestUnmarshal_String_Success(t *testing.T) { target := struct { Name string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "name": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_StringValue{ StringValue: "Ted", }, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if target.Name != "Ted" { t.Errorf("Expected Name to be 'Ted'") } } func TestUnmarshal_String_Err(t *testing.T) { target := struct { Name int }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "name": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_StringValue{ StringValue: "Ted", }, }, }, } if err := protocol.Unmarshal(spec, &target); err == nil { t.Errorf("Expected error") } else { t.Log(err.Error()) } } func TestUnmarshal_String_Slice_Success(t *testing.T) { target := struct { Names []string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "names": { Type: rpcv1.ModuleSpec_ValueTypeStringSlice, StringValues: []string{"Ted"}, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if len(target.Names) < 1 { t.Errorf("Expected Names to have at least one element") } else if target.Names[0] != "Ted" { t.Errorf("Expected Names[0] to be 'Ted'") } } func TestUnmarshal_String_Slice_Err(t *testing.T) { target := struct { Names []int }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "names": { Type: rpcv1.ModuleSpec_ValueTypeStringSlice, StringValues: []string{"Ted"}, }, }, } if err := protocol.Unmarshal(spec, &target); err == nil { t.Errorf("Expected error") } else { t.Log(err.Error()) } } func TestUnmarshal_Int_Success(t *testing.T) { target := struct { Age int }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "age": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_IntValue{ IntValue: 42, }, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if target.Age != 42 { t.Errorf("Expected Age to be 42") } } func TestUnmarshal_Int_Err(t *testing.T) { target := struct { Age string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "age": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_IntValue{ IntValue: 42, }, }, }, } if err := protocol.Unmarshal(spec, &target); err == nil { t.Errorf("Expected error") } else { t.Log(err.Error()) } } func TestUnmarshal_Int_Slice_Success(t *testing.T) { target := struct { Ages []int }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "ages": { Type: rpcv1.ModuleSpec_ValueTypeIntSlice, IntValues: []int64{42}, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if len(target.Ages) < 1 { t.Errorf("Expected Ages to have at least one element") } else if target.Ages[0] != 42 { t.Errorf("Expected Ages[0] to be 42") } } func TestUnmarshal_Int_Slice_Err(t *testing.T) { target := struct { Ages []string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "ages": { Type: rpcv1.ModuleSpec_ValueTypeIntSlice, IntValues: []int64{42}, }, }, } if err := protocol.Unmarshal(spec, &target); err == nil { t.Errorf("Expected error") } else { t.Log(err.Error()) } } func TestUnmarshal_Double_Success(t *testing.T) { target := struct { Pi float64 }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "Pi": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_DoubleValue{ DoubleValue: 3.14, }, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if target.Pi-3.14 > 0.0000001 { t.Errorf("Expected Pi to be 3.14") } } func TestUnmarshal_Double_Err(t *testing.T) { target := struct { Pi string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "pi": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_DoubleValue{ DoubleValue: 3.14, }, }, }, } if err := protocol.Unmarshal(spec, &target); err == nil { t.Errorf("Expected error") } else { t.Log(err.Error()) } } func TestUnmarshal_Double_Slice_Success(t *testing.T) { target := struct { Pis []float64 }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "pis": { Type: rpcv1.ModuleSpec_ValueTypeDoubleSlice, DoubleValues: []float64{3.14}, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if len(target.Pis) < 1 { t.Errorf("Expected Pis to have at least one element") } else if target.Pis[0]-3.14 > 0.0000001 { t.Errorf("Expected Pis[0] to be 3.14") } } func TestUnmarshal_Double_Slice_Err(t *testing.T) { target := struct { Pis []string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "pis": { Type: rpcv1.ModuleSpec_ValueTypeDoubleSlice, DoubleValues: []float64{3.14}, }, }, } if err := protocol.Unmarshal(spec, &target); err == nil { t.Errorf("Expected error") } else { t.Log(err.Error()) } } func TestUnmarshal_NestedStruct_Success(t *testing.T) { target := struct { Address struct { City string } }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "address": { Type: rpcv1.ModuleSpec_ValueTypeObject, ComplexValue: map[string]*rpcv1.ModuleSpec_Value{ "city": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_StringValue{ StringValue: "New York", }, }, }, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if target.Address.City != "New York" { t.Errorf("Expected City to be 'New York'") } } func TestUnmarshal_NestedStructPointer_Success(t *testing.T) { target := struct { Address *struct { City string } }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "address": { Type: rpcv1.ModuleSpec_ValueTypeObject, ComplexValue: map[string]*rpcv1.ModuleSpec_Value{ "city": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_StringValue{ StringValue: "New York", }, }, }, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if target.Address.City != "New York" { t.Errorf("Expected City to be 'New York'") } } func TestUnmarshal_Map_Success(t *testing.T) { target := struct { Values map[string]string }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "values": { Type: rpcv1.ModuleSpec_ValueTypeMap, ComplexValue: map[string]*rpcv1.ModuleSpec_Value{ "City": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_StringValue{ StringValue: "New York", }, }, }, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if target.Values["City"] != "New York" { t.Errorf("Expected City to be 'New York'") } } func TestUnmarshal_NestedMap_Success(t *testing.T) { target := struct { City struct { Labels map[string]string } }{} spec := &rpcv1.ModuleSpec{ Values: map[string]*rpcv1.ModuleSpec_Value{ "city": { Type: rpcv1.ModuleSpec_ValueTypeObject, ComplexValue: map[string]*rpcv1.ModuleSpec_Value{ "labels": { Type: rpcv1.ModuleSpec_ValueTypeMap, ComplexValue: map[string]*rpcv1.ModuleSpec_Value{ "Region": { Type: rpcv1.ModuleSpec_ValueTypeSingle, SingleValue: &rpcv1.ModuleSpec_Value_StringValue{ StringValue: "west", }, }, }, }, }, }, }, } if err := protocol.Unmarshal(spec, &target); err != nil { t.Errorf("Failed to unmarshal: %v", err) } if target.City.Labels["Region"] != "west" { t.Errorf("Expected 'Region' to be 'west'") } }