This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables_test.go
80 lines (67 loc) · 1.67 KB
/
variables_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
package plush
import (
"html/template"
"strings"
"testing"
"github.com/gobuffalo/tags"
"github.com/stretchr/testify/require"
)
func Test_Let_Reassignment(t *testing.T) {
r := require.New(t)
input := `<% let foo = "bar" %>
<%= for (a) in myArray { %>
<%= foo %>
<% if (foo != "baz") { %>
<% foo = "baz" %>
<% } %>
<% } %>
<% } %>`
ctx := NewContext()
ctx.Set("myArray", []string{"a", "b"})
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("bar\n \n \nbaz", strings.TrimSpace(s))
}
func Test_Let_Reassignment_UnknownIdent(t *testing.T) {
r := require.New(t)
input := `<% foo = "baz" %>`
ctx := NewContext()
ctx.Set("myArray", []string{"a", "b"})
_, err := Render(input, ctx)
r.Error(err)
}
func Test_Let_Inside_Helper(t *testing.T) {
r := require.New(t)
ctx := NewContextWith(map[string]interface{}{
"divwrapper": func(opts map[string]interface{}, helper HelperContext) (template.HTML, error) {
body, err := helper.Block()
if err != nil {
return template.HTML(""), err
}
t := tags.New("div", opts)
t.Append(body)
return t.HTML(), nil
},
})
input := `<%= divwrapper({"class": "myclass"}) { %>
<ul>
<% let a = [1, 2, "three", "four"] %>
<%= for (index, name) in a { %>
<li><%=index%> - <%=name%></li>
<% } %>
</ul>
<% } %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Contains(s, "<li>0 - 1</li>")
r.Contains(s, "<li>1 - 2</li>")
r.Contains(s, "<li>2 - three</li>")
r.Contains(s, "<li>3 - four</li>")
}
func Test_Render_Let_Hash(t *testing.T) {
r := require.New(t)
input := `<p><% let h = {"a": "A"} %><%= h["a"] %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>A</p>", s)
}