blob: d4f94c40d0c14cf16baedd8036f1ddd46183559a [file] [log] [blame]
Ben Claytond1140552022-11-21 17:43:11 +00001// Copyright 2022 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package template_test
16
17import (
18 "bytes"
19 "testing"
20
21 "dawn.googlesource.com/dawn/tools/src/template"
22 "github.com/google/go-cmp/cmp"
23)
24
Ben Clayton70a26132023-08-12 09:24:21 +000025func check(t *testing.T, content, expected string, fns template.Functions) {
Ben Claytond1140552022-11-21 17:43:11 +000026 t.Helper()
27 w := &bytes.Buffer{}
Ben Clayton70a26132023-08-12 09:24:21 +000028 if err := template.FromString("template", content).Run(w, nil, fns); err != nil {
29 t.Errorf("Template.Run() failed with %v", err)
Ben Claytond1140552022-11-21 17:43:11 +000030 return
31 }
32 got := w.String()
33 if diff := cmp.Diff(expected, got); diff != "" {
34 t.Errorf("output was not as expected. Diff:\n%v", diff)
35 }
36}
37
38func TestContains(t *testing.T) {
39 tmpl := `
40{{ Contains "hello world" "hello"}}
41{{ Contains "hello world" "fish"}}
42`
43 expected := `
44true
45false
46`
47 check(t, tmpl, expected, nil)
48}
49
50func TestEvalSingleParameter(t *testing.T) {
51 tmpl := `
52pre-eval
53{{ Eval "T" 123 }}
54{{ Eval "T" "cat" }}
55post-eval
56
57pre-define
58{{- define "T"}}
59 . is {{.}}
60{{- end }}
61post-define
62`
63 expected := `
64pre-eval
65
66 . is 123
67
68 . is cat
69post-eval
70
71pre-define
72post-define
73`
74 check(t, tmpl, expected, nil)
75}
76
77func TestEvalParameterPairs(t *testing.T) {
78 tmpl := `
79pre-eval
80{{ Eval "T" "number" 123 "animal" "cat" }}
81post-eval
82
83pre-define
84{{- define "T"}}
85 .number is {{.number}}
86 .animal is {{.animal}}
87{{- end }}
88post-define
89`
90 expected := `
91pre-eval
92
93 .number is 123
94 .animal is cat
95post-eval
96
97pre-define
98post-define
99`
100 check(t, tmpl, expected, nil)
101}
102
103func TestHasPrefix(t *testing.T) {
104 tmpl := `
105{{ HasPrefix "hello world" "hello"}}
106{{ HasPrefix "hello world" "world"}}
107`
108 expected := `
109true
110false
111`
112 check(t, tmpl, expected, nil)
113}
114
115func TestIterate(t *testing.T) {
116 tmpl := `
117{{- range $i := Iterate 5}}
118 {{$i}}
119{{- end}}
120`
121 expected := `
122 0
123 1
124 2
125 3
126 4
127`
128 check(t, tmpl, expected, nil)
129}
130
131func TestMap(t *testing.T) {
132 tmpl := `
133 {{- $m := Map }}
134 {{- $m.Put "one" 1 }}
135 {{- $m.Put "two" 2 }}
136 one: {{ $m.Get "one" }}
137 two: {{ $m.Get "two" }}
138`
139 expected := `
140 one: 1
141 two: 2
142`
143 check(t, tmpl, expected, nil)
144}
145
146func TestPascalCase(t *testing.T) {
147 tmpl := `
148{{ PascalCase "hello world" }}
149{{ PascalCase "hello_world" }}
150`
151 expected := `
152HelloWorld
153HelloWorld
154`
155 check(t, tmpl, expected, nil)
156}
157
158func TestSplit(t *testing.T) {
159 tmpl := `
160{{- range $i, $s := Split "cat_says_meow" "_" }}
161 {{$i}}: '{{$s}}'
162{{- end }}
163`
164 expected := `
165 0: 'cat'
166 1: 'says'
167 2: 'meow'
168`
169 check(t, tmpl, expected, nil)
170}
171
172func TestTitle(t *testing.T) {
173 tmpl := `
174{{Title "hello world"}}
175`
176 expected := `
177Hello World
178`
179 check(t, tmpl, expected, nil)
180}
181
182func TrimLeft(t *testing.T) {
183 tmpl := `
184'{{TrimLeft "hello world", "hel"}}'
185`
186 expected := `
187'o world'
188`
189 check(t, tmpl, expected, nil)
190}
191
192func TrimPrefix(t *testing.T) {
193 tmpl := `
194'{{TrimLeft "hello world", "hel"}}'
195'{{TrimLeft "hello world", "heo"}}'
196`
197 expected := `
198'o world'
199'hello world'
200`
201 check(t, tmpl, expected, nil)
202}
203
204func TrimRight(t *testing.T) {
205 tmpl := `
206'{{TrimRight "hello world", "wld"}}'
207`
208 expected := `
209'hello wor'
210`
211 check(t, tmpl, expected, nil)
212}
213
214func TrimSuffix(t *testing.T) {
215 tmpl := `
216'{{TrimRight "hello world", "rld"}}'
217'{{TrimRight "hello world", "wld"}}'
218`
219 expected := `
220'hello wo'
221'hello world'
222`
223 check(t, tmpl, expected, nil)
224}