1 package sprig
2
3 import (
4 "strings"
5 "testing"
6 )
7
8 func TestDict(t *testing.T) {
9 tpl := `{{$d := dict 1 2 "three" "four" 5}}{{range $k, $v := $d}}{{$k}}{{$v}}{{end}}`
10 out, err := runRaw(tpl, nil)
11 if err != nil {
12 t.Error(err)
13 }
14 if len(out) != 12 {
15 t.Errorf("Expected length 12, got %d", len(out))
16 }
17
18 if !strings.Contains(out, "12") {
19 t.Error("Expected grouping 12")
20 }
21 if !strings.Contains(out, "threefour") {
22 t.Error("Expected grouping threefour")
23 }
24 if !strings.Contains(out, "5") {
25 t.Error("Expected 5")
26 }
27 tpl = `{{$t := dict "I" "shot" "the" "albatross"}}{{$t.the}} {{$t.I}}`
28 if err := runt(tpl, "albatross shot"); err != nil {
29 t.Error(err)
30 }
31 }
32
33 func TestUnset(t *testing.T) {
34 tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
35 {{- $_ := unset $d "two" -}}
36 {{- range $k, $v := $d}}{{$k}}{{$v}}{{- end -}}
37 `
38
39 expect := "one1"
40 if err := runt(tpl, expect); err != nil {
41 t.Error(err)
42 }
43 }
44 func TestHasKey(t *testing.T) {
45 tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
46 {{- if hasKey $d "one" -}}1{{- end -}}
47 `
48
49 expect := "1"
50 if err := runt(tpl, expect); err != nil {
51 t.Error(err)
52 }
53 }
54
55 func TestPluck(t *testing.T) {
56 tpl := `
57 {{- $d := dict "one" 1 "two" 222222 -}}
58 {{- $d2 := dict "one" 1 "two" 33333 -}}
59 {{- $d3 := dict "one" 1 -}}
60 {{- $d4 := dict "one" 1 "two" 4444 -}}
61 {{- pluck "two" $d $d2 $d3 $d4 -}}
62 `
63
64 expect := "[222222 33333 4444]"
65 if err := runt(tpl, expect); err != nil {
66 t.Error(err)
67 }
68 }
69
70 func TestKeys(t *testing.T) {
71 tests := map[string]string{
72 `{{ dict "foo" 1 "bar" 2 | keys | sortAlpha }}`: "[bar foo]",
73 `{{ dict | keys }}`: "[]",
74 `{{ keys (dict "foo" 1) (dict "bar" 2) (dict "bar" 3) | uniq | sortAlpha }}`: "[bar foo]",
75 }
76 for tpl, expect := range tests {
77 if err := runt(tpl, expect); err != nil {
78 t.Error(err)
79 }
80 }
81 }
82
83 func TestPick(t *testing.T) {
84 tests := map[string]string{
85 `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "two" | len -}}`: "1",
86 `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "two" -}}`: "map[two:222222]",
87 `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "one" "two" | len -}}`: "2",
88 `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "one" "two" "three" | len -}}`: "2",
89 `{{- $d := dict }}{{ pick $d "two" | len -}}`: "0",
90 }
91 for tpl, expect := range tests {
92 if err := runt(tpl, expect); err != nil {
93 t.Error(err)
94 }
95 }
96 }
97 func TestOmit(t *testing.T) {
98 tests := map[string]string{
99 `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" | len -}}`: "1",
100 `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" -}}`: "map[two:222222]",
101 `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" "two" | len -}}`: "0",
102 `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "two" "three" | len -}}`: "1",
103 `{{- $d := dict }}{{ omit $d "two" | len -}}`: "0",
104 }
105 for tpl, expect := range tests {
106 if err := runt(tpl, expect); err != nil {
107 t.Error(err)
108 }
109 }
110 }
111
112 func TestGet(t *testing.T) {
113 tests := map[string]string{
114 `{{- $d := dict "one" 1 }}{{ get $d "one" -}}`: "1",
115 `{{- $d := dict "one" 1 "two" "2" }}{{ get $d "two" -}}`: "2",
116 `{{- $d := dict }}{{ get $d "two" -}}`: "",
117 }
118 for tpl, expect := range tests {
119 if err := runt(tpl, expect); err != nil {
120 t.Error(err)
121 }
122 }
123 }
124
125 func TestSet(t *testing.T) {
126 tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
127 {{- $_ := set $d "two" 2 -}}
128 {{- $_ := set $d "three" 3 -}}
129 {{- if hasKey $d "one" -}}{{$d.one}}{{- end -}}
130 {{- if hasKey $d "two" -}}{{$d.two}}{{- end -}}
131 {{- if hasKey $d "three" -}}{{$d.three}}{{- end -}}
132 `
133
134 expect := "123"
135 if err := runt(tpl, expect); err != nil {
136 t.Error(err)
137 }
138 }
139
140 func TestValues(t *testing.T) {
141 tests := map[string]string{
142 `{{- $d := dict "a" 1 "b" 2 }}{{ values $d | sortAlpha | join "," }}`: "1,2",
143 `{{- $d := dict "a" "first" "b" 2 }}{{ values $d | sortAlpha | join "," }}`: "2,first",
144 }
145
146 for tpl, expect := range tests {
147 if err := runt(tpl, expect); err != nil {
148 t.Error(err)
149 }
150 }
151 }
152
153 func TestDig(t *testing.T) {
154 tests := map[string]string{
155 `{{- $d := dict "a" (dict "b" (dict "c" 1)) }}{{ dig "a" "b" "c" "" $d }}`: "1",
156 `{{- $d := dict "a" (dict "b" (dict "c" 1)) }}{{ dig "a" "b" "z" "2" $d }}`: "2",
157 `{{ dict "a" 1 | dig "a" "" }}`: "1",
158 `{{ dict "a" 1 | dig "z" "2" }}`: "2",
159 }
160
161 for tpl, expect := range tests {
162 if err := runt(tpl, expect); err != nil {
163 t.Error(err)
164 }
165 }
166 }
167
View as plain text