-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_test.go
144 lines (130 loc) · 2.18 KB
/
op_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
package uni_filter
import (
"fmt"
"testing"
)
type Person struct {
Name string
Age int
}
type Location struct {
Jd float64
Wd float64
Country string
}
type Car struct {
Price float64
Name string
Location Location
}
func (p Person) GetCars() *[]*Car {
var cars []*Car
cars = append(cars, &Car{
Price: 1000,
Name: "lanbojini",
Location: Location{
Jd: 100,
Wd: 100,
Country: "china",
},
})
cars = append(cars, &Car{
Price: 1500,
Name: "lanboniuni",
Location: Location{
Jd: 100,
Wd: 100,
Country: "usa",
},
})
cars = append(cars, &Car{
Price: 1800,
Name: "mashaladi",
Location: Location{
Jd: 102,
Wd: 103,
Country: "hk",
},
})
return &cars
}
func TestAll(t *testing.T) {
//expr, err := Parse("!country=china or (!age__gte=18 and score__lte=9.0) or lover.name=jany or www__ex")
//expr, err := Parse("pets[].habbits[]__length=39")
expr, err := Parse("GetCars--call.Location.Country=china")
//expr, err := Parse("lover.name = jany")
if err != nil {
t.Fatal(err)
}
p := Person{}
matched := expr.Match(p)
if matched {
fmt.Println("match this person")
}
var cases = []struct {
v map[string]any
matched bool
}{
{
map[string]any{
"name": "myth",
"country": "what",
"age": 10,
"score": 9.5,
"lover": map[string]string{
"name": "jany",
},
"pets": []map[string]any{
{
"name": "maizi",
"habbits": []map[string]int{
{
"price": 30,
},
},
},
},
},
true,
},
{
map[string]any{
"name": "jany",
"country": "china",
"age": 18,
"score": 9.1,
},
true,
},
{
map[string]any{
"name": "lili",
"country": "usa",
"age": 13,
"score": 9.23,
"www": "",
"pets": []map[string]any{
{
"name": "chengzi",
},
},
},
true,
},
{
map[string]any{
"name": "marry",
"country": "usa",
"age": 18,
"score": 9.3,
},
true,
},
}
for i, c := range cases {
matched := expr.Match(c.v)
if matched != c.matched {
t.Errorf("test case at index %d failed\n", i)
}
}
}