forked from osteele/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drops_test.go
92 lines (77 loc) · 1.65 KB
/
drops_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package liquid
import (
"fmt"
"log"
"testing"
"github.com/stretchr/testify/require"
)
type dropTest struct{}
func (d dropTest) ToLiquid() any { return "drop" }
func TestDrops(t *testing.T) {
require.Equal(t, "drop", FromDrop(dropTest{}))
require.Equal(t, "not a drop", FromDrop("not a drop"))
}
type redConvertible struct{}
func (c redConvertible) ToLiquid() any {
return map[string]any{
"color": "red",
}
}
func ExampleDrop_map() {
// type redConvertible struct{}
//
// func (c redConvertible) ToLiquid() any {
// return map[string]any{
// "color": "red",
// }
// }
engine := NewEngine()
bindings := map[string]any{
"car": redConvertible{},
}
template := `{{ car.color }}`
out, err := engine.ParseAndRenderString(template, bindings)
if err != nil {
log.Fatalln(err)
}
fmt.Println(out)
// Output: red
}
type car struct{ color, model string }
func (c car) ToLiquid() any {
return carDrop{c.model, c.color}
}
type carDrop struct {
Model string
Color string `liquid:"color"`
}
func (c carDrop) Drive() string {
return "AWD"
}
func ExampleDrop_struct() {
// type car struct{ color, model string }
//
// func (c car) ToLiquid() any {
// return carDrop{c.model, c.color}
// }
//
// type carDrop struct {
// Model string
// Color string `liquid:"color"`
// }
//
// func (c carDrop) Drive() string {
// return "AWD"
// }
engine := NewEngine()
bindings := map[string]any{
"car": car{"blue", "S85"},
}
template := `{{ car.color }} {{ car.Drive }} Model {{ car.Model }}`
out, err := engine.ParseAndRenderString(template, bindings)
if err != nil {
log.Fatalln(err)
}
fmt.Println(out)
// Output: blue AWD Model S85
}