summaryrefslogtreecommitdiffhomepage
path: root/tests/00_syntax
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-11-03 16:49:31 +0100
committerJo-Philipp Wich <jo@mein.io>2020-11-03 16:49:54 +0100
commit01deebeae5f615aff8fd8a3bb35280e60e2082ad (patch)
tree8c488925d73247ee4264969105987f1e3b703b74 /tests/00_syntax
parente8e7692f169ae00434f3e0959f104d95284d2891 (diff)
syntax: implement ES6-like spread operator
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/00_syntax')
-rw-r--r--tests/00_syntax/13_object_literals41
-rw-r--r--tests/00_syntax/14_array_literals57
-rw-r--r--tests/00_syntax/15_function_declarations28
3 files changed, 126 insertions, 0 deletions
diff --git a/tests/00_syntax/13_object_literals b/tests/00_syntax/13_object_literals
index 4b4f934..60c9f32 100644
--- a/tests/00_syntax/13_object_literals
+++ b/tests/00_syntax/13_object_literals
@@ -49,3 +49,44 @@ either JSON or JavaScript notation.
print(third_obj, "\n");
print(nested_obj, "\n");
-- End --
+
+
+Additionally, utpl implements ES6-like spread operators to allow shallow copying
+of object properties into other objects.
+
+-- Expect stdout --
+{ "foo": true, "bar": false }
+{ "foo": true, "bar": false, "baz": 123, "qrx": 456 }
+{ "foo": false, "bar": true, "baz": 123, "qrx": 456 }
+{ "foo": true, "bar": false }
+{ "foo": true, "bar": false, "level2": { "baz": 123, "qrx": 456 } }
+{ "foo": true, "bar": false, "0": 7, "1": 8, "2": 9 }
+-- End --
+
+-- Testcase --
+{%
+ o1 = { foo: true, bar: false };
+ o2 = { baz: 123, qrx: 456 };
+ arr = [7, 8, 9];
+
+ print(join("\n", [
+ // copying one object into another
+ { ...o1 },
+
+ // combining two objects
+ { ...o1, ...o2 },
+
+ // copying object and override properties
+ { ...o1, ...o2, foo: false, bar: true },
+
+ // default properties overwritten by spread operator
+ { foo: 123, bar: 456, ...o1 },
+
+ // nested spread operators
+ { ...o1, level2: { ...o2 } },
+
+ // merging array into objects
+ { ...o1, ...arr }
+ ]), "\n");
+%}
+-- End --
diff --git a/tests/00_syntax/14_array_literals b/tests/00_syntax/14_array_literals
index ea5f9c0..941ee4a 100644
--- a/tests/00_syntax/14_array_literals
+++ b/tests/00_syntax/14_array_literals
@@ -23,3 +23,60 @@ The utpl script language supports declaring arrays using JSON notation.
print(empty_array, "\n");
print(json_array, "\n");
-- End --
+
+
+Additionally, utpl implements ES6-like spread operators to allow shallow copying
+of array values into other arrays.
+
+-- Expect stdout --
+[ 1, 2, 3 ]
+[ 1, 2, 3, 4, 5, 6 ]
+[ 1, 2, 3, 4, 5, 6, false, true ]
+[ 1, 2, 3, false, true, 4, 5, 6 ]
+[ 1, 2, 3, [ 4, 5, 6 ] ]
+-- End --
+
+-- Testcase --
+{%
+ a1 = [ 1, 2, 3 ];
+ a2 = [ 4, 5, 6 ];
+
+ print(join("\n", [
+ // copying one array into another
+ [ ...a1 ],
+
+ // combining two arrays
+ [ ...a1, ...a2 ],
+
+ // copying array and append values
+ [ ...a1, ...a2, false, true ],
+
+ // copy array and interleave values
+ [ ...a1, false, true, ...a2 ],
+
+ // nested spread operators
+ [ ...a1, [ ...a2 ] ]
+ ]), "\n");
+%}
+-- End --
+
+Contrary to merging arrays into objects, objects cannot be merged into arrays.
+
+-- Expect stderr --
+Type error: ({ "foo": true, "bar": false }) is not iterable
+In line 5, byte 21:
+
+ ` print([ ...arr, ...obj ], "\n");`
+ Near here -------------^
+
+
+-- End --
+
+-- Testcase --
+{%
+ arr = [ 1, 2, 3 ];
+ obj = { foo: true, bar: false };
+
+ print([ ...arr, ...obj ], "\n");
+%}
+-- End --
diff --git a/tests/00_syntax/15_function_declarations b/tests/00_syntax/15_function_declarations
index 1ed6f83..f02f90a 100644
--- a/tests/00_syntax/15_function_declarations
+++ b/tests/00_syntax/15_function_declarations
@@ -96,3 +96,31 @@ function variadic_2(...args) { ... }
]), "\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 --