summaryrefslogtreecommitdiffhomepage
path: root/tests/00_syntax/15_function_declarations
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-11-03 11:29:10 +0100
committerJo-Philipp Wich <jo@mein.io>2020-11-03 11:29:10 +0100
commite8e7692f169ae00434f3e0959f104d95284d2891 (patch)
tree8e4507bd6abe5a31c3538e45f0f6729028856826 /tests/00_syntax/15_function_declarations
parent3cf36bbefa1c659861ef3376f3c4d97f1c9db844 (diff)
syntax: implement ES6-like rest parameters for variadic functions
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/00_syntax/15_function_declarations')
-rw-r--r--tests/00_syntax/15_function_declarations41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/00_syntax/15_function_declarations b/tests/00_syntax/15_function_declarations
index cb391a4..1ed6f83 100644
--- a/tests/00_syntax/15_function_declarations
+++ b/tests/00_syntax/15_function_declarations
@@ -55,3 +55,44 @@ 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 --