aboutsummaryrefslogtreecommitdiffstats
path: root/test/testdata/eval-okay-regex-split.nix
diff options
context:
space:
mode:
Diffstat (limited to 'test/testdata/eval-okay-regex-split.nix')
-rw-r--r--test/testdata/eval-okay-regex-split.nix48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/testdata/eval-okay-regex-split.nix b/test/testdata/eval-okay-regex-split.nix
new file mode 100644
index 0000000..0073e05
--- /dev/null
+++ b/test/testdata/eval-okay-regex-split.nix
@@ -0,0 +1,48 @@
1with builtins;
2
3# Non capturing regex returns empty lists
4assert split "foobar" "foobar" == ["" [] ""];
5assert split "fo*" "f" == ["" [] ""];
6assert split "fo+" "f" == ["f"];
7assert split "fo*" "fo" == ["" [] ""];
8assert split "fo*" "foo" == ["" [] ""];
9assert split "fo+" "foo" == ["" [] ""];
10assert split "fo{1,2}" "foo" == ["" [] ""];
11assert split "fo{1,2}" "fooo" == ["" [] "o"];
12assert split "fo*" "foobar" == ["" [] "bar"];
13
14# Capturing regex returns a list of sub-matches
15assert split "(fo*)" "f" == ["" ["f"] ""];
16assert split "(fo+)" "f" == ["f"];
17assert split "(fo*)" "fo" == ["" ["fo"] ""];
18assert split "(f)(o*)" "f" == ["" ["f" ""] ""];
19assert split "(f)(o*)" "foo" == ["" ["f" "oo"] ""];
20assert split "(fo+)" "foo" == ["" ["foo"] ""];
21assert split "(fo{1,2})" "foo" == ["" ["foo"] ""];
22assert split "(fo{1,2})" "fooo" == ["" ["foo"] "o"];
23assert split "(fo*)" "foobar" == ["" ["foo"] "bar"];
24
25# Matches are greedy.
26assert split "(o+)" "oooofoooo" == ["" ["oooo"] "f" ["oooo"] ""];
27
28# Matches multiple times.
29assert split "(b)" "foobarbaz" == ["foo" ["b"] "ar" ["b"] "az"];
30
31# Split large strings containing newlines. null are inserted when a
32# pattern within the current did not match anything.
33assert split "[[:space:]]+|([',.!?])" ''
34 Nix Rocks!
35 That's why I use it.
36'' == [
37 "Nix" [ null ] "Rocks" ["!"] "" [ null ]
38 "That" ["'"] "s" [ null ] "why" [ null ] "I" [ null ] "use" [ null ] "it" ["."] "" [ null ]
39 ""
40];
41
42# Documentation examples
43assert split "(a)b" "abc" == [ "" [ "a" ] "c" ];
44assert split "([ac])" "abc" == [ "" [ "a" ] "b" [ "c" ] "" ];
45assert split "(a)|(c)" "abc" == [ "" [ "a" null ] "b" [ null "c" ] "" ];
46assert split "([[:upper:]]+)" " FOO " == [ " " [ "FOO" ] " " ];
47
48true