diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/00_syntax/15_function_declarations | 59 | ||||
-rw-r--r-- | tests/00_syntax/16_for_loop | 157 | ||||
-rw-r--r-- | tests/00_syntax/17_while_loop | 74 | ||||
-rw-r--r-- | tests/00_syntax/18_if_condition | 99 | ||||
-rw-r--r-- | tests/01_arithmetic/03_bitwise | 54 | ||||
-rw-r--r-- | tests/01_arithmetic/04_inc_dec | 50 |
6 files changed, 493 insertions, 0 deletions
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 -- diff --git a/tests/01_arithmetic/03_bitwise b/tests/01_arithmetic/03_bitwise new file mode 100644 index 0000000..faf4ffd --- /dev/null +++ b/tests/01_arithmetic/03_bitwise @@ -0,0 +1,54 @@ +Utpl implements C-style bitwise operations. One detail is that these operations +coerce their operands to signed 64bit integer values internally. + +-- Expect stdout -- +Left shift: +10 << 2 = 40 +3.3 << 4.1 = 48 + +Right shift: +10 >> 2 = 2 +3.3 >> 4.1 = 0 + +Bitwise and: +1234 & 200 = 192 +120.3 & 54.3 = 48 + +Bitwise xor: +1234 ^ 200 = 1050 +120.3 ^ 54.3 = 78 + +Bitwise or: +1234 | 200 = 1242 +120.3 | 54.3 = 126 + +Complement: +~0 = -1 +~10.4 = -11 +-- End -- + +-- Testcase -- +Left shift: +10 << 2 = {{ 10 << 2 }} +3.3 << 4.1 = {{ 3.3 << 4.1 }} + +Right shift: +10 >> 2 = {{ 10 >> 2 }} +3.3 >> 4.1 = {{ 3.3 >> 4.1 }} + +Bitwise and: +1234 & 200 = {{ 1234 & 200 }} +120.3 & 54.3 = {{ 120.3 & 54.3 }} + +Bitwise xor: +1234 ^ 200 = {{ 1234 ^ 200 }} +120.3 ^ 54.3 = {{ 120.3 ^ 54.3 }} + +Bitwise or: +1234 | 200 = {{ 1234 | 200 }} +120.3 | 54.3 = {{ 120.3 | 54.3 }} + +Complement: +~0 = {{ ~0 }} +~10.4 = {{ ~10.4 }} +-- End -- diff --git a/tests/01_arithmetic/04_inc_dec b/tests/01_arithmetic/04_inc_dec new file mode 100644 index 0000000..6647ced --- /dev/null +++ b/tests/01_arithmetic/04_inc_dec @@ -0,0 +1,50 @@ +Utpl implements C-style pre- and postfix increment and decrement operators. + +Pre-increment or -decrement operations first mutate the value and then return +the resulting value while post-increment or -decrement operations first return +the initial value and then mutate the operand. + +Since the decrement and increment operators mutate their operand, they +may only be applied to variables, not constant literal expressions. + +If an undefined variable is incremented or decremented, its initial value +is assumed to be "0". + +If a non-numeric value is incremented or decremented, it is converted to a +number first. If the value is not convertible, the result of the increment +or decrement operation is NaN. + +-- Expect stdout -- +Incrementing a not existing variable assumes "0" as initial value: + + - Postfix increment result: null, value after: 1 + - Prefix increment result: 1, value after: 1 + - Postfix decrement result: null, value after: -1 + - Prefix decrement result: -1, value after: -1 + +Incrementing a non-numeric value will convert it to a number: + +124 +3.5 +2 +NaN + +-- End -- + +-- Testcase -- +Incrementing a not existing variable assumes "0" as initial value: + + - Postfix increment result: {{ "" + a++ }}, value after: {{ a }} + - Prefix increment result: {{ "" + ++b }}, value after: {{ b }} + - Postfix decrement result: {{ "" + c-- }}, value after: {{ c }} + - Prefix decrement result: {{ "" + --d }}, value after: {{ d }} + +Incrementing a non-numeric value will convert it to a number: + +{% + n = "123"; n++; print(n, "\n"); + n = "4.5"; n--; print(n, "\n"); + n = true; n++; print(n, "\n"); + n = { some: "object" }; n--; print(n, "\n"); +%} +-- End -- |