diff options
Diffstat (limited to 'pkg/aflow/template_test.go')
| -rw-r--r-- | pkg/aflow/template_test.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/pkg/aflow/template_test.go b/pkg/aflow/template_test.go new file mode 100644 index 000000000..e42ddd2c3 --- /dev/null +++ b/pkg/aflow/template_test.go @@ -0,0 +1,68 @@ +// Copyright 2025 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package aflow + +import ( + "fmt" + "maps" + "reflect" + "slices" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestTemplate(t *testing.T) { + type Test struct { + template string + vars map[string]reflect.Type + used []string + err string + } + tests := []Test{ + { + template: `just text`, + }, + { + template: ` + {{if .bar}} + {{.foo}} + {{end}} + + {{if $local := .bar}} + {{$local}} + {{end}} + `, + vars: map[string]reflect.Type{ + "bar": reflect.TypeFor[bool](), + "foo": reflect.TypeFor[int](), + "baz": reflect.TypeFor[int](), + }, + used: []string{"bar", "foo"}, + }, + { + template: ` + {{if .bar}} + {{.foo}} + {{end}} + `, + vars: map[string]reflect.Type{ + "bar": reflect.TypeFor[bool](), + }, + err: "input foo is not provided", + }, + } + for i, test := range tests { + t.Run(fmt.Sprint(i), func(t *testing.T) { + used, err := verifyTemplate(test.template, test.vars) + if err != nil { + assert.Equal(t, err.Error(), test.err) + return + } + require.Empty(t, test.err) + assert.ElementsMatch(t, slices.Collect(maps.Keys(used)), test.used) + }) + } +} |
