-
Notifications
You must be signed in to change notification settings - Fork 59
/
liquid_test.go
43 lines (39 loc) · 1 KB
/
liquid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package liquid
import (
"fmt"
"log"
"testing"
"github.com/stretchr/testify/require"
)
func TestIterationKeyedMap(t *testing.T) {
vars := map[string]any{
"keyed_map": IterationKeyedMap(map[string]any{"a": 1, "b": 2}),
}
engine := NewEngine()
tpl, err := engine.ParseTemplate([]byte(`{% for k in keyed_map %}{{ k }}={{ keyed_map[k] }}.{% endfor %}`))
require.NoError(t, err)
out, err := tpl.RenderString(vars)
require.NoError(t, err)
require.Equal(t, "a=1.b=2.", out)
}
func ExampleIterationKeyedMap() {
vars := map[string]any{
"map": map[string]any{"a": 1},
"keyed_map": IterationKeyedMap(map[string]any{"a": 1}),
}
engine := NewEngine()
out, err := engine.ParseAndRenderString(
`{% for k in map %}{{ k[0] }}={{ k[1] }}.{% endfor %}`, vars)
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
out, err = engine.ParseAndRenderString(
`{% for k in keyed_map %}{{ k }}={{ keyed_map[k] }}.{% endfor %}`, vars)
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
// Output: a=1.
// a=1.
}