# 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"""
Test!
"""
const expected = html"""
Test!
"""
doAssert actual() == expected
block: #basic
proc actual: string = tmpli html"""
Test $$x
$x
"""
const expected = html"""
Test $x
5
"""
doAssert actual() == expected
block: #expression
proc actual: string = tmpli html"""
Test $$(x * 5)
$(x * 5)
"""
const expected = html"""
Test $(x * 5)
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"""
Test for
"""
const expected = html"""
Test for
"""
doAssert actual() == expected
block: #while
proc actual: string = tmpli html"""
Test while/stmt
${ var y = 0 }
$while y < 4 {
- $y
${ inc(y) }
}
"""
const expected = html"""
Test while/stmt
"""
doAssert actual() == expected
block: #ifElifElse
proc actual: string = tmpli html"""
Test if/elif/else
$if x == 8 {
x is 8!
}
$elif x == 7 {
x is 7!
}
$else {
x is neither!
}
"""
const expected = html"""
Test if/elif/else
x is neither!
"""
doAssert actual() == expected
block: #multiLineStatements
proc actual: string = tmpli html"""
Test multiline statements
${
var x = 5
var y = 7
}
$x$y
"""
const expected = html"""
Test multiline statements
57
"""
doAssert actual() == expected
block: #caseOfElse
proc actual: string = tmpli html"""
Test case
$case x
$of 5 {
x == 5
}
$of 6 {
x == 6
}
$else {}
"""
const expected = html"""
Test case
x == 5
"""
doAssert actual() == expected
when true: #embeddingTest
proc no_substitution: string = tmpli html"""
Template test!
"""
# # Single variable substitution
proc substitution(who = "nobody"): string = tmpli html"""
hello $who!
"""
# Expression template
proc test_expression(nums: openarray[int] = []): string =
var i = 2
tmpli html"""
$(no_substitution())
$(substitution("Billy"))
Age: $($nums[i] & "!!")
"""
proc test_statements(nums: openarray[int] = []): string =
tmpli html"""
$(test_expression(nums))
$if true {
$for i in nums {
- $(i * 2)
}
}
"""
var actual = test_statements([0,1,2])
const expected = html"""
Template test!
hello Billy!
Age: 2!!
"""
doAssert actual == expected
when defined(future):
block: #tryCatch
proc actual: string = tmpli html"""
Test try/catch
$try {
Lets try this!
}
$except {
Uh oh!
}
"""
const expected = html"""
Test try/catch
"""
doAssert actual() == expected