Ben Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package template_test |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "testing" |
| 20 | |
| 21 | "dawn.googlesource.com/dawn/tools/src/template" |
| 22 | "github.com/google/go-cmp/cmp" |
| 23 | ) |
| 24 | |
Ben Clayton | 70a2613 | 2023-08-12 09:24:21 +0000 | [diff] [blame] | 25 | func check(t *testing.T, content, expected string, fns template.Functions) { |
Ben Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 26 | t.Helper() |
| 27 | w := &bytes.Buffer{} |
Ben Clayton | 70a2613 | 2023-08-12 09:24:21 +0000 | [diff] [blame] | 28 | if err := template.FromString("template", content).Run(w, nil, fns); err != nil { |
| 29 | t.Errorf("Template.Run() failed with %v", err) |
Ben Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 30 | 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 | |
| 38 | func TestContains(t *testing.T) { |
| 39 | tmpl := ` |
| 40 | {{ Contains "hello world" "hello"}} |
| 41 | {{ Contains "hello world" "fish"}} |
| 42 | ` |
| 43 | expected := ` |
| 44 | true |
| 45 | false |
| 46 | ` |
| 47 | check(t, tmpl, expected, nil) |
| 48 | } |
| 49 | |
| 50 | func TestEvalSingleParameter(t *testing.T) { |
| 51 | tmpl := ` |
| 52 | pre-eval |
| 53 | {{ Eval "T" 123 }} |
| 54 | {{ Eval "T" "cat" }} |
| 55 | post-eval |
| 56 | |
| 57 | pre-define |
| 58 | {{- define "T"}} |
| 59 | . is {{.}} |
| 60 | {{- end }} |
| 61 | post-define |
| 62 | ` |
| 63 | expected := ` |
| 64 | pre-eval |
| 65 | |
| 66 | . is 123 |
| 67 | |
| 68 | . is cat |
| 69 | post-eval |
| 70 | |
| 71 | pre-define |
| 72 | post-define |
| 73 | ` |
| 74 | check(t, tmpl, expected, nil) |
| 75 | } |
| 76 | |
| 77 | func TestEvalParameterPairs(t *testing.T) { |
| 78 | tmpl := ` |
| 79 | pre-eval |
| 80 | {{ Eval "T" "number" 123 "animal" "cat" }} |
| 81 | post-eval |
| 82 | |
| 83 | pre-define |
| 84 | {{- define "T"}} |
| 85 | .number is {{.number}} |
| 86 | .animal is {{.animal}} |
| 87 | {{- end }} |
| 88 | post-define |
| 89 | ` |
| 90 | expected := ` |
| 91 | pre-eval |
| 92 | |
| 93 | .number is 123 |
| 94 | .animal is cat |
| 95 | post-eval |
| 96 | |
| 97 | pre-define |
| 98 | post-define |
| 99 | ` |
| 100 | check(t, tmpl, expected, nil) |
| 101 | } |
| 102 | |
| 103 | func TestHasPrefix(t *testing.T) { |
| 104 | tmpl := ` |
| 105 | {{ HasPrefix "hello world" "hello"}} |
| 106 | {{ HasPrefix "hello world" "world"}} |
| 107 | ` |
| 108 | expected := ` |
| 109 | true |
| 110 | false |
| 111 | ` |
| 112 | check(t, tmpl, expected, nil) |
| 113 | } |
| 114 | |
| 115 | func 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 | |
| 131 | func 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 | |
| 146 | func TestPascalCase(t *testing.T) { |
| 147 | tmpl := ` |
| 148 | {{ PascalCase "hello world" }} |
| 149 | {{ PascalCase "hello_world" }} |
| 150 | ` |
| 151 | expected := ` |
| 152 | HelloWorld |
| 153 | HelloWorld |
| 154 | ` |
| 155 | check(t, tmpl, expected, nil) |
| 156 | } |
| 157 | |
| 158 | func 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 | |
| 172 | func TestTitle(t *testing.T) { |
| 173 | tmpl := ` |
| 174 | {{Title "hello world"}} |
| 175 | ` |
| 176 | expected := ` |
| 177 | Hello World |
| 178 | ` |
| 179 | check(t, tmpl, expected, nil) |
| 180 | } |
| 181 | |
| 182 | func 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 | |
| 192 | func 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 | |
| 204 | func 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 | |
| 214 | func 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 | } |