| 1 | fn 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 | |
| 13 | fn 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 | |
| 26 | fn test_tmpl() { |
| 27 | expected := "name: Peter |
| 28 | age: 25 |
| 29 | numbers: [1, 2, 3] |
| 30 | |
| 31 | 1 |
| 32 | 2 |
| 33 | 3 |
| 34 | |
| 35 | 0 - 0 |
| 36 | 2 - 1 |
| 37 | 4 - 2 |
| 38 | 6 - 3 |
| 39 | 8 - 4 |
| 40 | 10 - 5 |
| 41 | 12 - 6 |
| 42 | 14 - 7 |
| 43 | 16 - 8 |
| 44 | 18 - 9 |
| 45 | |
| 46 | vlang/ui, downloaded 3201 times. |
| 47 | vlang/vtl, downloaded 123 times. |
| 48 | |
| 49 | this is not ignored |
| 50 | |
| 51 | so, it's basically true" |
| 52 | |
| 53 | assert one().trim_space() == expected |
| 54 | assert outside_return().trim_space() == expected |
| 55 | } |
| 56 | |
| 57 | fn 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 |
| 66 | age: 25 |
| 67 | numbers: [1, 2, 3] |
| 68 | |
| 69 | 1 |
| 70 | 2 |
| 71 | 3 |
| 72 | |
| 73 | 0 - 0 |
| 74 | 2 - 1 |
| 75 | 4 - 2 |
| 76 | 6 - 3 |
| 77 | 8 - 4 |
| 78 | 10 - 5 |
| 79 | 12 - 6 |
| 80 | 14 - 7 |
| 81 | 16 - 8 |
| 82 | 18 - 9 |
| 83 | |
| 84 | vlang/ui, downloaded 3201 times. |
| 85 | vlang/vtl, downloaded 123 times. |
| 86 | |
| 87 | this is not ignored |
| 88 | |
| 89 | so, it's basically true" |
| 90 | } |
| 91 | |
| 92 | fn test_tmpl_interpolation() { |
| 93 | my_var := 'foo' |
| 94 | s := $tmpl('tmpl/interpolation.txt') |
| 95 | assert s == 'result: foo\n' |
| 96 | } |
| 97 | |
| 98 | fn html_comment_tmpl() string { |
| 99 | return $tmpl('html_comment_template.html') |
| 100 | } |
| 101 | |
| 102 | fn 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 | |
| 109 | fn html_conditional_single_line_tmpl(optional bool) string { |
| 110 | content := 'hello!' |
| 111 | return $tmpl('tmpl/conditional_single_line.html') |
| 112 | } |
| 113 | |
| 114 | fn html_conditional_multi_line_tmpl(optional bool) string { |
| 115 | content := 'hello!' |
| 116 | return $tmpl('tmpl/conditional_multi_line.html') |
| 117 | } |
| 118 | |
| 119 | fn 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 | |
| 134 | fn 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 | |
| 149 | fn map_index_tmpl() string { |
| 150 | lang := { |
| 151 | 'test_entry': 'Test Text' |
| 152 | } |
| 153 | return $tmpl('tmpl/map_index.txt') |
| 154 | } |
| 155 | |
| 156 | fn test_tmpl_map_index() { |
| 157 | assert map_index_tmpl().trim_space() == 'direct: Test Text |
| 158 | paren: Test Text' |
| 159 | } |
| 160 | |
| 161 | fn 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 |
| 166 | fn 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 |
| 181 | const base = '<p>This is the base file</p>' |
| 182 | const child = '<p>This is the child file</p>' |
| 183 | const grandchild = '<p>This is the grandchild file</p>' |
| 184 | const parent = '<p>This is the parent file</p>' |
| 185 | |
| 186 | // Call the parent file which contains all child templates |
| 187 | fn 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 |
| 194 | fn 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 |
| 201 | fn 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 | |