|
1 | 1 | package render
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
| 5 | + "os" |
4 | 6 | "path/filepath"
|
5 | 7 | "testing"
|
6 | 8 |
|
7 | 9 | "github.com/stretchr/testify/assert"
|
8 | 10 | )
|
9 | 11 |
|
| 12 | +func TestAny(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + desc string |
| 15 | + value any |
| 16 | + data map[string]any |
| 17 | + expected any |
| 18 | + assertion assert.ErrorAssertionFunc |
| 19 | + }{ |
| 20 | + // non-string scalars should get passed through unchanged. |
| 21 | + {desc: "[nil] noop", value: nil, expected: nil, assertion: assert.NoError}, |
| 22 | + {desc: "[int] noop", value: 123, expected: 123, assertion: assert.NoError}, |
| 23 | + {desc: "[float] noop", value: 1.2, expected: 1.2, assertion: assert.NoError}, |
| 24 | + {desc: "[bool] noop", value: true, expected: true, assertion: assert.NoError}, |
| 25 | + |
| 26 | + { |
| 27 | + desc: "[string] should pass non-template strings through unchanged", |
| 28 | + value: "Howdy 👋", |
| 29 | + expected: "Howdy 👋", |
| 30 | + assertion: assert.NoError, |
| 31 | + }, |
| 32 | + { |
| 33 | + desc: "[string] should render template strings", |
| 34 | + value: "Hello, {{ .Name }} 👋", |
| 35 | + data: map[string]any{ |
| 36 | + "Name": "World", |
| 37 | + }, |
| 38 | + expected: "Hello, World 👋", |
| 39 | + assertion: assert.NoError, |
| 40 | + }, |
| 41 | + { |
| 42 | + desc: "[string] should return template errors", |
| 43 | + value: `{{ fail "boom" }}`, |
| 44 | + expected: "", |
| 45 | + assertion: assert.Error, |
| 46 | + }, |
| 47 | + |
| 48 | + { |
| 49 | + desc: "[slice] should pass non-template values through unchanged", |
| 50 | + value: []any{"111", 222, "333"}, |
| 51 | + expected: []any{"111", 222, "333"}, |
| 52 | + assertion: assert.NoError, |
| 53 | + }, |
| 54 | + { |
| 55 | + desc: "[slice] should render template strings recursively", |
| 56 | + value: []any{ |
| 57 | + "{{ .aaa }}", |
| 58 | + []any{ |
| 59 | + "{{ .bbb }}", |
| 60 | + []any{ |
| 61 | + "{{ .ccc }}", |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + data: map[string]any{ |
| 66 | + "aaa": "AAA", |
| 67 | + "bbb": "BBB", |
| 68 | + "ccc": "CCC", |
| 69 | + }, |
| 70 | + expected: []any{ |
| 71 | + "AAA", |
| 72 | + []any{ |
| 73 | + "BBB", |
| 74 | + []any{ |
| 75 | + "CCC", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + assertion: assert.NoError, |
| 80 | + }, |
| 81 | + { |
| 82 | + desc: "[slice] should return template errors", |
| 83 | + value: []any{"111", "222", `{{ fail "boom" }}`}, |
| 84 | + expected: []any(nil), |
| 85 | + assertion: assert.Error, |
| 86 | + }, |
| 87 | + |
| 88 | + { |
| 89 | + desc: "[map] should pass non-template values through unchanged", |
| 90 | + value: map[string]any{ |
| 91 | + "aaa": "aaa one", |
| 92 | + "bbb": []any{"bbb one"}, |
| 93 | + "ccc": map[string]any{ |
| 94 | + "ccc.1": "ccc one", |
| 95 | + "ccc.2": []any{"ccc two"}, |
| 96 | + }, |
| 97 | + }, |
| 98 | + expected: map[string]any{ |
| 99 | + "aaa": "aaa one", |
| 100 | + "bbb": []any{"bbb one"}, |
| 101 | + "ccc": map[string]any{ |
| 102 | + "ccc.1": "ccc one", |
| 103 | + "ccc.2": []any{"ccc two"}, |
| 104 | + }, |
| 105 | + }, |
| 106 | + assertion: assert.NoError, |
| 107 | + }, |
| 108 | + { |
| 109 | + desc: "[map] should render template strings recursively", |
| 110 | + value: map[string]any{ |
| 111 | + "{{ .aaa }}": "{{ .aaa }} one", |
| 112 | + "{{ .bbb }}": []any{"{{ .bbb }} one"}, |
| 113 | + "{{ .ccc }}": map[string]any{ |
| 114 | + "{{ .ccc }}.1": "{{ .ccc }} one", |
| 115 | + "{{ .ccc }}.2": []any{"{{ .ccc }} two"}, |
| 116 | + }, |
| 117 | + }, |
| 118 | + data: map[string]any{ |
| 119 | + "aaa": "AAA", |
| 120 | + "bbb": "BBB", |
| 121 | + "ccc": "CCC", |
| 122 | + }, |
| 123 | + expected: map[string]any{ |
| 124 | + "AAA": "AAA one", |
| 125 | + "BBB": []any{"BBB one"}, |
| 126 | + "CCC": map[string]any{ |
| 127 | + "CCC.1": "CCC one", |
| 128 | + "CCC.2": []any{"CCC two"}, |
| 129 | + }, |
| 130 | + }, |
| 131 | + assertion: assert.NoError, |
| 132 | + }, |
| 133 | + { |
| 134 | + desc: "[map] should return template errors from keys", |
| 135 | + value: map[string]any{ |
| 136 | + `{{ fail "boom" }}`: "aaa", |
| 137 | + }, |
| 138 | + expected: map[string]any(nil), |
| 139 | + assertion: assert.Error, |
| 140 | + }, |
| 141 | + { |
| 142 | + desc: "[map] should return template errors from values", |
| 143 | + value: map[string]any{ |
| 144 | + "aaa": "aaa", |
| 145 | + "bbb": map[string]any{ |
| 146 | + "ccc": `{{ fail "boom" }}`, |
| 147 | + }, |
| 148 | + }, |
| 149 | + expected: map[string]any(nil), |
| 150 | + assertion: assert.Error, |
| 151 | + }, |
| 152 | + } |
| 153 | + for _, tt := range tests { |
| 154 | + t.Run(tt.desc, func(t *testing.T) { |
| 155 | + actual, err := Any(tt.value, tt.data) |
| 156 | + if tt.assertion != nil { |
| 157 | + tt.assertion(t, err) |
| 158 | + } |
| 159 | + assert.Equal(t, tt.expected, actual) |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// Somewhat of an integration test for the primary use case. |
| 165 | +func TestAny_WithDataFromJSON(t *testing.T) { |
| 166 | + // Decode example.json into an untyped map |
| 167 | + var values map[string]any |
| 168 | + buf, err := os.ReadFile(filepath.Join("testdata", "example.json")) |
| 169 | + if err != nil { |
| 170 | + panic(err) |
| 171 | + } |
| 172 | + err = json.Unmarshal(buf, &values) |
| 173 | + if err != nil { |
| 174 | + panic(err) |
| 175 | + } |
| 176 | + |
| 177 | + // Render the map w/ some data. |
| 178 | + rendered, err := Any(values, map[string]any{ |
| 179 | + "aaa": "AAA", |
| 180 | + "bbb": "BBB", |
| 181 | + "ccc": "CCC", |
| 182 | + }) |
| 183 | + |
| 184 | + assert.NoError(t, err) |
| 185 | + assert.Equal(t, map[string]any{ |
| 186 | + "AAA": "AAA", |
| 187 | + "BBB": []any{ |
| 188 | + "BBB.1", |
| 189 | + "BBB.2", |
| 190 | + []any{ |
| 191 | + "BBB.3.1", |
| 192 | + "BBB.3.2", |
| 193 | + }, |
| 194 | + }, |
| 195 | + "CCC": map[string]any{ |
| 196 | + "CCC.1": map[string]any{ |
| 197 | + "CCC.1.1": "CCC.1.1", |
| 198 | + "CCC.1.2": "CCC.1.2", |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, rendered) |
| 202 | +} |
| 203 | + |
10 | 204 | func TestRenderFile(t *testing.T) {
|
11 | 205 | tests := []struct {
|
12 | 206 | Desc string
|
|
0 commit comments