1
16
17 package engine
18
19 import (
20 "strings"
21 "testing"
22 "text/template"
23
24 "github.com/stretchr/testify/assert"
25 )
26
27 func TestFuncs(t *testing.T) {
28
29 tests := []struct {
30 tpl, expect string
31 vars interface{}
32 }{{
33 tpl: `{{ toYaml . }}`,
34 expect: `foo: bar`,
35 vars: map[string]interface{}{"foo": "bar"},
36 }, {
37 tpl: `{{ toToml . }}`,
38 expect: "foo = \"bar\"\n",
39 vars: map[string]interface{}{"foo": "bar"},
40 }, {
41 tpl: `{{ toJson . }}`,
42 expect: `{"foo":"bar"}`,
43 vars: map[string]interface{}{"foo": "bar"},
44 }, {
45 tpl: `{{ fromYaml . }}`,
46 expect: "map[hello:world]",
47 vars: `hello: world`,
48 }, {
49 tpl: `{{ fromYamlArray . }}`,
50 expect: "[one 2 map[name:helm]]",
51 vars: "- one\n- 2\n- name: helm\n",
52 }, {
53 tpl: `{{ fromYamlArray . }}`,
54 expect: "[one 2 map[name:helm]]",
55 vars: `["one", 2, { "name": "helm" }]`,
56 }, {
57
58 tpl: `{{ toToml . }}`,
59 expect: "[mast]\n sail = \"white\"\n",
60 vars: map[string]map[string]string{"mast": {"sail": "white"}},
61 }, {
62 tpl: `{{ fromYaml . }}`,
63 expect: "map[Error:error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go value of type map[string]interface {}]",
64 vars: "- one\n- two\n",
65 }, {
66 tpl: `{{ fromJson .}}`,
67 expect: `map[hello:world]`,
68 vars: `{"hello":"world"}`,
69 }, {
70 tpl: `{{ fromJson . }}`,
71 expect: `map[Error:json: cannot unmarshal array into Go value of type map[string]interface {}]`,
72 vars: `["one", "two"]`,
73 }, {
74 tpl: `{{ fromJsonArray . }}`,
75 expect: `[one 2 map[name:helm]]`,
76 vars: `["one", 2, { "name": "helm" }]`,
77 }, {
78 tpl: `{{ fromJsonArray . }}`,
79 expect: `[json: cannot unmarshal object into Go value of type []interface {}]`,
80 vars: `{"hello": "world"}`,
81 }, {
82 tpl: `{{ merge .dict (fromYaml .yaml) }}`,
83 expect: `map[a:map[b:c]]`,
84 vars: map[string]interface{}{"dict": map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, "yaml": `{"a":{"b":"d"}}`},
85 }, {
86 tpl: `{{ merge (fromYaml .yaml) .dict }}`,
87 expect: `map[a:map[b:d]]`,
88 vars: map[string]interface{}{"dict": map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, "yaml": `{"a":{"b":"d"}}`},
89 }, {
90 tpl: `{{ fromYaml . }}`,
91 expect: `map[Error:error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go value of type map[string]interface {}]`,
92 vars: `["one", "two"]`,
93 }, {
94 tpl: `{{ fromYamlArray . }}`,
95 expect: `[error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go value of type []interface {}]`,
96 vars: `hello: world`,
97 }, {
98
99 tpl: `{{ lookup "v1" "Namespace" "" "unlikelynamespace99999999" }}`,
100 expect: `map[]`,
101 vars: `["one", "two"]`,
102 }}
103
104 for _, tt := range tests {
105 var b strings.Builder
106 err := template.Must(template.New("test").Funcs(funcMap()).Parse(tt.tpl)).Execute(&b, tt.vars)
107 assert.NoError(t, err)
108 assert.Equal(t, tt.expect, b.String(), tt.tpl)
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123 func TestMerge(t *testing.T) {
124 dict := map[string]interface{}{
125 "src2": map[string]interface{}{
126 "h": 10,
127 "i": "i",
128 "j": "j",
129 },
130 "src1": map[string]interface{}{
131 "a": 1,
132 "b": 2,
133 "d": map[string]interface{}{
134 "e": "four",
135 },
136 "g": []int{6, 7},
137 "i": "aye",
138 "j": "jay",
139 "k": map[string]interface{}{
140 "l": false,
141 },
142 },
143 "dst": map[string]interface{}{
144 "a": "one",
145 "c": 3,
146 "d": map[string]interface{}{
147 "f": 5,
148 },
149 "g": []int{8, 9},
150 "i": "eye",
151 "k": map[string]interface{}{
152 "l": true,
153 },
154 },
155 }
156 tpl := `{{merge .dst .src1 .src2}}`
157 var b strings.Builder
158 err := template.Must(template.New("test").Funcs(funcMap()).Parse(tpl)).Execute(&b, dict)
159 assert.NoError(t, err)
160
161 expected := map[string]interface{}{
162 "a": "one",
163 "b": 2,
164 "c": 3,
165 "d": map[string]interface{}{
166 "e": "four",
167 "f": 5,
168 },
169 "g": []int{8, 9},
170 "h": 10,
171 "i": "eye",
172 "j": "jay",
173 "k": map[string]interface{}{
174 "l": true,
175 },
176 }
177 assert.Equal(t, expected, dict["dst"])
178 }
179
View as plain text