-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfilter_test.go
More file actions
158 lines (146 loc) · 4.71 KB
/
filter_test.go
File metadata and controls
158 lines (146 loc) · 4.71 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
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
148
149
150
151
152
153
154
155
156
157
158
package filter
import (
"fmt"
"testing"
)
func ExampleParseFilter_and() {
fmt.Println(ParseFilter([]byte("title pr and userType eq \"Employee\"")))
// Output:
// title pr and userType eq "Employee" <nil>
}
func ExampleParseFilter_attrExp() {
fmt.Println(ParseFilter([]byte("schemas eq \"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User\"")))
// Output:
// schemas eq "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" <nil>
}
func ExampleParseFilter_caseInsensitivity() {
fmt.Println(ParseFilter([]byte("NAME PR AND NOT (FIRST EQ \"test\") AND ANOTHER NE \"test\"")))
// Output:
// NAME pr and not(FIRST eq "test") and ANOTHER ne "test" <nil>
}
func ExampleParseFilter_not() {
fmt.Println(ParseFilter([]byte("not (emails co \"example.com\" or emails.value co \"example.org\")")))
// Output:
// not(emails co "example.com" or emails.value co "example.org") <nil>
}
func ExampleParseFilter_or() {
fmt.Println(ParseFilter([]byte("title pr or userType eq \"Intern\"")))
// Output:
// title pr or userType eq "Intern" <nil>
}
func ExampleParseFilter_parentheses() {
fmt.Println(ParseFilter([]byte("(emails.type eq \"work\")")))
// Output:
// emails.type eq "work" <nil>
}
func ExampleParseFilter_valuePath() {
fmt.Println(ParseFilter([]byte("emails[type eq \"work\" and value co \"@example.com\"]")))
// Output:
// emails[type eq "work" and value co "@example.com"] <nil>
}
func Example_walk() {
expression, _ := ParseFilter([]byte("emails[type eq \"work\" and value co \"@example.com\"] or ims[type eq \"xmpp\" and value co \"@foo.com\"]"))
var walk func(e Expression) error
walk = func(e Expression) error {
switch v := e.(type) {
case *LogicalExpression:
_ = walk(v.Left)
_ = walk(v.Right)
case *ValuePath:
_ = walk(v.ValueFilter)
case *AttributeExpression:
fmt.Printf("%s %s %q\n", v.AttributePath, v.Operator, v.CompareValue)
default:
// etc...
}
return nil
}
_ = walk(expression)
// Output:
// type eq "work"
// value co "@example.com"
// type eq "xmpp"
// value co "@foo.com"
}
func TestParseFilter(t *testing.T) {
for _, example := range []string{
"userName eq \"bjensen\"",
"userName Eq \"bjensen\"",
"name.familyName co \"O'Malley\"",
"userName sw \"J\"",
"urn:ietf:params:scim:schemas:core:2.0:User:userName sw \"J\"",
"title pr",
"meta.lastModified gt \"2011-05-13T04:42:34Z\"",
"meta.lastModified ge \"2011-05-13T04:42:34Z\"",
"meta.lastModified lt \"2011-05-13T04:42:34Z\"",
"meta.lastModified le \"2011-05-13T04:42:34Z\"",
"title pr and userType eq \"Employee\"",
"title pr or userType eq \"Intern\"",
"schemas eq \"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User\"",
"userType eq \"Employee\" and (emails co \"example.com\" or emails.value co \"example.org\")",
"userType ne \"Employee\" and not (emails co \"example.com\" or emails.value co \"example.org\")",
"userType eq \"Employee\" and (emails.type eq \"work\")",
"userType eq \"Employee\" and emails[type eq \"work\" and value co \"@example.com\"]",
"emails[type eq \"work\" and value co \"@example.com\"] or ims[type eq \"xmpp\" and value co \"@foo.com\"]",
"name pr and userName pr and title pr",
"name pr and not (first eq \"test\") and another ne \"test\"",
"NAME PR AND NOT (FIRST EQ \"test\") AND ANOTHER NE \"test\"",
"name pr or userName pr or title pr",
} {
t.Run(example, func(t *testing.T) {
if _, err := ParseFilter([]byte(example)); err != nil {
t.Error(err)
}
})
}
}
func TestParseFilter_precedence(t *testing.T) {
for _, tc := range []struct {
input string
want string
}{
{
input: "(a eq \"1\" or b eq \"2\") and c eq \"3\"",
want: "(a eq \"1\" or b eq \"2\") and c eq \"3\"",
},
{
input: "c eq \"3\" and (a eq \"1\" or b eq \"2\")",
want: "c eq \"3\" and (a eq \"1\" or b eq \"2\")",
},
{
input: "(a eq \"1\" or b eq \"2\") and (c eq \"3\" or d eq \"4\")",
want: "(a eq \"1\" or b eq \"2\") and (c eq \"3\" or d eq \"4\")",
},
{
input: "a eq \"1\" and (b eq \"2\" or c eq \"3\") and d eq \"4\"",
want: "a eq \"1\" and (b eq \"2\" or c eq \"3\") and d eq \"4\"",
},
{
input: "a eq \"1\" or b eq \"2\" and c eq \"3\"",
want: "a eq \"1\" or b eq \"2\" and c eq \"3\"",
},
{
input: "(a eq \"1\" and b eq \"2\") or c eq \"3\"",
want: "a eq \"1\" and b eq \"2\" or c eq \"3\"",
},
{
input: "(a eq \"1\")",
want: "a eq \"1\"",
},
{
input: "a eq \"1\" or (b eq \"2\" and c eq \"3\")",
want: "a eq \"1\" or b eq \"2\" and c eq \"3\"",
},
} {
t.Run(tc.input, func(t *testing.T) {
exp, err := ParseFilter([]byte(tc.input))
if err != nil {
t.Fatal(err)
}
got := fmt.Sprintf("%v", exp)
if got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
})
}
}