summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/17_splice
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-01-29 23:31:16 +0100
committerJo-Philipp Wich <jo@mein.io>2022-02-03 17:22:43 +0100
commit7edad5cefa0f065aa83dffd2d7830aeaf9f38662 (patch)
tree86b727f434302ffb28cb59278243517f9765e170 /tests/custom/03_stdlib/17_splice
parentd5003fde57eab19588da7bfdbaefe93d47435eb6 (diff)
tests: add functional tests for builtin functions
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/03_stdlib/17_splice')
-rw-r--r--tests/custom/03_stdlib/17_splice98
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/custom/03_stdlib/17_splice b/tests/custom/03_stdlib/17_splice
new file mode 100644
index 0000000..f8fb9a2
--- /dev/null
+++ b/tests/custom/03_stdlib/17_splice
@@ -0,0 +1,98 @@
+The `splice()` function performs in-place addition and removal of elements
+on the given array.
+
+If no offset, remove count and additional items are supplied, all elements
+are removed from the array.
+
+If just an offset, but no remove count and not additional items are given,
+all elements beginning with the given offset until the end of the array
+are removed.
+
+If at least an offset and a remove count are given, then that amount of
+items are removed from the array, beginning at the specified offset. Any
+further supplied additional item (if any) is inserted in the same order
+beginning at the given offset.
+
+If either the offset or the remove count are negative, they're treated
+as counting towards the end of the array. If either value exceeds the
+array length, it is capped to the length of the array.
+
+Returns the modified input array.
+
+Returns `null` if the given input array value is not an array.
+
+
+-- Testcase --
+{%
+ let arr = [ 6, 4.3, 1, 45, 3.01, 2 ];
+
+ print(join("\n", [
+ // remove all items
+ splice([ ...arr ]),
+
+ // remove all items from index 4 till end
+ splice([ ...arr ], 4),
+
+ // remove item 2 and 3
+ splice([ ...arr ], 1, 2),
+
+ // remove last two items
+ splice([ ...arr ], -2),
+
+ // remove items 4 and 5
+ splice([ ...arr ], -3, -1),
+
+ // replace item 2
+ splice([ ...arr ], 1, 1, 7.9),
+
+ // add item between 3 and 4
+ splice([ ...arr ], 3, 0, 34),
+
+ // append three items
+ splice([ ...arr ], length(arr), 0, 123, 456, 789)
+ ]), "\n");
+%}
+-- End --
+
+-- Expect stdout --
+[ ]
+[ 6, 4.3, 1, 45 ]
+[ 6, 45, 3.01, 2 ]
+[ 6, 4.3, 1, 45 ]
+[ 6, 4.3, 1, 2 ]
+[ 6, 7.9, 1, 45, 3.01, 2 ]
+[ 6, 4.3, 1, 34, 45, 3.01, 2 ]
+[ 6, 4.3, 1, 45, 3.01, 2, 123, 456, 789 ]
+-- End --
+
+
+Supplying an invalid array will yield `null`.
+
+-- Testcase --
+{%
+ printf("%.J\n", splice("not_an_array", 0, 1));
+%}
+-- End --
+
+-- Expect stdout --
+null
+-- End --
+
+
+Invalid, non-numeric offset or index values are treated as 0.
+
+-- Testcase --
+{%
+ let arr = [ 6, 4.3, 1, 45, 3.01, 2 ];
+
+ print(join("\n", [
+ splice([ ...arr ], "foo", "bar"),
+ splice([ ...arr ], "foo", "bar", "baz")
+ ]), "\n");
+%}
+-- End --
+
+-- Expect stdout --
+[ 6, 4.3, 1, 45, 3.01, 2 ]
+[ "baz", 6, 4.3, 1, 45, 3.01, 2 ]
+-- End --