Repository navigation
Expand file tree
/
Copy pathsplit_test.go
More file actions
109 lines (100 loc) · 3.16 KB
/
Copy pathsplit_test.go
File metadata and controls
109 lines (100 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package regexp2
import (
"slices"
"strings"
"testing"
)
func TestBasicSplit(t *testing.T) {
re := MustCompile("a(.)c(.)e")
vals, err := re.Split("123abcde456aBCDe789", -1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if want, got := []string{"123", "b", "d", "456aBCDe789"}, vals; !slices.Equal(want, got) {
t.Errorf("wanted %v got %v", want, got)
}
}
func TestBasicSplit_IgnoreCase(t *testing.T) {
re := MustCompile("a(.)c(.)e", IgnoreCase)
vals, err := re.Split("123abcde456aBCDe789", -1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if want, got := []string{"123", "b", "d", "456", "B", "D", "789"}, vals; !slices.Equal(want, got) {
t.Errorf("wanted %v got %v", want, got)
}
}
func TestSplitRightToLeftRegression(t *testing.T) {
for _, tt := range []struct {
pattern, input string
count int
want []string
}{
{`-`, "a-b-c", -1, []string{"a", "b", "c"}},
{`-`, "a-b-c-d", 2, []string{"a-b", "c", "d"}},
{`-`, "a-b-c", 1, []string{"a-b-c"}},
{`-`, "abc", -1, []string{"abc"}},
{`-`, "é-猫-日", -1, []string{"é", "猫", "日"}},
{`(-)(:)`, "a-:b-:c", -1, []string{"a", ":", "-", "b", ":", "-", "c"}},
{`(?=.)`, "abc", -1, []string{"", "a", "b", "c"}},
} {
t.Run(tt.pattern+tt.input, func(t *testing.T) {
got, err := MustCompile(tt.pattern, RightToLeft).Split(tt.input, tt.count)
if err != nil || !slices.Equal(got, tt.want) {
t.Fatalf("got %q, %v; want %q", got, err, tt.want)
}
})
}
}
func TestSplit_ZeroWidth(t *testing.T) {
re := MustCompile(`(?<=\G..)(?=..)`)
vals, err := re.Split("aabbccdd", -1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if want, got := []string{"aa", "bb", "cc", "dd"}, vals; !slices.Equal(want, got) {
t.Errorf("wanted %v got %v", want, got)
}
}
func TestSplit_LimitCountRemainder(t *testing.T) {
re := MustCompile("a(.)c(.)e", IgnoreCase)
vals, err := re.Split("123abcde456aBCDe789abcde", 2)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if want, got := []string{"123", "b", "d", "456", "B", "D", "789abcde"}, vals; !slices.Equal(want, got) {
t.Errorf("wanted %v got %v", want, got)
}
}
func TestSplit_LimitCount1(t *testing.T) {
re := MustCompile("a(.)c(.)e", IgnoreCase)
vals, err := re.Split("123abcde456aBCDe789abcde", 1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if want, got := []string{"123abcde456aBCDe789abcde"}, vals; !slices.Equal(want, got) {
t.Errorf("wanted %v got %v", want, got)
}
}
func TestSplit_UnicodePrefixCaptures(t *testing.T) {
re := MustCompile(`(needle)`)
prefix := strings.Repeat("pré", 8)
input := prefix + "needle" + "tail"
vals, err := re.Split(input, -1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if want, got := []string{prefix, "needle", "tail"}, vals; !slices.Equal(want, got) {
t.Errorf("wanted %v got %v", want, got)
}
}
func TestSplit_LimitCount0(t *testing.T) {
re := MustCompile("a(.)c(.)e", IgnoreCase)
vals, err := re.Split("123abcde456aBCDe789abcde", 0)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if want, got := []string{}, vals; !slices.Equal(want, got) {
t.Errorf("wanted %v got %v", want, got)
}
}