Repository navigation
Expand file tree
/
Copy pathsyntax_features_test.go
More file actions
158 lines (150 loc) · 3.78 KB
/
Copy pathsyntax_features_test.go
File metadata and controls
158 lines (150 loc) · 3.78 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package plush_test
import (
"html/template"
"testing"
"github.com/gobuffalo/plush/v5"
"github.com/stretchr/testify/require"
)
func Test_Render_Whitespace_Trim_Tags(t *testing.T) {
tests := []struct {
name string
input string
expected string
ctx *plush.Context
}{
{
name: "issue newline example",
input: "<pre>\n<%- \"Hello\" %>\n</pre>",
expected: "<pre>Hello</pre>",
ctx: plush.NewContext(),
},
{
name: "issue inline spaces example",
input: `<pre> <%- "Hello" %> </pre>`,
expected: "<pre>Hello</pre>",
ctx: plush.NewContext(),
},
{
name: "middle of document",
input: "<p>A</p>\n<%- \"B\" %>\n<p>C</p>",
expected: "<p>A</p>B<p>C</p>",
ctx: plush.NewContext(),
},
{
name: "raw html safety unchanged",
input: `<%- raw("<b>x</b>") %>`,
expected: "<b>x</b>",
ctx: plush.NewContext(),
},
{
name: "inside if",
input: "<%= if true { %>\n<%- \"yes\" %>\n<% } %>",
expected: "yes",
ctx: plush.NewContext(),
},
{
name: "inside for",
input: "<%= for (i,v) in items { %>\n<%- v %>\n<% } %>",
expected: "ab",
ctx: plush.NewContextWith(map[string]interface{}{
"items": []string{"a", "b"},
}),
},
{
name: "inside helper block",
input: "<%= wrap() { %>\n<%- \"x\" %>\n<% } %>",
expected: "<span>x</span>",
ctx: plush.NewContextWith(map[string]interface{}{
"wrap": func(help plush.HelperContext) (template.HTML, error) {
body, err := help.Block()
return template.HTML("<span>" + body + "</span>"), err
},
}),
},
{
name: "inside partial",
input: `<%= partial("row.plush") %>`,
expected: "<span>x</span>",
ctx: plush.NewContextWith(map[string]interface{}{
"partialFeeder": func(string) (string, error) {
return "<span>\n<%- \"x\" %>\n</span>", nil
},
}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, err := plush.Render(tt.input, tt.ctx)
require.NoError(t, err)
require.Equal(t, tt.expected, out)
})
}
}
func Test_Render_Old_Eval_Whitespace_Behavior_Unchanged(t *testing.T) {
out, err := plush.Render("<pre>\n<%= \"Hello\" %>\n</pre>", plush.NewContext())
require.NoError(t, err)
require.Equal(t, "<pre>\nHello\n</pre>", out)
}
func Test_Render_Optional_If_Parentheses(t *testing.T) {
type robot struct {
Name string
}
tests := []struct {
name string
input string
expected string
ctx *plush.Context
}{
{
name: "literal true",
input: `<%= if true { %>yes<% } %>`,
expected: "yes",
ctx: plush.NewContext(),
},
{
name: "identifier truthy",
input: `<%= if name { %><%= name %><% } %>`,
expected: "mark",
ctx: plush.NewContextWith(map[string]interface{}{
"name": "mark",
}),
},
{
name: "equality",
input: `<%= if name == "mark" { %>yes<% } %>`,
expected: "yes",
ctx: plush.NewContextWith(map[string]interface{}{
"name": "mark",
}),
},
{
name: "else if expression",
input: `<%= if false { %>no<% } else if name == "mark" { %>yes<% } %>`,
expected: "yes",
ctx: plush.NewContextWith(map[string]interface{}{
"name": "mark",
}),
},
{
name: "indexed struct field",
input: `<%= if robots[0].Name == "mark" { %>yes<% } %>`,
expected: "yes",
ctx: plush.NewContextWith(map[string]interface{}{
"robots": []robot{{Name: "mark"}},
}),
},
{
name: "old syntax still works",
input: `<%= if (true) { %>yes<% } else if (false) { %>no<% } %>`,
expected: "yes",
ctx: plush.NewContext(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, err := plush.Render(tt.input, tt.ctx)
require.NoError(t, err)
require.Equal(t, tt.expected, out)
})
}
}