2023-07-25 14:42:53 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-25 15:18:43 +00:00
|
|
|
"strings"
|
2023-07-25 14:42:53 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-07-25 15:18:43 +00:00
|
|
|
func TestTemplatePrintStringWithoutProcessing(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
r, err := newRenderer(ctx, nil, "./testdata/print-without-processing/template", "./testdata/print-without-processing/library", tmpDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
2023-08-01 13:43:27 +00:00
|
|
|
cleanContent := strings.Trim(string(r.files[0].(*inMemoryFile).content), "\n\r")
|
2023-07-25 15:18:43 +00:00
|
|
|
assert.Equal(t, `{{ fail "abc" }}`, cleanContent)
|
|
|
|
}
|
|
|
|
|
2023-07-25 14:42:53 +00:00
|
|
|
func TestTemplateRegexpCompileFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
r, err := newRenderer(ctx, nil, "./testdata/regexp-compile/template", "./testdata/regexp-compile/library", tmpDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
2023-08-01 13:43:27 +00:00
|
|
|
content := string(r.files[0].(*inMemoryFile).content)
|
2023-07-25 14:42:53 +00:00
|
|
|
assert.Contains(t, content, "0:food")
|
|
|
|
assert.Contains(t, content, "1:fool")
|
|
|
|
}
|
2023-07-27 09:51:31 +00:00
|
|
|
|
|
|
|
func TestTemplateUrlFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
r, err := newRenderer(ctx, nil, "./testdata/urlparse-function/template", "./testdata/urlparse-function/library", tmpDir)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
2023-08-01 13:43:27 +00:00
|
|
|
assert.Equal(t, "https://www.databricks.com", string(r.files[0].(*inMemoryFile).content))
|
2023-07-27 09:51:31 +00:00
|
|
|
}
|
2023-08-15 16:07:22 +00:00
|
|
|
|
|
|
|
func TestTemplateMapPairFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
r, err := newRenderer(ctx, nil, "./testdata/map-pair/template", "./testdata/map-pair/library", tmpDir)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
|
|
|
assert.Equal(t, "false 123 hello 12.3", string(r.files[0].(*inMemoryFile).content))
|
|
|
|
}
|