summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/18_split
diff options
context:
space:
mode:
Diffstat (limited to 'tests/custom/03_stdlib/18_split')
-rw-r--r--tests/custom/03_stdlib/18_split87
1 files changed, 87 insertions, 0 deletions
diff --git a/tests/custom/03_stdlib/18_split b/tests/custom/03_stdlib/18_split
new file mode 100644
index 0000000..e31ce78
--- /dev/null
+++ b/tests/custom/03_stdlib/18_split
@@ -0,0 +1,87 @@
+The `split()` function breaks the given string into multiple substrings,
+using the given separator value.
+
+The separator may be either a string or a regular expression value.
+
+Returns an array containing the resulting parts.
+
+Returns `null` if the given input value is not a string or if the separator
+argument is neither a string nor a regular expression.
+
+
+-- Testcase --
+{%
+ print(join("\n", [
+ // split by string
+ split("foo|bar|baz", "|"),
+
+ // split by regexp
+ split("apples, bananas and strawberries are fruits", /, | and | are | /),
+
+ // splitting an empty string yields an array containing one empty string
+ split("", "|"),
+ split("", ""),
+ split("", /\s+/),
+
+ // splitting with an empty string as separator yields an array containing
+ // all characters individually
+ split("foo|bar|baz", ""),
+ split("foo|bar|baz", /()/),
+
+ // splitting on a separator not found within the string will yield an
+ // array containing the entire string as sole element
+ split("foo|bar|baz", "xxx"),
+ split("foo|bar|baz", /\d+/),
+
+ // subsequent separators are not coalesced
+ split("abc|||def", "|"),
+ split("foo1bar23baz", /[[:digit:]]/),
+
+ // leading and trailing empty substrings are retained
+ split("|abc|def|", "|"),
+ split(",foo;bar:", /[,;:]/),
+ ]), "\n");
+%}
+-- End --
+
+-- Expect stdout --
+[ "foo", "bar", "baz" ]
+[ "apples", "bananas", "strawberries", "fruits" ]
+[ "" ]
+[ "" ]
+[ "" ]
+[ "f", "o", "o", "|", "b", "a", "r", "|", "b", "a", "z" ]
+[ "f", "o", "o", "|", "b", "a", "r", "|", "b", "a", "z" ]
+[ "foo|bar|baz" ]
+[ "foo|bar|baz" ]
+[ "abc", "", "", "def" ]
+[ "foo", "bar", "", "baz" ]
+[ "", "abc", "def", "" ]
+[ "", "foo", "bar", "" ]
+-- End --
+
+
+Supplying an invalid input string value will yield `null`.
+
+-- Testcase --
+{%
+ printf("%.J\n", split(true, "u"));
+%}
+-- End --
+
+-- Expect stdout --
+null
+-- End --
+
+
+Supplying a non-string, non-regexp separator will yield `null`.
+
+-- Testcase --
+{%
+ printf("%.J\n", split("null true false", true));
+%}
+-- End --
+
+-- Expect stdout --
+null
+-- End --