blob: 120146343bd139ed3008fa79dcbd3c0f6ba7f48c (
plain) (
blame)
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
|
# Fields
const x = 5
# Test substring
static:
assert "test".substring(3) == "t"
assert "test".substring(2,1) == "s"
assert "test".substring(3,2) == "t"
assert "test".substring(1,2) == "es"
# Various parsing tests
when true:
block: #no_substitution
proc actual: string = tmpli html"""
<p>Test!</p>
"""
const expected = html"""
<p>Test!</p>
"""
doAssert actual() == expected
block: #basic
proc actual: string = tmpli html"""
<p>Test $$x</p>
$x
"""
const expected = html"""
<p>Test $x</p>
5
"""
doAssert actual() == expected
block: #expression
proc actual: string = tmpli html"""
<p>Test $$(x * 5)</p>
$(x * 5)
"""
const expected = html"""
<p>Test $(x * 5)</p>
25
"""
doAssert actual() == expected
block: #escape
proc actual: string = tmpli js"""
[{
"hello world"
}]
"""
const expected = js"""
[{
"hello world"
}]
"""
doAssert actual() == expected
block: #forIn
proc actual: string = tmpli html"""
<p>Test for</p>
<ul>
$for y in 0..2 {
<li>$y</li>
}
</ul>
"""
const expected = html"""
<p>Test for</p>
<ul>
<li>0</li>
<li>1</li>
<li>2</li>
</ul>
"""
doAssert actual() == expected
block: #while
proc actual: string = tmpli html"""
<p>Test while/stmt</p>
<ul>
${ var y = 0 }
$while y < 4 {
<li>$y</li>
${ inc(y) }
}
</ul>
"""
const expected = html"""
<p>Test while/stmt</p>
<ul>
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
"""
doAssert actual() == expected
block: #ifElifElse
proc actual: string = tmpli html"""
<p>Test if/elif/else</p>
$if x == 8 {
<div>x is 8!</div>
}
$elif x == 7 {
<div>x is 7!</div>
}
$else {
<div>x is neither!</div>
}
"""
const expected = html"""
<p>Test if/elif/else</p>
<div>x is neither!</div>
"""
doAssert actual() == expected
block: #multiLineStatements
proc actual: string = tmpli html"""
<p>Test multiline statements</p>
${
var x = 5
var y = 7
}
<span>$x</span><span>$y</span>
"""
const expected = html"""
<p>Test multiline statements</p>
<span>5</span><span>7</span>
"""
doAssert actual() == expected
block: #caseOfElse
proc actual: string = tmpli html"""
<p>Test case</p>
$case x
$of 5 {
<div>x == 5</div>
}
$of 6 {
<div>x == 6</div>
}
$else {}
"""
const expected = html"""
<p>Test case</p>
<div>x == 5</div>
"""
doAssert actual() == expected
when true: #embeddingTest
proc no_substitution: string = tmpli html"""
<h1>Template test!</h1>
"""
# # Single variable substitution
proc substitution(who = "nobody"): string = tmpli html"""
<div id="greeting">hello $who!</div>
"""
# Expression template
proc test_expression(nums: openarray[int] = []): string =
var i = 2
tmpli html"""
$(no_substitution())
$(substitution("Billy"))
<div id="age">Age: $($nums[i] & "!!")</div>
"""
proc test_statements(nums: openarray[int] = []): string =
tmpli html"""
$(test_expression(nums))
$if true {
<ul>
$for i in nums {
<li>$(i * 2)</li>
}
</ul>
}
"""
var actual = test_statements([0,1,2])
const expected = html"""
<h1>Template test!</h1>
<div id="greeting">hello Billy!</div>
<div id="age">Age: 2!!</div>
<ul>
<li>0</li>
<li>2</li>
<li>4</li>
</ul>
"""
doAssert actual == expected
when defined(future):
block: #tryCatch
proc actual: string = tmpli html"""
<p>Test try/catch</p>
<div>
$try {
<div>Lets try this!</div>
}
$except {
<div>Uh oh!</div>
}
</div>
"""
const expected = html"""
<p>Test try/catch</p>
<div>
<div>Lets try this!</div>
</div>
"""
doAssert actual() == expected
|