-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypermatch_simple_test.go
146 lines (129 loc) · 3.18 KB
/
hypermatch_simple_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
package hypermatch
import (
"gotest.tools/v3/assert"
"log"
"slices"
"testing"
)
func containsAll(data []RuleIdentifier, entries ...RuleIdentifier) bool {
for _, e := range entries {
if !slices.Contains(data, e) {
log.Printf("Does not contain %d: %v", e, data)
return false
}
}
return true
}
func TestHyperMatchSimple(t *testing.T) {
h := NewHyperMatch()
assert.NilError(t, h.AddRule(1, []Condition{
{Path: "y", Pattern: Pattern{Type: PatternEquals, Value: "bb"}},
{Path: "x", Pattern: Pattern{Type: PatternEquals, Value: "aa"}},
}))
assert.NilError(t, h.AddRule(2, []Condition{
{Path: "y", Pattern: Pattern{Type: PatternEquals, Value: "bb"}},
}))
assert.NilError(t, h.AddRule(3, []Condition{
{Path: "x", Pattern: Pattern{Type: PatternEquals, Value: "dd"}},
{Path: "y", Pattern: Pattern{Type: PatternEquals, Value: "cc"}},
}))
assert.NilError(t, h.AddRule(4, []Condition{
{Path: "x", Pattern: Pattern{Type: PatternPrefix, Value: "a"}},
{Path: "y", Pattern: Pattern{Type: PatternPrefix, Value: "b"}},
}))
assert.NilError(t, h.AddRule(5, []Condition{
{Path: "x", Pattern: Pattern{Type: PatternSuffix, Value: "x"}},
{Path: "y", Pattern: Pattern{Type: PatternSuffix, Value: "y"}},
}))
assert.NilError(t, h.AddRule(6, []Condition{
{Path: "y", Pattern: Pattern{Type: PatternAnythingBut, Sub: []Pattern{
{Type: PatternEquals, Value: "bb"},
}}},
}))
assert.NilError(t, h.AddRule(7, []Condition{
{Path: "y", Pattern: Pattern{Type: PatternAnythingBut, Sub: []Pattern{
{Type: PatternEquals, Value: "b*y"},
}}},
}))
assert.Check(t, containsAll(h.Match([]Property{
{Path: "x", Values: []string{"aa"}},
{Path: "y", Values: []string{"bb"}},
}), 1, 2, 4))
assert.Check(t, containsAll(h.Match([]Property{
{Path: "x", Values: []string{"dd"}},
{Path: "y", Values: []string{"cc"}},
}), 3, 6))
assert.Check(t, containsAll(h.Match([]Property{
{Path: "x", Values: []string{"aax"}},
{Path: "y", Values: []string{"bby"}},
}), 4, 5, 6, 7))
assert.Check(t, containsAll(h.Match([]Property{
{Path: "x", Values: []string{"aax", "dd"}},
{Path: "y", Values: []string{"bby", "cc"}},
}), 3, 4, 5, 6, 7))
}
func TestValidateRule(t *testing.T) {
tests := []struct {
name string
set ConditionSet
wantErr bool
}{
{
name: "empty set",
set: []Condition{},
wantErr: false,
},
{
name: "valid set",
set: []Condition{
{
Path: "a",
Pattern: Pattern{
Type: PatternEquals,
Value: "2",
},
},
{
Path: "b",
Pattern: Pattern{
Type: PatternEquals,
Value: "1",
},
},
},
wantErr: false,
},
{
name: "invalid set",
set: []Condition{
{
Path: "a",
Pattern: Pattern{
Type: PatternEquals,
Sub: []Pattern{
{
Type: PatternEquals,
Value: "2",
},
},
},
},
{
Path: "b",
Pattern: Pattern{
Type: PatternEquals,
Value: "1",
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ValidateRule(tt.set); (err != nil) != tt.wantErr {
t.Errorf("ValidateRule() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}