-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsteamregexindex_test.go
More file actions
63 lines (60 loc) · 1.34 KB
/
steamregexindex_test.go
File metadata and controls
63 lines (60 loc) · 1.34 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
package streamregex
import (
"context"
"regexp"
"strings"
"testing"
)
func TestFindReaderIndex(t *testing.T) {
tests := []struct {
input string
match string
regex *regexp.Regexp
maxMatchLen int
numMatchs int
locations [][]int
}{
{
`0123456789this is a stream of data with lots of trailing information`,
`stream of`,
regexp.MustCompile(`stream\s+of`),
40,
1,
[][]int{{20, 32}},
},
{
`test test test test test test test test test`,
`test`,
regexp.MustCompile(`test`),
4,
9,
[][]int{{0, 4}, {5, 9}, {10, 14}, {15, 19}, {20, 24}, {25, 29}, {30, 34}, {35, 39}, {40, 44}},
},
{
`cut off match`,
`f ma`,
regexp.MustCompile(`f ma`),
4,
1,
[][]int{{6, 10}},
},
}
for _, test := range tests {
// Find matches
matchedData, locationsData := FindReaderIndex(context.Background(), test.regex, test.maxMatchLen, strings.NewReader(test.input))
matches := 0
for match := range matchedData {
location := <-locationsData
matches++
if match != test.match {
t.Errorf("Invalid match: %s", match)
}
if location[0] != test.locations[matches-1][0] && location[1] != test.locations[matches-1][1] {
t.Errorf("Invalid match location: %d", location)
}
}
if matches != test.numMatchs {
t.Errorf("Did not find correct number of matches")
}
}
}