-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathfor_test.go
103 lines (91 loc) · 2.33 KB
/
for_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
93
94
95
96
97
98
99
100
101
102
103
package plush
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Render_For_Array(t *testing.T) {
r := require.New(t)
input := `<% for (i,v) in ["a", "b", "c"] {return v} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}
func Test_Render_For_Hash(t *testing.T) {
r := require.New(t)
input := `<%= for (k,v) in myMap { %><%= k + ":" + v%><% } %>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"myMap": map[string]string{
"a": "A",
"b": "B",
},
}))
r.NoError(err)
r.Contains(s, "a:A")
r.Contains(s, "b:B")
}
func Test_Render_For_Array_Return(t *testing.T) {
r := require.New(t)
input := `<%= for (i,v) in ["a", "b", "c"] {return v} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("abc", s)
}
func Test_Render_For_Array_Key_Only(t *testing.T) {
r := require.New(t)
input := `<%= for (v) in ["a", "b", "c"] {%><%=v%><%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("abc", s)
}
func Test_Render_For_Func_Range(t *testing.T) {
r := require.New(t)
input := `<%= for (v) in range(3,5) { %><%=v%><% } %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("345", s)
}
func Test_Render_For_Func_Between(t *testing.T) {
r := require.New(t)
input := `<%= for (v) in between(3,6) { %><%=v%><% } %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("45", s)
}
func Test_Render_For_Func_Until(t *testing.T) {
r := require.New(t)
input := `<%= for (v) in until(3) { %><%=v%><% } %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("012", s)
}
func Test_Render_For_Array_Key_Value(t *testing.T) {
r := require.New(t)
input := `<%= for (i,v) in ["a", "b", "c"] {%><%=i%><%=v%><%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("0a1b2c", s)
}
func Test_Render_For_Nil(t *testing.T) {
r := require.New(t)
input := `<% for (i,v) in nilValue {return v} %>`
ctx := NewContext()
ctx.Set("nilValue", nil)
s, err := Render(input, ctx)
r.Error(err)
r.Equal("", s)
}
func Test_Render_For_Map_Nil_Value(t *testing.T) {
r := require.New(t)
input := `
<%= for (k, v) in flash["errors"] { %>
Flash:
<%= k %>:<%= v %>
<% } %>
`
ctx := NewContext()
ctx.Set("flash", map[string][]string{})
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("", strings.TrimSpace(s))
}