summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/66_unshift
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-01-23 11:36:31 +0100
committerJo-Philipp Wich <jo@mein.io>2023-01-23 11:36:31 +0100
commitf1be0d725735fd8dd489494b00e954a93223f722 (patch)
tree2a433e6506d6cd3a61bf75f4829de46f22c9a9af /tests/custom/03_stdlib/66_unshift
parent48a6eac1da1584ea784fd7d53f761baa87ae84d2 (diff)
types: fix array unshift operations and add test coverage
- Fix `ucv_array_unshift()` improperly rejecting operation on empty arrays - Fix `uc_unshift()` improperly reversing maintaining argument order - Add missing test coverage for `push()`, `pop()`, `unshift()` and `shift()` array operations. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/03_stdlib/66_unshift')
-rw-r--r--tests/custom/03_stdlib/66_unshift52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/custom/03_stdlib/66_unshift b/tests/custom/03_stdlib/66_unshift
new file mode 100644
index 0000000..9771661
--- /dev/null
+++ b/tests/custom/03_stdlib/66_unshift
@@ -0,0 +1,52 @@
+The `unshift()` function places the given argument(s) at the begin of the
+given array while maintaining their order.
+
+Returns the last added value.
+
+Returns `null` if the given destination argment is not an array.
+
+Throws a type exception if the given array is immuatable.
+
+-- Testcase --
+{%
+ let arr = [];
+
+ printf("%.J\n", [
+ // add one element
+ unshift(arr, 123),
+
+ // add multiple elements
+ unshift(arr, 1, 2, 3),
+
+ // add null values
+ unshift(arr, null, null, 4),
+
+ // no-op
+ unshift(arr),
+
+ // invalid destination
+ unshift({}, 1, 2, 3)
+ ]);
+
+ printf("%.J\n", arr);
+%}
+-- End --
+
+-- Expect stdout --
+[
+ 123,
+ 3,
+ 4,
+ null,
+ null
+]
+[
+ null,
+ null,
+ 4,
+ 1,
+ 2,
+ 3,
+ 123
+]
+-- End --