-
Notifications
You must be signed in to change notification settings - Fork 56
/
hashes_test.go
89 lines (74 loc) · 1.96 KB
/
hashes_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
package plush_test
import (
"testing"
"github.com/gobuffalo/plush/v5"
"github.com/stretchr/testify/require"
)
func Test_Render_Hash_Key_Interface(t *testing.T) {
r := require.New(t)
input := `<%= m["first"]%>`
s, err := plush.Render(input, plush.NewContextWith(map[string]interface{}{
"m": map[interface{}]bool{"first": true},
}))
r.NoError(err)
r.Equal("true", s)
}
func Test_Render_Hash_Key_Int_With_String_Index(t *testing.T) {
r := require.New(t)
input := `<%= m["first"]%>`
_, err := plush.Render(input, plush.NewContextWith(map[string]interface{}{
"m": map[int]bool{0: true},
}))
errStr := "line 1: cannot use first (string constant) as int value in map index"
r.Error(err)
r.Equal(errStr, err.Error())
}
func Test_Render_Hash_Array_Index(t *testing.T) {
r := require.New(t)
input := `<%= m["first"] + " " + m["last"] %>|<%= a[0+1] %>`
s, err := plush.Render(input, plush.NewContextWith(map[string]interface{}{
"m": map[string]string{"first": "Mark", "last": "Bates"},
"a": []string{"john", "paul"},
}))
r.NoError(err)
r.Equal("Mark Bates|paul", s)
}
func Test_Render_HashCall(t *testing.T) {
r := require.New(t)
input := `<%= m["a"] %>`
ctx := plush.NewContext()
ctx.Set("m", map[string]string{
"a": "A",
})
s, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("A", s)
}
func Test_Render_HashCall_OnAttribute(t *testing.T) {
r := require.New(t)
input := `<%= m.MyMap[key] %>`
ctx := plush.NewContext()
ctx.Set("m", struct {
MyMap map[string]string
}{
MyMap: map[string]string{"a": "A"},
})
ctx.Set("key", "a")
s, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("A", s)
}
func Test_Render_HashCall_OnAttribute_IntoFunction(t *testing.T) {
r := require.New(t)
input := `<%= debug(m.MyMap[key]) %>`
ctx := plush.NewContext()
ctx.Set("m", struct {
MyMap map[string]string
}{
MyMap: map[string]string{"a": "A"},
})
ctx.Set("key", "a")
s, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("<pre>A</pre>", s)
}