From 95d4da6dc095fafdc9ff0b62ac822f26610a8e9c Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 23 May 2023 19:02:09 +0200 Subject: [PATCH] fix and added execute template tests --- internal/init_test.go | 2 +- libs/template/execute.go | 1 - libs/template/execute_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 libs/template/execute_test.go diff --git a/internal/init_test.go b/internal/init_test.go index 98c378b5..58c48a03 100644 --- a/internal/init_test.go +++ b/internal/init_test.go @@ -36,7 +36,7 @@ func TestTemplateInitializationForDevConfig(t *testing.T) { // materialize the template cmd := root.RootCmd - cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp}) + cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp, "--config-file", filepath.Join(tmp, "config.json")}) err = cmd.Execute() require.NoError(t, err) diff --git a/libs/template/execute.go b/libs/template/execute.go index 6f63a7ac..82f80c52 100644 --- a/libs/template/execute.go +++ b/libs/template/execute.go @@ -11,7 +11,6 @@ import ( // Executes the template by applying config on it. Returns the materialized config // as a string -// TODO: test this function func executeTemplate(config map[string]any, templateDefinition string) (string, error) { // configure template with helper functions tmpl, err := template.New("").Funcs(HelperFuncs).Parse(templateDefinition) diff --git a/libs/template/execute_test.go b/libs/template/execute_test.go new file mode 100644 index 00000000..906c886a --- /dev/null +++ b/libs/template/execute_test.go @@ -0,0 +1,34 @@ +package template + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestExecuteTemplate(t *testing.T) { + templateText := + `"{{.count}} items are made of {{.Material}}". +{{if eq .Animal "sheep" }} +Sheep wool is the best! +{{else}} +{{.Animal}} wool is not too bad... +{{end}} +` + statement, err := executeTemplate(map[string]any{ + "Material": "wool", + "count": 1, + "Animal": "sheep", + }, templateText) + require.NoError(t, err) + assert.Equal(t, "\"1 items are made of wool\".\n\nSheep wool is the best!\n\n", statement) + + statement, err = executeTemplate(map[string]any{ + "Material": "wool", + "count": 1, + "Animal": "cat", + }, templateText) + require.NoError(t, err) + assert.Equal(t, "\"1 items are made of wool\".\n\ncat wool is not too bad...\n\n", statement) +}