-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
75 lines (67 loc) · 2.18 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"testing"
"text/template"
"github.com/stretchr/testify/assert"
)
func TestNewEnvMap(t *testing.T) {
env := []string{
"A=a",
"B=",
"C",
}
expected := EnvMap{"A": "a", "B": "", "C": ""}
actual := NewEnvMap(env)
assert.Equal(t, expected, actual, "NewEnvMap() returned unexpected results")
}
func TestNewTemplateData(t *testing.T) {
inputs := map[string]any{
"foo_bar": "foobar",
"foo-baz": "foobaz",
}
env := []string{"A=a", "B=b"}
expected := TemplateData{
Input: map[string]any{
"foo_bar": "foobar",
"foo_baz": "foobaz",
},
Env: map[string]string{
"A": "a",
"B": "b",
},
}
actual := NewTemplateData(inputs, env)
assert.Equal(t, expected, actual, "NewTemplateData() returned unexpected results")
}
func TestRenderTemplate(t *testing.T) {
data := TemplateData{
Input: map[string]any{"foobar": "a"},
Env: map[string]string{"FOOBAR": "b"},
}
expected := "Input: a, Input: <no value>, Env: b, Env: <no value>"
t.Run("given a string", func(t *testing.T) {
text := "Input: {{input \"foobar\"}}, Input: {{input \"foobaz\"}}, Env: {{env \"FOOBAR\"}}, Env: {{env \"FOOBAZ\"}}"
actual, err := RenderTemplate(text, data)
assert.NoError(t, err, "RenderTemplate() returned unexpected error")
assert.Equal(t, expected, actual, "RenderTemplate() returned unexpected results")
})
t.Run("given a template object", func(t *testing.T) {
text := "Input: {{.Input.foobar}}, Input: {{.Input.foobaz}}, Env: {{.Env.FOOBAR}}, Env: {{.Env.FOOBAZ}}"
tmpl := template.New("")
_, err := tmpl.Parse(text)
assert.NoError(t, err, "Could not render template")
actual, err := RenderTemplate(tmpl, data)
assert.NoError(t, err, "RenderTemplate() returned unexpected error")
assert.Equal(t, expected, actual, "RenderTemplate() returned unexpected results")
})
t.Run("given other", func(t *testing.T) {
_, actual := RenderTemplate(nil, data)
assert.EqualError(t, actual, "unsupported type: <nil>", "RenderTemplate() returned unexpected error")
})
}
func TestDiffStrings(t *testing.T) {
a := []string{"a", "b", "c"}
b := []string{"a"}
expected := []string{"b", "c"}
assert.Equal(t, expected, DiffStrings(a, b), "DiffStrings() returned unexpected results")
}