1
- import std/ [unittest, htmlparser, xmltree, strtabs, sequtils]
1
+ import std/ [unittest, os, htmlparser, xmltree, strtabs, sequtils]
2
2
import ../ src/ tim
3
3
4
4
var t = newTim (" ./app/templates" , " ./app/storage" ,
@@ -32,6 +32,9 @@ proc toHtml(id, code: string): (Parser, HtmlCompiler) =
32
32
result [0 ] = parseSnippet (id, code)
33
33
result [1 ] = newCompiler (result [0 ].getAst, false )
34
34
35
+ proc load (x: string ): string =
36
+ readFile (currentSourcePath ().parentDir / " snippets" / x & " .timl" )
37
+
35
38
test " assignment var" :
36
39
const code = """
37
40
var a = 123
@@ -42,13 +45,8 @@ var b = {}
42
45
check x[0 ].hasErrors == false
43
46
check x[1 ].hasErrors == false
44
47
45
- test " assignment const" :
46
- let code = """
47
- const x = 123
48
- h1: $x
49
- $x = 321
50
- """
51
- let x = toHtml (" test_var" , code)
48
+ test " invalid timl code" :
49
+ let x = toHtml (" invalid" , load (" invalid" ))
52
50
check x[0 ].hasErrors == false
53
51
check x[1 ].hasErrors == true
54
52
@@ -84,9 +82,10 @@ if 1 != 1:
84
82
elif 1 > 1:
85
83
span.just-some-basic-stuff: "this is basic"
86
84
else:
87
- span """
85
+ span.none
86
+ """
88
87
assert tim.toHtml (" test_if" , code) ==
89
- """ <span></span> """
88
+ """ <span class="none" ></span> """
90
89
91
90
test " loops for" :
92
91
let code = """
@@ -156,4 +155,50 @@ while $i != 0:
156
155
dec($i)
157
156
span: "Remained: " & $i.toString """
158
157
assert tim.toHtml (" test_while_dec" , code) ==
159
- """ <span>Remained: 0</span> """
158
+ """ <span>Remained: 0</span> """
159
+
160
+ test " function return string" :
161
+ let code = """
162
+ fn hello(x: string): string =
163
+ return $x
164
+ h1: hello("Tim is awesome!")
165
+ """
166
+ assert tim.toHtml (" test_function" , code) ==
167
+ """ <h1>Tim is awesome!</h1> """
168
+
169
+ test " function return int" :
170
+ let code = """
171
+ fn hello(x: int): int =
172
+ return $x + 10
173
+ h1: hello(7)
174
+ """
175
+ assert tim.toHtml (" test_function" , code) ==
176
+ """ <h1>17</h1> """
177
+
178
+ test " objects anonymous function" :
179
+ let code = """
180
+ @import "std/strings"
181
+ @import "std/os"
182
+
183
+ var x = {
184
+ getHello:
185
+ fn(x: string): string {
186
+ return toUpper($x & " World")
187
+ }
188
+ }
189
+ h1: $x.getHello("Hello")
190
+ """
191
+ assert tim.toHtml (" anonymous_function" , code) ==
192
+ """ <h1>HELLO WORLD</h1> """
193
+
194
+ test " std/strings" :
195
+ let x = toHtml (" std_strings" , load (" std_strings" ))
196
+ assert x[1 ].hasErrors == false
197
+
198
+ test " std/arrays" :
199
+ let x = toHtml (" std_arrays" , load (" std_arrays" ))
200
+ assert x[1 ].hasErrors == false
201
+
202
+ test " std/objects" :
203
+ let x = toHtml (" std_objects" , load (" std_objects" ))
204
+ assert x[1 ].hasErrors == false
0 commit comments