1import unittest, strutils
2include nre
3
4block: # string splitting
5  block: # splitting strings
6    check("1 2 3 4 5 6 ".split(re" ") == @["1", "2", "3", "4", "5", "6", ""])
7    check("1  2  ".split(re(" ")) == @["1", "", "2", "", ""])
8    check("1 2".split(re(" ")) == @["1", "2"])
9    check("foo".split(re("foo")) == @["", ""])
10    check("".split(re"foo") == @[""])
11    check("9".split(re"\son\s") == @["9"])
12
13  block: # captured patterns
14    check("12".split(re"(\d)") == @["", "1", "", "2", ""])
15
16  block: # maxsplit
17    check("123".split(re"", maxsplit = 2) == @["1", "23"])
18    check("123".split(re"", maxsplit = 1) == @["123"])
19    check("123".split(re"", maxsplit = -1) == @["1", "2", "3"])
20
21  block: # split with 0-length match
22    check("12345".split(re("")) == @["1", "2", "3", "4", "5"])
23    check("".split(re"") == newSeq[string]())
24    check("word word".split(re"\b") == @["word", " ", "word"])
25    check("word\r\lword".split(re"(*ANYCRLF)(?m)$") == @["word", "\r\lword"])
26    check("слово слово".split(re"(*U)(\b)") == @["", "слово", "", " ", "", "слово", ""])
27
28  block: # perl split tests
29    check("forty-two"                    .split(re"")      .join(",") == "f,o,r,t,y,-,t,w,o")
30    check("forty-two"                    .split(re"", 3)   .join(",") == "f,o,rty-two")
31    check("split this string"            .split(re" ")     .join(",") == "split,this,string")
32    check("split this string"            .split(re" ", 2)  .join(",") == "split,this string")
33    check("try$this$string"              .split(re"\$")    .join(",") == "try,this,string")
34    check("try$this$string"              .split(re"\$", 2) .join(",") == "try,this$string")
35    check("comma, separated, values"     .split(re", ")    .join("|") == "comma|separated|values")
36    check("comma, separated, values"     .split(re", ", 2) .join("|") == "comma|separated, values")
37    check("Perl6::Camelia::Test"         .split(re"::")    .join(",") == "Perl6,Camelia,Test")
38    check("Perl6::Camelia::Test"         .split(re"::", 2) .join(",") == "Perl6,Camelia::Test")
39    check("split,me,please"              .split(re",")     .join("|") == "split|me|please")
40    check("split,me,please"              .split(re",", 2)  .join("|") == "split|me,please")
41    check("Hello World    Goodbye   Mars".split(re"\s+")   .join(",") == "Hello,World,Goodbye,Mars")
42    check("Hello World    Goodbye   Mars".split(re"\s+", 3).join(",") == "Hello,World,Goodbye   Mars")
43    check("Hello test"                   .split(re"(\s+)") .join(",") == "Hello, ,test")
44    check("this will be split"           .split(re" ")     .join(",") == "this,will,be,split")
45    check("this will be split"           .split(re" ", 3)  .join(",") == "this,will,be split")
46    check("a.b"                          .split(re"\.")    .join(",") == "a,b")
47    check(""                             .split(re"")      .len       == 0)
48    check(":"                            .split(re"")      .len       == 1)
49
50  block: # start position
51    check("abc".split(re"", start = 1) == @["b", "c"])
52    check("abc".split(re"", start = 2) == @["c"])
53    check("abc".split(re"", start = 3) == newSeq[string]())
54