Enhance tpl func test (#619)

enfore tpl func test

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2023-01-07 11:04:52 +08:00 committed by GitHub
parent fdfa520f1b
commit db5c2a52d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 7 deletions

View File

@ -307,14 +307,44 @@ func TestSetValueAtPath_TwoComponents(t *testing.T) {
}
func TestTpl(t *testing.T) {
text := `foo: {{ .foo }}
`
expected := `foo: FOO
`
ctx := &Context{basePath: "."}
actual, err := ctx.Tpl(text, map[string]interface{}{"foo": "FOO"})
require.NoError(t, err)
require.Equal(t, expected, actual)
tests := []struct {
name string
input string
expected string
hasErr bool
data map[string]interface{}
}{
{
name: "simple",
input: `foo: {{ .foo }}`,
expected: `foo: Foo`,
data: map[string]interface{}{
"foo": "Foo",
},
},
{
name: "multiline_input",
input: `{{ .name }}
end`,
expected: "multiline\nend",
data: map[string]interface{}{
"name": "multiline",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := ctx.Tpl(tt.input, tt.data)
if tt.hasErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, tt.expected, actual)
})
}
}
func TestRequired(t *testing.T) {