diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-09-08 21:19:06 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-09-08 21:19:06 +0200 |
commit | c77a5bebc9ae8df17b851b6256cb42fa5d763dba (patch) | |
tree | 77c7dac828ad7ef8bad17972f2a256ac632fbea3 /tests/00_syntax/18_if_condition | |
parent | c735882bb492ff81f98773186652dbe878ff3d60 (diff) |
tests: add further arithmetic and syntax test cases
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/00_syntax/18_if_condition')
-rw-r--r-- | tests/00_syntax/18_if_condition | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/00_syntax/18_if_condition b/tests/00_syntax/18_if_condition new file mode 100644 index 0000000..0148466 --- /dev/null +++ b/tests/00_syntax/18_if_condition @@ -0,0 +1,99 @@ +Utpl implements C-style if/else conditions and ?: ternary statements. + +Like with for- and while-loops, an alternative syntax form suitable +for template blocks is supported. + + +-- Expect stdout -- +This should print "one": +one + +This should print "two": +two + +Multiple conditions can be used by chaining if/else statements: +three + +If the conditional block consists of only one statement, the curly +braces may be omitted: +two + +An if-condition using the alternative syntax: +Variable x has another value. + + +Ternary expressions function similar to if/else statements but +only allow for a single expression in the true and false branches: +Variable x is one +-- End -- + +-- Testcase -- +This should print "one": +{% + x = 0; + + if (x == 0) { + print("one"); + } + else { + print("two"); + } +%} + +This should print "two": +{% + x = 1; + + if (x == 0) { + print("one"); + } + else { + print("two"); + } +%} + +Multiple conditions can be used by chaining if/else statements: +{% + x = 2; + + if (x == 0) { + print("one"); + } + else if (x == 1) { + print("two"); + } + else if (x == 2) { + print("three"); + } + else { + print("four"); + } +%} + +If the conditional block consists of only one statement, the curly +braces may be omitted: +{% + x = 5; + + if (x == 0) + print("one"); + else + print("two"); +%} + +An if-condition using the alternative syntax: +{% if (x == 1): -%} +Variable x was set to one. +{% else -%} +Variable x has another value. +{% endif %} + +Ternary expressions function similar to if/else statements but +only allow for a single expression in the true and false branches: +{% + x = 1; + s = (x == 1) ? "Variable x is one" : "Variable x has another value"; + + print(s); +%} +-- End -- |