-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_test.go
222 lines (197 loc) · 3.66 KB
/
render_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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package moontpl
import (
"fmt"
"strings"
"testing"
)
func TestSimple(t *testing.T) {
testRender(t, Data{code: `
require("html").importGlobals()
return DIV {
P "Hello, world"
}
`, expected: `
<div>
<p>Hello, world</p>
</div>
`})
}
func TestNested(t *testing.T) {
testRender(t, Data{code: `
require("html").importGlobals()
return DIV {
H1 "this is a heading";
H2 "this is a subheading";
P [[
A paragraph of text is here. Sentence follows naturally.
]];
P {
[[ Another paragraph starts. ]] ;
I "Italic, but not italian" ;
B "Bold, and emphasized, barely a sentence.";
};
DIV {
id="provided";
class="middle",
"A div with a middle class and a provided id";
}
}
`, expected: `
<div>
<h1>this is a heading</h1>
<h2>this is a subheading</h2>
<p> A paragraph of text is here. Sentence follows naturally.
</p>
<p> Another paragraph starts. <i>Italic, but not italian</i><b>Bold, and emphasized, barely a sentence.</b></p>
<div id="provided" class="middle">A div with a middle class and a provided id</div>
</div>
`})
}
func TestHTMLEscape(t *testing.T) {
testRender(t, Data{code: `
require("html").importGlobals()
return DIV {
P {
"<script>alert('not okay')</script>"
};
P {
["--noHTMLEscape"]=true;
"<script>alert('okay')</script>"
};
}
`, expected: `
<div>
<p><script>alert('not okay')</script></p>
<p>
<script>
alert('okay')
</script>
</p>
</div>
`})
}
func TestFragment(t *testing.T) {
testRender(t, Data{
code: `
require("html").importGlobals()
return DIV {
FRAGMENT(SPAN "one");
FRAGMENT{SPAN "two", SPAN "three"};
function()
return SPAN "four";
end;
{
SPAN "six";
SPAN "seven";
};
}
`,
expected: `
<div>
<span>one</span><span>two</span><span>three</span><span>four</span><span>six</span><span>seven</span>
</div>
`})
}
func TestOperator(t *testing.T) {
testRender(t, Data{
code: `
require("html").importGlobals()
return FRAGMENT {
P "111"
/ I "222"
/ B "333";
P ^ I ^ B "333"
}
`,
expected: `
<p>111<i>222</i><b>333</b></p>
<p>
<i><b>333</b></i>
</p>
`})
}
func TestMultilineParagraphs(t *testing.T) {
testRender(t, Data{
code: `
require("html").importGlobals()
return FRAGMENT {
PP {class="para"}
/ [[This is a paragraph.
This is another paragraph.
This one is a ]]
/ A {href="localhost", "link"}
/ [[.
Third paragraph.
Fourth ]] / EM "paragraph.";
PP [[
aaaaaaa
bbbbbbb
ccccccc
ddddddd
eeeeee ffff
ggggggggg
]]
}
`,
expected: `
<p class="para">
This is a paragraph.
</p>
<p class="para">
This is another paragraph.
This one is a <a href="localhost">link</a>.
</p>
<p class="para">
Third paragraph.
</p>
<p class="para">
Fourth <em>paragraph.</em>
</p>
<p>
aaaaaaa
bbbbbbb
</p>
<p>
ccccccc
ddddddd
</p>
<p>
eeeeee ffff
</p>
<p>
ggggggggg
</p>
<p>
</p>
`})
}
func TestBuildHook(t *testing.T) {
output, err := New().RenderFile("test/data/hook-build.html.lua")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "[hook]") {
t.Error("failed to add render hook")
t.Logf("output: \n%s", output)
}
}
func printComparison(expected, actual string) {
s := fmt.Sprintf("\n------[expected]------ \n%s\n------[ actual ]------\n%s", expected, actual)
println(s)
}
type Data struct {
code string
expected string
}
func testRender(t *testing.T, data Data) {
output, err := New().RenderString(data.code)
if err != nil {
t.Errorf("failed to execute lua: %s\n%s", err, data.code)
}
output = strings.TrimSpace(output)
expected := strings.TrimSpace(data.expected)
if expected != output {
t.Errorf("unexpected output")
printComparison(expected, output)
}
}