blob: 9a950a4e6369d5536df2eb697bc421738c51fa84 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2022 The Dawn & Tint Authors
Ben Claytond1140552022-11-21 17:43:11 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ben Claytond1140552022-11-21 17:43:11 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ben Claytond1140552022-11-21 17:43:11 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ben Claytond1140552022-11-21 17:43:11 +000027
28package template_test
29
30import (
31 "bytes"
32 "testing"
33
34 "dawn.googlesource.com/dawn/tools/src/template"
35 "github.com/google/go-cmp/cmp"
36)
37
Ben Clayton70a26132023-08-12 09:24:21 +000038func check(t *testing.T, content, expected string, fns template.Functions) {
Ben Claytond1140552022-11-21 17:43:11 +000039 t.Helper()
40 w := &bytes.Buffer{}
Ben Clayton70a26132023-08-12 09:24:21 +000041 if err := template.FromString("template", content).Run(w, nil, fns); err != nil {
42 t.Errorf("Template.Run() failed with %v", err)
Ben Claytond1140552022-11-21 17:43:11 +000043 return
44 }
45 got := w.String()
46 if diff := cmp.Diff(expected, got); diff != "" {
47 t.Errorf("output was not as expected. Diff:\n%v", diff)
48 }
49}
50
51func TestContains(t *testing.T) {
52 tmpl := `
53{{ Contains "hello world" "hello"}}
54{{ Contains "hello world" "fish"}}
55`
56 expected := `
57true
58false
59`
60 check(t, tmpl, expected, nil)
61}
62
63func TestEvalSingleParameter(t *testing.T) {
64 tmpl := `
65pre-eval
66{{ Eval "T" 123 }}
67{{ Eval "T" "cat" }}
68post-eval
69
70pre-define
71{{- define "T"}}
72 . is {{.}}
73{{- end }}
74post-define
75`
76 expected := `
77pre-eval
78
79 . is 123
80
81 . is cat
82post-eval
83
84pre-define
85post-define
86`
87 check(t, tmpl, expected, nil)
88}
89
90func TestEvalParameterPairs(t *testing.T) {
91 tmpl := `
92pre-eval
93{{ Eval "T" "number" 123 "animal" "cat" }}
94post-eval
95
96pre-define
97{{- define "T"}}
98 .number is {{.number}}
99 .animal is {{.animal}}
100{{- end }}
101post-define
102`
103 expected := `
104pre-eval
105
106 .number is 123
107 .animal is cat
108post-eval
109
110pre-define
111post-define
112`
113 check(t, tmpl, expected, nil)
114}
115
116func TestHasPrefix(t *testing.T) {
117 tmpl := `
118{{ HasPrefix "hello world" "hello"}}
119{{ HasPrefix "hello world" "world"}}
120`
121 expected := `
122true
123false
124`
125 check(t, tmpl, expected, nil)
126}
127
128func TestIterate(t *testing.T) {
129 tmpl := `
130{{- range $i := Iterate 5}}
131 {{$i}}
132{{- end}}
133`
134 expected := `
135 0
136 1
137 2
138 3
139 4
140`
141 check(t, tmpl, expected, nil)
142}
143
144func TestMap(t *testing.T) {
145 tmpl := `
146 {{- $m := Map }}
147 {{- $m.Put "one" 1 }}
148 {{- $m.Put "two" 2 }}
149 one: {{ $m.Get "one" }}
150 two: {{ $m.Get "two" }}
151`
152 expected := `
153 one: 1
154 two: 2
155`
156 check(t, tmpl, expected, nil)
157}
158
159func TestPascalCase(t *testing.T) {
160 tmpl := `
161{{ PascalCase "hello world" }}
162{{ PascalCase "hello_world" }}
163`
164 expected := `
165HelloWorld
166HelloWorld
167`
168 check(t, tmpl, expected, nil)
169}
170
171func TestSplit(t *testing.T) {
172 tmpl := `
173{{- range $i, $s := Split "cat_says_meow" "_" }}
174 {{$i}}: '{{$s}}'
175{{- end }}
176`
177 expected := `
178 0: 'cat'
179 1: 'says'
180 2: 'meow'
181`
182 check(t, tmpl, expected, nil)
183}
184
185func TestTitle(t *testing.T) {
186 tmpl := `
187{{Title "hello world"}}
188`
189 expected := `
190Hello World
191`
192 check(t, tmpl, expected, nil)
193}
194
195func TrimLeft(t *testing.T) {
196 tmpl := `
197'{{TrimLeft "hello world", "hel"}}'
198`
199 expected := `
200'o world'
201`
202 check(t, tmpl, expected, nil)
203}
204
205func TrimPrefix(t *testing.T) {
206 tmpl := `
207'{{TrimLeft "hello world", "hel"}}'
208'{{TrimLeft "hello world", "heo"}}'
209`
210 expected := `
211'o world'
212'hello world'
213`
214 check(t, tmpl, expected, nil)
215}
216
217func TrimRight(t *testing.T) {
218 tmpl := `
219'{{TrimRight "hello world", "wld"}}'
220`
221 expected := `
222'hello wor'
223`
224 check(t, tmpl, expected, nil)
225}
226
227func TrimSuffix(t *testing.T) {
228 tmpl := `
229'{{TrimRight "hello world", "rld"}}'
230'{{TrimRight "hello world", "wld"}}'
231`
232 expected := `
233'hello wo'
234'hello world'
235`
236 check(t, tmpl, expected, nil)
237}