-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathregexp_validity_test.go
108 lines (101 loc) · 2.7 KB
/
regexp_validity_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
package quamina
import (
"fmt"
"testing"
)
func oneRegexp(t *testing.T, re string, valid bool) {
t.Helper()
_, err := readRegexp(re)
if valid && err != nil {
t.Errorf("should be valid: /%s/, but <%s>", re, err.Error())
}
if (!valid) && err == nil {
t.Errorf("should NOT be valid: /%s/", re)
}
//fmt.Println("ERR: " + err.Error())
}
func TestDebugRegexp(t *testing.T) {
oneRegexp(t, "[~]", false)
}
func TestEmptyRegexp(t *testing.T) {
parse := newRxParseState([]byte{})
parse, err := readRegexpWithParse(parse)
if err != nil {
fmt.Println("OOPS: " + err.Error())
}
table, _ := makeRegexpNFA(parse.tree, false)
// raw empty string should NOT match
var transitions []*fieldMatcher
bufs := &bufpair{}
fields := traverseNFA(table, []byte(""), transitions, bufs)
if len(fields) != 0 {
t.Error("Matched empty string")
}
// matching on a field SHOULD match
pattern := `{"a": [{"regexp": ""}]}`
cm := newCoreMatcher()
err = cm.addPattern("a", pattern)
if err != nil {
t.Error("addPattern: " + err.Error())
}
event := `{"a": ""}`
mm, err := cm.matchesForJSONEvent([]byte(event))
if err != nil {
t.Error("M4J: " + err.Error())
}
if len(mm) == 0 {
t.Error("Didn't match empty to empty")
}
}
func TestRegexpValidity(t *testing.T) {
problems := 0
tests := 0
implemented := 0
for _, sample := range regexpSamples {
tests++
parse := newRxParseState([]byte(sample.regex))
parse, err := readRegexpWithParse(parse)
if sample.valid {
if len(parse.features.foundUnimplemented()) == 0 {
implemented++
table, dest := makeRegexpNFA(parse.tree, false)
for _, should := range sample.matches {
// the sample regexp tests think the empty string matches lots of regexps with which
// I don't think it should
if should == "" {
continue
}
var transitions []*fieldMatcher
bufs := &bufpair{}
fields := traverseNFA(table, []byte(should), transitions, bufs)
if !containsFM(t, fields, dest) {
t.Errorf("<%s> failed to match /%s/", should, sample.regex)
problems++
}
}
for _, shouldNot := range sample.nomatches {
var transitions []*fieldMatcher
bufs := &bufpair{}
fields := traverseNFA(table, []byte(shouldNot), transitions, bufs)
if len(fields) != 0 {
t.Errorf("<%s> matched /%s/", shouldNot, sample.regex)
problems++
}
}
}
if err != nil {
t.Errorf("should be valid: /%s/, but <%s> (after %d lines) ", sample.regex, err.Error(), tests)
problems++
}
} else {
if err == nil {
t.Errorf("should NOT be valid: /%s/ (after %d lines) ", sample.regex, tests)
problems++
}
}
if problems == 10 {
return
}
}
fmt.Printf("tests: %d, implemented: %d\n", tests, implemented)
}