diff options
author | Petr Štetiar <ynezz@true.cz> | 2021-03-19 16:54:55 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-04-23 00:42:30 +0200 |
commit | 2b59097c3f61fa901e91ac4cea48940760439578 (patch) | |
tree | 958d739a78f959dfcd55b3d76e6e970ca53fa1c6 /tests/custom/00_syntax/15_function_declarations | |
parent | 80393611fb6634abcc0da1dee2da7c4418dbde8d (diff) |
tests: create custom tests from current tests cases
Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'tests/custom/00_syntax/15_function_declarations')
-rw-r--r-- | tests/custom/00_syntax/15_function_declarations | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/tests/custom/00_syntax/15_function_declarations b/tests/custom/00_syntax/15_function_declarations new file mode 100644 index 0000000..4257dd6 --- /dev/null +++ b/tests/custom/00_syntax/15_function_declarations @@ -0,0 +1,164 @@ +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 -- + + +Additionally, utpl implements ES6-like "rest" argument syntax to declare +variadic functions. + +-- Expect stdout -- +function non_variadic(a, b, c, d, e) { ... } +[ 1, 2, 3, 4, 5 ] +function variadic_1(a, b, ...args) { ... } +[ 1, 2, [ 3, 4, 5 ] ] +function variadic_2(...args) { ... } +[ [ 1, 2, 3, 4, 5 ] ] +-- End -- + +-- Testcase -- +{% + // ordinary, non-variadic function + function non_variadic(a, b, c, d, e) { + return [ a, b, c, d, e ]; + } + + // fixed amount of arguments with variable remainder + function variadic_1(a, b, ...args) { + return [ a, b, args ]; + } + + // only variable arguments + function variadic_2(...args) { + return [ args ]; + } + + print(join("\n", [ + non_variadic, + non_variadic(1, 2, 3, 4, 5), + variadic_1, + variadic_1(1, 2, 3, 4, 5), + variadic_2, + variadic_2(1, 2, 3, 4, 5) + ]), "\n"); +%} +-- End -- + + +Complementary to the "rest" argument syntax, the spread operator may be +used in function call arguments to pass arrays of values as argument list. + +-- Expect stdout -- +[ 1, 2, 3, 4, 5, 6 ] +[ 4, 5, 6, 1, 2, 3 ] +[ 1, 2, 3, 1, 2, 3 ] +[ 1, 2, 3 ] +-- End -- + +-- Testcase -- +{% + arr = [ 1, 2, 3 ]; + + function test(...args) { + return args; + } + + print(join("\n", [ + test(...arr, 4, 5, 6), + test(4, 5, 6, ...arr), + test(...arr, ...arr), + test(...arr) + ]), "\n"); +%} +-- End -- + + +Rest arguments may be only used once in a declaration and they must always +be the last item in the argument list. + +-- Expect stderr -- +Syntax error: Unexpected token +Expecting ')' +In line 2, byte 26: + + ` function illegal(...args, ...args2) {}` + Near here ------------------^ + + +-- End -- + +-- Testcase -- +{% + function illegal(...args, ...args2) {} +%} +-- End -- + +-- Expect stderr -- +Syntax error: Unexpected token +Expecting ')' +In line 2, byte 26: + + ` function illegal(...args, a, b) {}` + Near here ------------------^ + + +-- End -- + +-- Testcase -- +{% + function illegal(...args, a, b) {} +%} +-- End -- |