-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregex_test.go
131 lines (111 loc) · 3.81 KB
/
regex_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
122
123
124
125
126
127
128
129
130
131
package regex
import (
"bytes"
"errors"
"math/rand"
"strconv"
"testing"
"time"
)
func TestCompile(t *testing.T) {
reC := Comp("this is test %1", "a")
if reC.RE.ReplaceAllString(`this is test a`, `this is test b`, 0) != `this is test b` {
t.Error(`[this is test %1] [a]`, "\n", errors.New("failed to compile params"))
}
re := `test .*`
reEscaped := Escape(re)
if re == reEscaped || Comp(reEscaped).Match([]byte(`test 1`)) {
t.Error("[", reEscaped, "]\n", errors.New("escape function failed"))
}
r := Comp(`test %1`, "%2", "a")
if r.Match([]byte(`test a`)) {
t.Error(`[test %1] [%2, a]`, "\n", errors.New("escape function failed to escape '%' char"))
}
}
func TestReplaceStr(t *testing.T) {
var check = func(s string, re, r string, e string) {
res := Comp(re).RepStrLit([]byte(s), []byte(r))
if !bytes.Equal(res, []byte(e)) {
t.Error("[", string(res), "]\n", errors.New("result does not match expected result"))
}
}
check("this is a test", `(?#a\s+)test`, "", "this is a ")
check("string with `block` quotes", `\'.*?\'`, "'single'", "string with 'single' quotes")
}
func TestReplaceStrComplex(t *testing.T) {
var check = func(s string, re, r string, e string) {
res := Comp(re).RepStr([]byte(s), []byte(r))
if !bytes.Equal(res, []byte(e)) {
t.Error("[", string(res), "]\n", errors.New("result does not match expected result"))
}
}
check("this is a Test", `(?i)a (test)`, "some $1", "this is some Test")
check("I Need Coffee!!!", `Coffee(!*)`, "More Coffee$1", "I Need More Coffee!!!")
}
func TestReplaceFunc(t *testing.T) {
var check = func(s string, re, r string, e string) {
res := Comp(re).RepFunc([]byte(s), func(data func(int) []byte) []byte {
return JoinBytes(data(1), ' ', r)
})
if !bytes.Equal(res, []byte(e)) {
t.Error("[", string(res), "]\n", errors.New("result does not match expected result"))
}
}
check("this is a new test", `(new) test`, "pizza", "this is a new pizza")
check("a random string", `(a) random`, "not so random", "a not so random string")
}
func TestConcurrent(t *testing.T) {
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
go (func() {
res := Comp(`(t)`).RepFunc([]byte("test"), func(data func(int) []byte) []byte {
return data(1)
})
_ = res
time.Sleep(10 * time.Nanosecond)
})()
}
// time.Sleep(1000000 * 1000) // 1 second
time.Sleep(1000000 * 100) // 0.1 second
}
}
func TestCache(t *testing.T) {
var check = func(s string, re, r string, e string) {
res := Comp(re).RepStrLit([]byte(s), []byte(r))
if !bytes.Equal(res, []byte(e)) {
t.Error("[", string(res), "]\n", errors.New("result does not match expected result"))
}
}
check("this is a test", `\sis\s`, " was ", "this was a test")
check("this is a test", `\sis\s`, " was ", "this was a test")
}
func TestFlags(t *testing.T) {
var check = func(s string, re, r string, e string) {
res := Comp(re).RepStrLit([]byte(s), []byte(r))
if !bytes.Equal(res, []byte(e)) {
t.Error("[", string(res), "]\n", errors.New("result does not match expected result"))
}
}
check("this is a\nmultiline text", `(?s)a\s*multiline`, "", "this is text")
check("list line 1\nlist line 2\n list line 3", `(?m)^list`, "a list", "a list line 1\na list line 2\n list line 3")
check("a MultiCase text", `(?i)multicase`, "", "a text")
check("a MultiCase text no flag", `multicase`, "", "a MultiCase text no flag")
// check("a multi\nline text", `multi\s*line`, "", "a multi\nline text")
}
func TestPerformance(t *testing.T) {
for i := 0; i < 10000; i++ {
Comp(strconv.Itoa(rand.Int()))
}
}
func TestValid(t *testing.T) {
var check = func(re string, e bool) {
res := IsValid(re)
if res != e {
t.Error("[", string(re), "]\n", errors.New("result does not match expected result"))
}
}
check(`[\w_\-]+`, true)
check(`[\w_-.]+`, false)
check(`(?<test>)`, true)
check(`(?i)test`, true)
}