aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2026-01-20 21:24:55 +0100
committerDmitry Vyukov <dvyukov@google.com>2026-01-21 13:38:45 +0000
commite477616cc03edfcd82015477fada8dcab6760cee (patch)
treee7f604ea512f6235870d083db9a2bef1938a0775 /pkg
parentc9820ab0fe2dce0914ff01bcaf3829ca82150eb2 (diff)
pkg/aflow: add convertFromMap test
Diffstat (limited to 'pkg')
-rw-r--r--pkg/aflow/schema_test.go77
1 files changed, 77 insertions, 0 deletions
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)
+ }
+ }
+}