fix and added execute template tests

This commit is contained in:
Shreyas Goenka 2023-05-23 19:02:09 +02:00
parent 3626588511
commit 95d4da6dc0
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 35 additions and 2 deletions

View File

@ -36,7 +36,7 @@ func TestTemplateInitializationForDevConfig(t *testing.T) {
// materialize the template // materialize the template
cmd := root.RootCmd 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() err = cmd.Execute()
require.NoError(t, err) require.NoError(t, err)

View File

@ -11,7 +11,6 @@ import (
// Executes the template by applying config on it. Returns the materialized config // Executes the template by applying config on it. Returns the materialized config
// as a string // as a string
// TODO: test this function
func executeTemplate(config map[string]any, templateDefinition string) (string, error) { func executeTemplate(config map[string]any, templateDefinition string) (string, error) {
// configure template with helper functions // configure template with helper functions
tmpl, err := template.New("").Funcs(HelperFuncs).Parse(templateDefinition) tmpl, err := template.New("").Funcs(HelperFuncs).Parse(templateDefinition)

View File

@ -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)
}