From c77a5bebc9ae8df17b851b6256cb42fa5d763dba Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 8 Sep 2020 21:19:06 +0200 Subject: tests: add further arithmetic and syntax test cases Signed-off-by: Jo-Philipp Wich --- tests/00_syntax/15_function_declarations | 59 ++++++++++++ tests/00_syntax/16_for_loop | 157 +++++++++++++++++++++++++++++++ tests/00_syntax/17_while_loop | 74 +++++++++++++++ tests/00_syntax/18_if_condition | 99 +++++++++++++++++++ 4 files changed, 389 insertions(+) create mode 100644 tests/00_syntax/15_function_declarations create mode 100644 tests/00_syntax/16_for_loop create mode 100644 tests/00_syntax/17_while_loop create mode 100644 tests/00_syntax/18_if_condition (limited to 'tests/00_syntax') diff --git a/tests/00_syntax/15_function_declarations b/tests/00_syntax/15_function_declarations new file mode 100644 index 0000000..846f379 --- /dev/null +++ b/tests/00_syntax/15_function_declarations @@ -0,0 +1,59 @@ +Function declarations follow the ECMAScript 5 syntax. Functions can be +declared anonymously, which is useful for "throw-away" functions such +as sort or filter callbacks or for building objects or arrays of function +values. + +If functions are declared with a name, the resulting function value is +automatically assigned under the given name to the current scope. + +When function values are stringifed, the resulting string will describe +the declaration of the function. + +Nesting function declarations is possible as well. + + +-- Expect stdout -- +function() { ... } +function test_fn(a, b) { ... } +function test2_fn(a, b) { ... } + + +A function declaration using the alternative syntax: +The function was called with arguments 123 and 456. +-- End -- + +-- Testcase -- +{% + // declare an anonymous function and + // assign resulting value + anon_fn = function() { + return "test"; + }; + + // declare a named function + function test_fn(a, b) { + return a + b; + } + + // nesting functions is legal + function test2_fn(a, b) { + function test3_fn(a, b) { + return a * b; + } + + return a + test3_fn(a, b); + } + + print(anon_fn, "\n"); + print(test_fn, "\n"); + print(test2_fn, "\n"); +%} + +A function declaration using the alternative syntax: + +{%- function test3_fn(a, b): %} +The function was called with arguments {{ a }} and {{ b }}. +{%- endfunction -%} + +{{ test3_fn(123, 456) }} +-- End -- diff --git a/tests/00_syntax/16_for_loop b/tests/00_syntax/16_for_loop new file mode 100644 index 0000000..61e86a1 --- /dev/null +++ b/tests/00_syntax/16_for_loop @@ -0,0 +1,157 @@ +Two for-loop variants are supported: a C-style counting for loop +consisting of an initialization expression, a test condition +and a step expression and a for-in-loop variant which allows +enumerating properties of objects or items of arrays. + +Additionally, utpl supports an alternative syntax suitable for +template block tags. + + +-- Expect stdout -- +A simple counting for-loop: +Iteration 0 +Iteration 1 +Iteration 2 +Iteration 3 +Iteration 4 +Iteration 5 +Iteration 6 +Iteration 7 +Iteration 8 +Iteration 9 + + +If the loop body consists of only one statement, the curly braces +may be omitted: +Iteration 0 +Iteration 1 +Iteration 2 +Iteration 3 +Iteration 4 +Iteration 5 +Iteration 6 +Iteration 7 +Iteration 8 +Iteration 9 + + +Any of the init-, test- and increment expressions may be omitted. + +Loop without initialization statement: +Iteration null +Iteration 1 +Iteration 2 + + +Loop without test statement: +Iteration 0 +Iteration 1 +Iteration 2 + + +Loop without init-, test- or increment statement: +Iteration 1 +Iteration 2 +Iteration 3 + + +For-in loop enumerating object properties: +Key: foo +Key: bar + + +For-in loop enumerating array items: +Item: one +Item: two +Item: three + + +A counting for-loop using the alternative syntax: +Iteration 0 +Iteration 1 +Iteration 2 +Iteration 3 +Iteration 4 +Iteration 5 +Iteration 6 +Iteration 7 +Iteration 8 +Iteration 9 + + +A for-in loop using the alternative syntax: +Item 123 +Item 456 +Item 789 + +-- End -- + +-- Testcase -- +A simple counting for-loop: +{% + for (i = 0; i < 10; i++) { + print("Iteration "); + print(i); + print("\n"); + } +%} + +If the loop body consists of only one statement, the curly braces +may be omitted: +{% + for (i = 0; i < 10; i++) + print("Iteration ", i, "\n"); +%} + +Any of the init-, test- and increment expressions may be omitted. + +Loop without initialization statement: +{% + for (; j < 3; j++) + print("Iteration " + j + "\n"); +%} + +Loop without test statement: +{% + for (j = 0;; j++) { + if (j == 3) + break; + + print("Iteration ", j, "\n"); + } +%} + +Loop without init-, test- or increment statement: +{% + for (;;) { + if (k++ == 3) + break; + + print("Iteration ", k, "\n"); + } +%} + +For-in loop enumerating object properties: +{% + obj = { foo: true, bar: false }; + for (key in obj) + print("Key: ", key, "\n"); +%} + +For-in loop enumerating array items: +{% + arr = [ "one", "two", "three" ]; + for (item in arr) + print("Item: ", item, "\n"); +%} + +A counting for-loop using the alternative syntax: +{% for (x = 0; x < 10; x++): -%} +Iteration {{ x }} +{% endfor %} + +A for-in loop using the alternative syntax: +{% for (n in [123, 456, 789]): -%} +Item {{ n }} +{% endfor %} +-- End -- diff --git a/tests/00_syntax/17_while_loop b/tests/00_syntax/17_while_loop new file mode 100644 index 0000000..6d6dc4a --- /dev/null +++ b/tests/00_syntax/17_while_loop @@ -0,0 +1,74 @@ +Utpl implements C-style while loops which run as long as the condition +is fulfilled. + +Like with for-loops, an alternative syntax form suitable for template +blocks is supported. + + +-- Expect stdout -- +A simple counting while-loop: +Iteration 0 +Iteration 1 +Iteration 2 +Iteration 3 +Iteration 4 +Iteration 5 +Iteration 6 +Iteration 7 +Iteration 8 +Iteration 9 + + +If the loop body consists of only one statement, the curly braces +may be omitted: +Iteration 0 +Iteration 1 +Iteration 2 +Iteration 3 +Iteration 4 +Iteration 5 +Iteration 6 +Iteration 7 +Iteration 8 +Iteration 9 + + +A counting while-loop using the alternative syntax: +Iteration null +Iteration 1 +Iteration 2 +Iteration 3 +Iteration 4 +Iteration 5 +Iteration 6 +Iteration 7 +Iteration 8 +Iteration 9 + +-- End -- + +-- Testcase -- +A simple counting while-loop: +{% + i = 0; + while (i < 10) { + print("Iteration "); + print(i); + print("\n"); + i++; + } +%} + +If the loop body consists of only one statement, the curly braces +may be omitted: +{% + i = 0; + while (i < 10) + print("Iteration ", i++, "\n"); +%} + +A counting while-loop using the alternative syntax: +{% while (x < 10): -%} +Iteration {{ "" + x++ }} +{% endwhile %} +-- End -- 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 -- -- cgit v1.2.3