1 package sprig
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestDefault(t *testing.T) {
10 tpl := `{{"" | default "foo"}}`
11 if err := runt(tpl, "foo"); err != nil {
12 t.Error(err)
13 }
14 tpl = `{{default "foo" 234}}`
15 if err := runt(tpl, "234"); err != nil {
16 t.Error(err)
17 }
18 tpl = `{{default "foo" 2.34}}`
19 if err := runt(tpl, "2.34"); err != nil {
20 t.Error(err)
21 }
22
23 tpl = `{{ .Nothing | default "123" }}`
24 if err := runt(tpl, "123"); err != nil {
25 t.Error(err)
26 }
27 tpl = `{{ default "123" }}`
28 if err := runt(tpl, "123"); err != nil {
29 t.Error(err)
30 }
31 }
32
33 func TestEmpty(t *testing.T) {
34 tpl := `{{if empty 1}}1{{else}}0{{end}}`
35 if err := runt(tpl, "0"); err != nil {
36 t.Error(err)
37 }
38
39 tpl = `{{if empty 0}}1{{else}}0{{end}}`
40 if err := runt(tpl, "1"); err != nil {
41 t.Error(err)
42 }
43 tpl = `{{if empty ""}}1{{else}}0{{end}}`
44 if err := runt(tpl, "1"); err != nil {
45 t.Error(err)
46 }
47 tpl = `{{if empty 0.0}}1{{else}}0{{end}}`
48 if err := runt(tpl, "1"); err != nil {
49 t.Error(err)
50 }
51 tpl = `{{if empty false}}1{{else}}0{{end}}`
52 if err := runt(tpl, "1"); err != nil {
53 t.Error(err)
54 }
55
56 dict := map[string]interface{}{"top": map[string]interface{}{}}
57 tpl = `{{if empty .top.NoSuchThing}}1{{else}}0{{end}}`
58 if err := runtv(tpl, "1", dict); err != nil {
59 t.Error(err)
60 }
61 tpl = `{{if empty .bottom.NoSuchThing}}1{{else}}0{{end}}`
62 if err := runtv(tpl, "1", dict); err != nil {
63 t.Error(err)
64 }
65 }
66
67 func TestCoalesce(t *testing.T) {
68 tests := map[string]string{
69 `{{ coalesce 1 }}`: "1",
70 `{{ coalesce "" 0 nil 2 }}`: "2",
71 `{{ $two := 2 }}{{ coalesce "" 0 nil $two }}`: "2",
72 `{{ $two := 2 }}{{ coalesce "" $two 0 0 0 }}`: "2",
73 `{{ $two := 2 }}{{ coalesce "" $two 3 4 5 }}`: "2",
74 `{{ coalesce }}`: "<no value>",
75 }
76 for tpl, expect := range tests {
77 assert.NoError(t, runt(tpl, expect))
78 }
79
80 dict := map[string]interface{}{"top": map[string]interface{}{}}
81 tpl := `{{ coalesce .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
82 if err := runtv(tpl, "airplane", dict); err != nil {
83 t.Error(err)
84 }
85 }
86
87 func TestAll(t *testing.T) {
88 tests := map[string]string{
89 `{{ all 1 }}`: "true",
90 `{{ all "" 0 nil 2 }}`: "false",
91 `{{ $two := 2 }}{{ all "" 0 nil $two }}`: "false",
92 `{{ $two := 2 }}{{ all "" $two 0 0 0 }}`: "false",
93 `{{ $two := 2 }}{{ all "" $two 3 4 5 }}`: "false",
94 `{{ all }}`: "true",
95 }
96 for tpl, expect := range tests {
97 assert.NoError(t, runt(tpl, expect))
98 }
99
100 dict := map[string]interface{}{"top": map[string]interface{}{}}
101 tpl := `{{ all .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
102 if err := runtv(tpl, "false", dict); err != nil {
103 t.Error(err)
104 }
105 }
106
107 func TestAny(t *testing.T) {
108 tests := map[string]string{
109 `{{ any 1 }}`: "true",
110 `{{ any "" 0 nil 2 }}`: "true",
111 `{{ $two := 2 }}{{ any "" 0 nil $two }}`: "true",
112 `{{ $two := 2 }}{{ any "" $two 3 4 5 }}`: "true",
113 `{{ $zero := 0 }}{{ any "" $zero 0 0 0 }}`: "false",
114 `{{ any }}`: "false",
115 }
116 for tpl, expect := range tests {
117 assert.NoError(t, runt(tpl, expect))
118 }
119
120 dict := map[string]interface{}{"top": map[string]interface{}{}}
121 tpl := `{{ any .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
122 if err := runtv(tpl, "true", dict); err != nil {
123 t.Error(err)
124 }
125 }
126
127 func TestFromJson(t *testing.T) {
128 dict := map[string]interface{}{"Input": `{"foo": 55}`}
129
130 tpl := `{{.Input | fromJson}}`
131 expected := `map[foo:55]`
132 if err := runtv(tpl, expected, dict); err != nil {
133 t.Error(err)
134 }
135
136 tpl = `{{(.Input | fromJson).foo}}`
137 expected = `55`
138 if err := runtv(tpl, expected, dict); err != nil {
139 t.Error(err)
140 }
141 }
142
143 func TestToJson(t *testing.T) {
144 dict := map[string]interface{}{"Top": map[string]interface{}{"bool": true, "string": "test", "number": 42}}
145
146 tpl := `{{.Top | toJson}}`
147 expected := `{"bool":true,"number":42,"string":"test"}`
148 if err := runtv(tpl, expected, dict); err != nil {
149 t.Error(err)
150 }
151 }
152
153 func TestToPrettyJson(t *testing.T) {
154 dict := map[string]interface{}{"Top": map[string]interface{}{"bool": true, "string": "test", "number": 42}}
155 tpl := `{{.Top | toPrettyJson}}`
156 expected := `{
157 "bool": true,
158 "number": 42,
159 "string": "test"
160 }`
161 if err := runtv(tpl, expected, dict); err != nil {
162 t.Error(err)
163 }
164 }
165
166 func TestToRawJson(t *testing.T) {
167 dict := map[string]interface{}{"Top": map[string]interface{}{"bool": true, "string": "test", "number": 42, "html": "<HEAD>"}}
168 tpl := `{{.Top | toRawJson}}`
169 expected := `{"bool":true,"html":"<HEAD>","number":42,"string":"test"}`
170
171 if err := runtv(tpl, expected, dict); err != nil {
172 t.Error(err)
173 }
174 }
175
176 func TestTernary(t *testing.T) {
177 tpl := `{{true | ternary "foo" "bar"}}`
178 if err := runt(tpl, "foo"); err != nil {
179 t.Error(err)
180 }
181
182 tpl = `{{ternary "foo" "bar" true}}`
183 if err := runt(tpl, "foo"); err != nil {
184 t.Error(err)
185 }
186
187 tpl = `{{false | ternary "foo" "bar"}}`
188 if err := runt(tpl, "bar"); err != nil {
189 t.Error(err)
190 }
191
192 tpl = `{{ternary "foo" "bar" false}}`
193 if err := runt(tpl, "bar"); err != nil {
194 t.Error(err)
195 }
196 }
197
View as plain text