forked from dlclark/regexp2
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp_re2_test.go
121 lines (113 loc) · 2.8 KB
/
regexp_re2_test.go
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
110
111
112
113
114
115
116
117
118
119
120
121
package regexp2
import "testing"
func TestRE2CompatCapture(t *testing.T) {
r := MustCompile(`re(?P<a>2)`, RE2)
if m, err := r.FindStringMatch("blahre2blah"); m == nil {
t.Fatal("Expected match")
} else if err != nil {
t.Fatalf("Unexpected error: %v", err)
} else {
g := m.GroupByName("a")
if want, got := "2", g.String(); want != got {
t.Fatalf("Wanted %v got %v", want, got)
}
}
}
func TestRE2CompatCapture_Invalid(t *testing.T) {
bogus := []string{
`(?P<name>a`,
`(?P<name>`,
`(?P<name`,
`(?P<x y>a)`,
`(?P<>a)`,
}
for _, inp := range bogus {
t.Run(inp, func(t *testing.T) {
r, err := Compile(inp, RE2)
if err == nil {
t.Fatal("Expected failure to parse")
}
if r != nil {
t.Fatal("expected regexp to be nil")
}
})
}
}
func TestRE2NamedAscii(t *testing.T) {
table := []struct {
nm string
pos string
neg string
}{
{nm: "alnum", pos: "1", neg: "!"},
{nm: "alpha", pos: "g", neg: "0"},
{nm: "blank", pos: " ", neg: "_"},
{nm: "ascii", pos: "*", neg: "\x8f"},
{nm: "cntrl", pos: "\t", neg: "A"},
{nm: "graph", pos: "!", neg: " "},
{nm: "lower", pos: "a", neg: "A"},
{nm: "print", pos: " ", neg: "\r"},
{nm: "punct", pos: "@", neg: "A"},
{nm: "space", pos: " ", neg: "A"},
{nm: "digit", pos: "1", neg: "A"},
{nm: "upper", pos: "A", neg: "a"},
{nm: "word", pos: "_", neg: "-"},
{nm: "xdigit", pos: "A", neg: "G"},
}
for _, row := range table {
t.Run(row.nm, func(t *testing.T) {
r := MustCompile(`[[:`+row.nm+`:]]`, RE2)
if m, _ := r.MatchString(row.pos); !m {
t.Fatal("Expected match")
}
if m, _ := r.MatchString(row.neg); m {
t.Fatal("Expected no match")
}
})
t.Run(row.nm+" negate", func(t *testing.T) {
r := MustCompile(`[[:^`+row.nm+`:]]`, RE2)
if m, _ := r.MatchString(row.neg); !m {
t.Fatal("Expected match")
}
if m, _ := r.MatchString(row.pos); m {
t.Fatal("Expected no match")
}
})
}
}
func TestRE2NamedAscii_Concat(t *testing.T) {
r := MustCompile(`[[:digit:]a]`, RE2)
if m, _ := r.MatchString("b"); m {
t.Fatal("Expected no match")
}
if m, _ := r.MatchString("a"); !m {
t.Fatal("Expected match")
}
if m, _ := r.MatchString("["); m {
t.Fatal("Expected no match")
}
if m, _ := r.MatchString("5"); !m {
t.Fatal("Expected match")
}
}
func TestRE2Dollar_Singleline(t *testing.T) {
// PCRE allows for \n after the $ and RE2 doesn't
r := MustCompile(`^ac$\n`, RE2)
if m, _ := r.MatchString("ac"); m {
t.Fatal("Expected no match")
}
if m, _ := r.MatchString("ac\n"); m {
t.Fatal("Expected no match")
}
}
func TestRE2Dollar_Multiline(t *testing.T) {
r := MustCompile(`^ac$\n`, RE2|Multiline)
if m, _ := r.MatchString("ac"); m {
t.Fatal("Expected no match")
}
if m, err := r.MatchString("ac\n"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}