Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Dawn & Tint Authors |
Ben Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ben Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ben Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 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 Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 27 | |
| 28 | package template_test |
| 29 | |
| 30 | import ( |
| 31 | "bytes" |
| 32 | "testing" |
| 33 | |
| 34 | "dawn.googlesource.com/dawn/tools/src/template" |
| 35 | "github.com/google/go-cmp/cmp" |
| 36 | ) |
| 37 | |
Ben Clayton | 70a2613 | 2023-08-12 09:24:21 +0000 | [diff] [blame] | 38 | func check(t *testing.T, content, expected string, fns template.Functions) { |
Ben Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 39 | t.Helper() |
| 40 | w := &bytes.Buffer{} |
Ben Clayton | 70a2613 | 2023-08-12 09:24:21 +0000 | [diff] [blame] | 41 | if err := template.FromString("template", content).Run(w, nil, fns); err != nil { |
| 42 | t.Errorf("Template.Run() failed with %v", err) |
Ben Clayton | d114055 | 2022-11-21 17:43:11 +0000 | [diff] [blame] | 43 | 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 | |
| 51 | func TestContains(t *testing.T) { |
| 52 | tmpl := ` |
| 53 | {{ Contains "hello world" "hello"}} |
| 54 | {{ Contains "hello world" "fish"}} |
| 55 | ` |
| 56 | expected := ` |
| 57 | true |
| 58 | false |
| 59 | ` |
| 60 | check(t, tmpl, expected, nil) |
| 61 | } |
| 62 | |
| 63 | func TestEvalSingleParameter(t *testing.T) { |
| 64 | tmpl := ` |
| 65 | pre-eval |
| 66 | {{ Eval "T" 123 }} |
| 67 | {{ Eval "T" "cat" }} |
| 68 | post-eval |
| 69 | |
| 70 | pre-define |
| 71 | {{- define "T"}} |
| 72 | . is {{.}} |
| 73 | {{- end }} |
| 74 | post-define |
| 75 | ` |
| 76 | expected := ` |
| 77 | pre-eval |
| 78 | |
| 79 | . is 123 |
| 80 | |
| 81 | . is cat |
| 82 | post-eval |
| 83 | |
| 84 | pre-define |
| 85 | post-define |
| 86 | ` |
| 87 | check(t, tmpl, expected, nil) |
| 88 | } |
| 89 | |
| 90 | func TestEvalParameterPairs(t *testing.T) { |
| 91 | tmpl := ` |
| 92 | pre-eval |
| 93 | {{ Eval "T" "number" 123 "animal" "cat" }} |
| 94 | post-eval |
| 95 | |
| 96 | pre-define |
| 97 | {{- define "T"}} |
| 98 | .number is {{.number}} |
| 99 | .animal is {{.animal}} |
| 100 | {{- end }} |
| 101 | post-define |
| 102 | ` |
| 103 | expected := ` |
| 104 | pre-eval |
| 105 | |
| 106 | .number is 123 |
| 107 | .animal is cat |
| 108 | post-eval |
| 109 | |
| 110 | pre-define |
| 111 | post-define |
| 112 | ` |
| 113 | check(t, tmpl, expected, nil) |
| 114 | } |
| 115 | |
| 116 | func TestHasPrefix(t *testing.T) { |
| 117 | tmpl := ` |
| 118 | {{ HasPrefix "hello world" "hello"}} |
| 119 | {{ HasPrefix "hello world" "world"}} |
| 120 | ` |
| 121 | expected := ` |
| 122 | true |
| 123 | false |
| 124 | ` |
| 125 | check(t, tmpl, expected, nil) |
| 126 | } |
| 127 | |
| 128 | func 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 | |
| 144 | func 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 | |
| 159 | func TestPascalCase(t *testing.T) { |
| 160 | tmpl := ` |
| 161 | {{ PascalCase "hello world" }} |
| 162 | {{ PascalCase "hello_world" }} |
| 163 | ` |
| 164 | expected := ` |
| 165 | HelloWorld |
| 166 | HelloWorld |
| 167 | ` |
| 168 | check(t, tmpl, expected, nil) |
| 169 | } |
| 170 | |
| 171 | func 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 | |
| 185 | func TestTitle(t *testing.T) { |
| 186 | tmpl := ` |
| 187 | {{Title "hello world"}} |
| 188 | ` |
| 189 | expected := ` |
| 190 | Hello World |
| 191 | ` |
| 192 | check(t, tmpl, expected, nil) |
| 193 | } |
| 194 | |
| 195 | func 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 | |
| 205 | func 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 | |
| 217 | func 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 | |
| 227 | func 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 | } |