aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/aflow/template_test.go
blob: e42ddd2c3275e12be6c1a66fd29e91b9c8f8050c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)
		})
	}
}