-
Notifications
You must be signed in to change notification settings - Fork 34
/
regx_test.go
66 lines (62 loc) · 1.73 KB
/
regx_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
package main
import (
"fmt"
"reflect"
"testing"
"github.com/rjkroege/edwood/sam"
)
func TestRegexpForward(t *testing.T) {
tt := []struct {
text string
re string
expected []RangeSet
nmax int // Max number of matches
}{
{"aaa", "b", nil, 10},
{"aaa", "a", []RangeSet{{{0, 1}}, {{1, 2}}, {{2, 3}}}, 10},
{"cba", "ba", []RangeSet{{{1, 3}}}, 10},
{"aaaaa", "a", []RangeSet{{{0, 1}}, {{1, 2}}}, 2},
}
for i, tc := range tt {
t.Run(fmt.Sprintf("test-%02d", i), func(t *testing.T) {
re, err := rxcompile(tc.re)
if err != nil {
t.Fatalf("failed to compile regular expression %q", tc.re)
}
text := sam.NewTextBuffer(0, 0, []rune(tc.text))
rs := re.rxexecute(text, nil, 0, text.Nc(), tc.nmax)
if !reflect.DeepEqual(rs, tc.expected) {
t.Errorf("regexp %q incorrectly matches %q:\nexpected: %v\ngot: %v",
tc.re, tc.text, tc.expected, rs)
}
})
}
}
func TestRegexpBackward(t *testing.T) {
tt := []struct {
text string
re string
expected RangeSet
nmax int // Max number of matches
}{
{"baa", "ba", RangeSet{{0, 2}}, 10},
{"aaa", "a", RangeSet{{2, 3}, {1, 2}, {0, 1}}, 10},
{"cba", "a", RangeSet{{2, 3}}, 10},
{"aba", "a", RangeSet{{2, 3}, {0, 1}}, 10},
{"aaaa", "a", RangeSet{{3, 4}, {2, 3}}, 2},
}
for i, tc := range tt {
t.Run(fmt.Sprintf("test-%02d", i), func(t *testing.T) {
re, err := rxcompile(tc.re)
if err != nil {
t.Fatalf("failed to compile regular expression %q", tc.re)
}
text := sam.NewTextBuffer(0, 0, []rune(tc.text))
rs := re.rxbexecute(text, text.Nc(), tc.nmax)
if !reflect.DeepEqual(rs, tc.expected) {
t.Errorf("regexp %q incorrectly matches %q:\nexpected: %v\ngot: %v",
tc.re, tc.text, tc.expected, rs)
}
})
}
}