forked from dlclark/regexp2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtl_test.go
52 lines (44 loc) · 1.26 KB
/
rtl_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
package regexp2
import "testing"
func TestRightToLeft_Basic(t *testing.T) {
re := MustCompile(`foo\d+`, RightToLeft)
s := "0123456789foo4567890foo1foo 0987"
m, err := re.FindStringMatch(s)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "foo1", m.String(); want != got {
t.Fatalf("Match 0 failed, wanted %v, got %v", want, got)
}
m, err = re.FindNextMatch(m)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "foo4567890", m.String(); want != got {
t.Fatalf("Match 1 failed, wanted %v, got %v", want, got)
}
}
func TestRightToLeft_StartAt(t *testing.T) {
re := MustCompile(`\d`, RightToLeft)
m, err := re.FindStringMatchStartingAt("0123", -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if m == nil {
t.Fatalf("Expected match")
}
if want, got := "3", m.String(); want != got {
t.Fatalf("Find failed, wanted '%v', got '%v'", want, got)
}
}
func TestRightToLeft_Replace(t *testing.T) {
re := MustCompile(`\d`, RightToLeft)
s := "0123456789foo4567890foo "
str, err := re.Replace(s, "#", -1, 7)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "0123456789foo#######foo ", str; want != got {
t.Fatalf("Replace failed, wanted '%v', got '%v'", want, got)
}
}