From e477616cc03edfcd82015477fada8dcab6760cee Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 20 Jan 2026 21:24:55 +0100 Subject: pkg/aflow: add convertFromMap test --- pkg/aflow/schema_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'pkg') diff --git a/pkg/aflow/schema_test.go b/pkg/aflow/schema_test.go index ac441f7d6..b5ed1062f 100644 --- a/pkg/aflow/schema_test.go +++ b/pkg/aflow/schema_test.go @@ -4,6 +4,7 @@ package aflow import ( + "encoding/json" "fmt" "testing" @@ -48,3 +49,79 @@ func TestSchema(t *testing.T) { }) } } + +func TestConvertFromMap(t *testing.T) { + testConvertFromMap(t, false, map[string]any{ + "I0": -1, + "I1": 2.0, + "I2": 3.0, + "S": "foo", + "VM": map[string]any{ + "Foo": 1, + "Str": "str", + }, + "unused": "unused", + }, struct { + I0 int + I1 int + I2 uint8 + S string + VM json.RawMessage + }{ + I0: -1, + I1: 2, + I2: 3, + S: "foo", + VM: json.RawMessage(`{"Foo":1,"Str":"str"}`), + }, + "", "") + + testConvertFromMap(t, true, map[string]any{ + "I1": 2.0, + }, struct { + I0 int + }{}, + `missing argument "I0"`, + `field "I0" is not present when converting map to struct { I0 int }`) + + testConvertFromMap(t, true, map[string]any{ + "I0": "foo", + }, struct { + I0 int + }{}, + `argument "I0" has wrong type: got string, want int`, + `field "I0" has wrong type: got string, want int`) + + testConvertFromMap(t, true, map[string]any{ + "I0": 1.1, + }, struct { + I0 int + }{}, + `argument I0: float value truncated from 1.1 to 1`, + `field I0: float value truncated from 1.1 to 1`) + + testConvertFromMap(t, true, map[string]any{ + "I0": -1, + "I1": 2.0, + }, struct { + I0 int + }{}, + `unused fields when converting map to struct { I0 int }: map[I1:2]`, + `unused fields when converting map to struct { I0 int }: map[I1:2]`) +} + +func testConvertFromMap[T any](t *testing.T, strict bool, input map[string]any, output T, toolErr, nonToolErr string) { + for _, tool := range []bool{true, false} { + wantErr := nonToolErr + if tool { + wantErr = toolErr + } + got, err := convertFromMap[T](input, strict, tool) + if err != nil { + require.Equal(t, err.Error(), wantErr) + } else { + require.Empty(t, wantErr) + require.Equal(t, got, output) + } + } +} -- cgit mrf-deployment