-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmonocase_test.go
113 lines (107 loc) · 3.27 KB
/
monocase_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
package quamina
import (
"fmt"
"testing"
)
func TestABCDMono(t *testing.T) {
permuteAndTest(t, "abcd", "ABCD")
}
func TestHungarianMono(t *testing.T) {
orig := []rune{0x10C80, 0x10C9D, 0x10C95, 0x10C8B}
alts := []rune{0x10CC0, 0x10CDD, 0x10CD5, 0x10CCB}
permuteAndTest(t, string(orig), string(alts))
}
func TestIntermittentMono(t *testing.T) {
permuteAndTest(t, "a,8899bc d", "A,8899BC D")
}
func permuteAndTest(t *testing.T, origS, altsS string) {
t.Helper()
orig := []byte(origS)
alts := []byte(altsS)
t.Helper()
permutations := permuteCase(t, orig, alts, nil, 0, nil)
pp := newPrettyPrinter(98987)
fa, fm := makeMonocaseFA(orig, pp)
for _, p := range permutations {
ff := traverseDFA(fa, p, nil)
if len(ff) != 1 || ff[0] != fm {
t.Error("FfFfAIL")
}
}
fmt.Printf("%s/%s: %s\n", origS, altsS, pp.printNFA(fa))
}
func permuteCase(t *testing.T, orig []byte, alts []byte, sofar []byte, index int, permutations [][]byte) [][]byte {
t.Helper()
if index == len(orig) {
next := make([]byte, len(sofar))
copy(next, sofar)
permutations = append(permutations, next)
} else {
permutations = permuteCase(t, orig, alts, append(sofar, orig[index]), index+1, permutations)
permutations = permuteCase(t, orig, alts, append(sofar, alts[index]), index+1, permutations)
}
return permutations
}
func TestSingletonMonocaseMerge(t *testing.T) {
cm := newCoreMatcher()
var err error
err = cm.addPattern("singleton", `{"x": ["singleton"] }`)
if err != nil {
t.Error("add singleton: " + err.Error())
}
err = cm.addPattern("mono", `{"x": [ {"equals-ignore-case": "foo"}]}`)
if err != nil {
t.Error("add mono")
}
matches, _ := cm.matchesForJSONEvent([]byte(`{"x": "singleton"}`))
if len(matches) != 1 && !containsX(matches, "singleton") {
t.Error("singleton match failed")
}
matches, _ = cm.matchesForJSONEvent([]byte(`{"x": "FoO"}`))
if len(matches) != 1 && !containsX(matches, "mono") {
t.Error("singleton match failed")
}
}
func TestEqualsIgnoreCaseMatching(t *testing.T) {
rule1 := "{ \"a\" : [ { \"equals-ignore-case\": \"aBc\" } ] }"
rule2 := "{ \"b\" : [ { \"equals-ignore-case\": \"XyZ\" } ] }"
rule3 := "{ \"b\" : [ { \"equals-ignore-case\": \"xyZ\" } ] }"
var err error
cm := newCoreMatcher()
err = cm.addPattern("r1", rule1)
if err != nil {
t.Error("AddPattern: " + err.Error())
}
err = cm.addPattern("r2", rule2)
if err != nil {
t.Error("AddPattern: " + err.Error())
}
err = cm.addPattern("r3", rule3)
if err != nil {
t.Error("AddPattern: " + err.Error())
}
matches, _ := cm.matchesForJSONEvent([]byte("{\"a\" : \"abc\"}"))
if len(matches) != 1 || matches[0] != "r1" {
t.Error("wrong on rule1")
}
matches, _ = cm.matchesForJSONEvent([]byte("{\"b\" : \"XYZ\"}"))
if len(matches) != 2 || !containsX(matches, "r2", "r3") {
t.Error("wrong on XYZ")
}
matches, _ = cm.matchesForJSONEvent([]byte("{\"a\" : \"AbC\"}"))
if len(matches) != 1 || !containsX(matches, "r1") {
t.Error("wrong on AbC")
}
matches, _ = cm.matchesForJSONEvent([]byte("{\"b\" : \"xyzz\"}"))
if len(matches) != 0 {
t.Error("wrong on xyzz")
}
matches, _ = cm.matchesForJSONEvent([]byte("{\"b\" : \"aabc\"}"))
if len(matches) != 0 {
t.Error("wrong on aabc")
}
matches, _ = cm.matchesForJSONEvent([]byte("{\"b\" : \"ABCXYZ\"}"))
if len(matches) != 0 {
t.Error("wrong on ABCXYZ")
}
}