forked from timbray/quamina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanything_but_test.go
147 lines (139 loc) · 3.78 KB
/
anything_but_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package quamina
import (
"strings"
"testing"
)
func TestAnythingButMerging(t *testing.T) {
pFoo := `{"z": [ "foo" ]}`
pAbFoot := `{"z": [ {"anything-but": [ "foot"] } ]}`
q, _ := New()
var err error
// can merge with DFA?
err = q.AddPattern("pFoo", pFoo)
if err != nil {
t.Error("add pFoo")
}
err = q.AddPattern("pAbFoot", pAbFoot)
if err != nil {
t.Error("add pAbFoot: " + err.Error())
}
var m []X
m, err = q.MatchesForEvent([]byte(`{"z": "foo"}`))
if err != nil {
t.Error("m4E - foo: " + err.Error())
}
if len(m) != 2 {
t.Errorf("len=%d?!?", len(m))
}
m, err = q.MatchesForEvent([]byte(`{"z": "foot"}`))
if err != nil {
t.Error("m4E - foo: " + err.Error())
}
if len(m) != 0 {
t.Errorf("len=%d?!?", len(m))
}
// can merge with NFA?
pFooStar := `{"z": [ {"shellstyle": "foo*" } ]}`
q, _ = New()
err = q.AddPattern("pFooStar", pFooStar)
if err != nil {
t.Error("pFooStar: " + err.Error())
}
err = q.AddPattern("pAbFoot", pAbFoot)
if err != nil {
t.Error("add pAbFoot: " + err.Error())
}
m, err = q.MatchesForEvent([]byte(`{"z": "foo"}`))
if err != nil {
t.Error("m4E: " + err.Error())
}
if len(m) != 2 {
t.Errorf("len=%d?!?", len(m))
}
m, err = q.MatchesForEvent([]byte(`{"z": "foot"}`))
if err != nil {
t.Error("m4E: " + err.Error())
}
if len(m) != 1 {
t.Errorf("len=%d?!?", len(m))
}
}
func TestAnythingButMatching(t *testing.T) {
q, _ := New()
// the idea is we're testing against all the 5-letter Wordle patterns, so we want a 4-letter prefix and
// suffix of an existing wordle, a 5-letter non-wordle, and a 6-letter where the wordle might match at the start
// and end. I tried to think of scenarios that would defeat the pretty-simple anything-but DFA but couldn't.
problemWords := []string{
`"bloo"`,
`"aper"`,
`"fnord"`,
`"doubts"`,
`"astern"`,
}
pws := strings.Join(problemWords, ",")
pattern := `{"a": [ {"anything-but": [ ` + pws + `] } ] }"`
err := q.AddPattern(pattern, pattern)
if err != nil {
t.Error("AP: " + err.Error())
}
words := readWWords(t)
template := `{"a": "XX"}`
problemTemplate := `{"a": XX}`
for _, word := range problemWords {
event := strings.ReplaceAll(problemTemplate, "XX", word)
matches, err := q.MatchesForEvent([]byte(event))
if err != nil {
t.Error("on problem word: " + err.Error())
}
if len(matches) != 0 {
t.Error("Matched on : " + word)
}
}
for _, word := range words {
ws := string(word)
event := strings.ReplaceAll(template, "XX", ws)
matches, err := q.MatchesForEvent([]byte(event))
if err != nil {
t.Error("m4E: " + err.Error())
}
if len(matches) != 1 {
t.Errorf("missed on (len=%d): "+event, len(matches))
}
}
}
func TestParseAnythingButPattern(t *testing.T) {
goods := []string{
`{"a": [ {"anything-but": [ "foo" ] } ] }`,
`{"a": [ {"anything-but": [ "bif", "x", "y", "a;sldkfjas;lkdfjs" ] } ] }`,
}
bads := []string{
`{"a": [ {"anything-but": x } ] }`,
`{"a": [ {"anything-but": 1 } ] }`,
`{"a": [ {"anything-but": [ "a"`,
`{"a": [ {"anything-but": [ x ] } ] }`,
`{"a": [ {"anything-but": [ {"z": 1} ] } ] }`,
`{"a": [ {"anything-but": [ true ] } ] }`,
`{"a": [ {"anything-but": [ "foo" ] x`,
`{"a": [ {"anything-but": [ "foo" ] ] ] }`,
`{"a": [ {"anything-but": {"x":1} } ] }`,
`{"a": [ {"anything-but": "foo" } ] }`,
`{"a": [ 2, {"anything-but": [ "foo" ] } ] }`,
`{"a": [ {"anything-but": [ "foo" ] }, 2 ] }`,
`{"a": [ {"anything-but": [ ] } ] }`,
}
for i, good := range goods {
fields, _, err := patternFromJSON([]byte(good))
if err != nil {
t.Errorf("parse anything-but i=%d: "+err.Error(), i)
}
if len(fields[0].vals) != 1 {
t.Errorf("wanted11 fields got %d", len(fields))
}
}
for _, bad := range bads {
_, _, err := patternFromJSON([]byte(bad))
if err == nil {
t.Errorf(`accepted anything-but "%s"`, bad)
}
}
}