From db5c2a52d6578f6421b5079b1e0f59467393b453 Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Sat, 7 Jan 2023 11:04:52 +0800 Subject: [PATCH] Enhance tpl func test (#619) enfore tpl func test Signed-off-by: yxxhero --- pkg/tmpl/context_funcs_test.go | 44 ++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/pkg/tmpl/context_funcs_test.go b/pkg/tmpl/context_funcs_test.go index 3c25494f..f7fd40c0 100644 --- a/pkg/tmpl/context_funcs_test.go +++ b/pkg/tmpl/context_funcs_test.go @@ -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) {