v2 / vlib / v / tests / tmpl_test.v
205 lines · 170 sloc · 4.27 KB · 743c34b4475e0c0bf061d82cb4dd3987cf51bf6e
Raw
1fn one() string {
2 name := 'Peter'
3 age := 25
4 numbers := [1, 2, 3]
5 downloads := {
6 'vlang/ui': '3201'
7 'vlang/vtl': '123'
8 }
9 ignored := true
10 return $tmpl('tmpl/base.txt')
11}
12
13fn outside_return() string {
14 name := 'Peter'
15 age := 25
16 numbers := [1, 2, 3]
17 downloads := {
18 'vlang/ui': '3201'
19 'vlang/vtl': '123'
20 }
21 ignored := true
22 result := $tmpl('tmpl/base.txt')
23 return result
24}
25
26fn test_tmpl() {
27 expected := "name: Peter
28age: 25
29numbers: [1, 2, 3]
30
311
322
333
34
350 - 0
362 - 1
374 - 2
386 - 3
398 - 4
4010 - 5
4112 - 6
4214 - 7
4316 - 8
4418 - 9
45
46vlang/ui, downloaded 3201 times.
47vlang/vtl, downloaded 123 times.
48
49this is not ignored
50
51so, it's basically true"
52
53 assert one().trim_space() == expected
54 assert outside_return().trim_space() == expected
55}
56
57fn test_tmpl_in_anon_fn() {
58 anon := fn (name string, age int, numbers []int, downloads map[string]string, ignored bool) string {
59 return $tmpl('tmpl/base.txt')
60 }
61
62 assert anon('Peter', 25, [1, 2, 3], {
63 'vlang/ui': '3201'
64 'vlang/vtl': '123'
65 }, true).trim_space() == "name: Peter
66age: 25
67numbers: [1, 2, 3]
68
691
702
713
72
730 - 0
742 - 1
754 - 2
766 - 3
778 - 4
7810 - 5
7912 - 6
8014 - 7
8116 - 8
8218 - 9
83
84vlang/ui, downloaded 3201 times.
85vlang/vtl, downloaded 123 times.
86
87this is not ignored
88
89so, it's basically true"
90}
91
92fn test_tmpl_interpolation() {
93 my_var := 'foo'
94 s := $tmpl('tmpl/interpolation.txt')
95 assert s == 'result: foo\n'
96}
97
98fn html_comment_tmpl() string {
99 return $tmpl('html_comment_template.html')
100}
101
102fn test_tmpl_html_comments_do_not_interpolate() {
103 result := html_comment_tmpl()
104 assert result.contains('<!-- @numbers -->')
105 assert result.contains('<!--\n@numbers\n-->')
106 assert result.contains('<p>hello</p>')
107}
108
109fn html_conditional_single_line_tmpl(optional bool) string {
110 content := 'hello!'
111 return $tmpl('tmpl/conditional_single_line.html')
112}
113
114fn html_conditional_multi_line_tmpl(optional bool) string {
115 content := 'hello!'
116 return $tmpl('tmpl/conditional_multi_line.html')
117}
118
119fn test_tmpl_html_conditional_single_line() {
120 result := html_conditional_single_line_tmpl(true)
121 assert result.trim_space() == '<html>
122
123<body>
124 <main>
125 <p>hello!</p>
126 <p>optional</p>
127 </main>
128</body>
129
130</html>'
131 assert !html_conditional_single_line_tmpl(false).contains('<p>optional</p>')
132}
133
134fn test_tmpl_html_conditional_multi_line() {
135 result := html_conditional_multi_line_tmpl(true)
136 assert result.trim_space() == '<html>
137
138<body>
139 <main>
140 <p>hello!</p>
141 <p>optional</p>
142 </main>
143</body>
144
145</html>'
146 assert !html_conditional_multi_line_tmpl(false).contains('<p>optional</p>')
147}
148
149fn map_index_tmpl() string {
150 lang := {
151 'test_entry': 'Test Text'
152 }
153 return $tmpl('tmpl/map_index.txt')
154}
155
156fn test_tmpl_map_index() {
157 assert map_index_tmpl().trim_space() == 'direct: Test Text
158paren: Test Text'
159}
160
161fn my_fn(s string) string {
162 return s
163}
164
165// Add more examples of potentially buggy patterns in vlib/v/tests/tmpl/index.html
166fn test_tmpl_comptime() {
167 index := $tmpl('tmpl/index.html').trim_space()
168 // dump(index)
169 assert index.contains('<br>Line ending with percent %\n')
170 assert index.contains('<br>Line ending with at @\n')
171 assert index.contains('<br>Line ending with ampersand &\n')
172 assert index.contains('<br>Line ending with hash #\n')
173 assert index.contains('<br>Line ending with slash /\n')
174 assert index.contains('<br>Line ending with dollar $\n')
175 assert index.contains('<br>Line ending with caret ^\n')
176}
177
178// Add a tests for @include
179
180// File contents for building repsonse
181const base = '<p>This is the base file</p>'
182const child = '<p>This is the child file</p>'
183const grandchild = '<p>This is the grandchild file</p>'
184const parent = '<p>This is the parent file</p>'
185
186// Call the parent file which contains all child templates
187fn test_tmpl_include_parent() {
188 expected := [base, base, child, base, base, child, grandchild, parent].join('\n')
189 parent_tmpl := $tmpl('tmpl/parent.html')
190 assert parent_tmpl.contains(expected)
191}
192
193// Test the child template which calls parent template
194fn test_tmpl_include_child() {
195 expected := [base, child].join('\n')
196 child_tmpl := $tmpl('tmpl/nested/child.html')
197 assert child_tmpl.contains(expected)
198}
199
200// Test the grandchild templates calling both parent and grandparent templates
201fn test_tmpl_include_grandchild() {
202 expected := [base, base, child, grandchild].join('\n')
203 child_tmpl := $tmpl('tmpl/nested/nested_deeper/grandchild.html')
204 assert child_tmpl.contains(expected)
205}
206