-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
readme_test.v
253 lines (213 loc) · 4.26 KB
/
readme_test.v
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
module main
import template
import datamodel
fn test_normal_text() {
input := 'Sample text'
simple_template := template.from_strings(
input: input
)!
assert simple_template.run(false)! == 'Sample text'
}
fn test_first_example() {
simple_template := template.from_strings(input: 'Hello, {{name}}!')!
data := datamodel.from_json('{"name": "World"}')!
assert simple_template.run(data)! == 'Hello, World!'
}
fn test_double_curly_braces_indicate_sections() {
input := 'Hello, {{name}}!'
simple_template := template.from_strings(
input: input
)!
data := datamodel.from_json('{
"name": "world"
}')!
assert simple_template.run(data)! == 'Hello, world!'
}
fn test_changing_delimiters() {
input := "
{{=[ ]=}}
module main
fn main() {
println('[greeting]')
}".trim_indent()
data := datamodel.from_json('{
"greeting": "Have a nice day!"
}')!
delimiter_template := template.from_strings(input: input)!
assert delimiter_template.run(data)!.trim_space() == "
module main
fn main() {
println('Have a nice day!')
}".trim_indent()
}
fn test_boolean_positive_negative_sections() {
input := '
<nav>
<ul>
<li>Home</li>
<li>About</li>
{{-logged_in}}<li>Log In</li>{{/logged_in}}
{{+logged_in}}<li>Account: {{user.name}}</li>{{/logged_in}}
</ul>
</nav>
'.trim_indent()
data_list := [
datamodel.from_json('{
"logged_in": false,
}')!,
datamodel.from_json('{
"logged_in": true,
"user": {
"name": "whisker"
}
}')!,
]
outputs := [
'
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Log In</li>
</ul>
</nav>
'.trim_indent(),
'
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Account: whisker</li>
</ul>
</nav>
'.trim_indent(),
]
section_template := template.from_strings(input: input)!
for index, data in data_list {
assert section_template.run(data)! == outputs[index]
}
}
fn test_list_positive_negative_sections() {
input := '
{{+vacation}}
<h1>Currently on vacation</h1>
<ul>
{{*.}}
<li>{{.}}</li>
{{/.}}
</ul>
{{/vacation}}
{{-vacation}}
<p>Nobody is on vacation currently</p>
{{/vacation}}
'.trim_indent()
data_list := [
datamodel.from_json('{
"vacation": []
}')!,
datamodel.from_json('{
"vacation": ["Homer", "Marge"]
}')!,
]
outputs := [
'
<p>Nobody is on vacation currently</p>
'.trim_indent(),
'
<h1>Currently on vacation</h1>
<ul>
<li>Homer</li>
<li>Marge</li>
</ul>
'.trim_indent(),
]
section_template := template.from_strings(input: input)!
for index, data in data_list {
assert section_template.run(data)! == outputs[index]
}
}
fn test_map_key_value_iteration() {
input := '
<ul>
{{*user}}
<li>{{key}}: {{value}}</li>
{{/user}}
</ul>
'.trim_indent()
data := datamodel.from_json('{
"user": {
"First Name": "Homer",
"Last Name": "Simpson"
}
}')!
section_template := template.from_strings(input: input)!
assert section_template.run(data)! == '
<ul>
<li>First Name: Homer</li>
<li>Last Name: Simpson</li>
</ul>
'.trim_indent()
}
fn test_map_positive_negative_sections() {
input := '
{{+user}}
<p>Welcome {{last_name}}, {{first_name}}</h1>
{{/user}}
{{-user}}
<p>Create account?</p>
{{/user}}
'.trim_indent()
data_list := [
datamodel.from_json('{
"user" : {}
}')!,
datamodel.from_json('{
"user" : {
"last_name": "Simpson",
"first_name": "Homer"
}
}')!,
]
outputs := [
'
<p>Create account?</p>
'.trim_indent(),
'
<p>Welcome Simpson, Homer</h1>
'.trim_indent(),
]
section_template := template.from_strings(input: input)!
for index, data in data_list {
assert section_template.run(data)! == outputs[index]
}
}
fn test_maps_lists_partials() {
input := '
<ol>
{{*items}}
{{>item}}
{{/items}}
</ol>'.trim_indent()
partials := {
'item': '<li>{{name}}: {{description}}</li>\n'
}
data := datamodel.from_json('{
"items": [
{
"name": "Banana",
"description": "Rich in potassium and naturally sweet."
},
{
"name": "Orange",
"description": "High in Vitamin C and very refreshing."
}
]
}')!
advanced_template := template.from_strings(input: input, partials: partials)!
assert advanced_template.run(data)! == '
<ol>
<li>Banana: Rich in potassium and naturally sweet.</li>
<li>Orange: High in Vitamin C and very refreshing.</li>
</ol>
'.trim_indent()
}