-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
selector_test.go
156 lines (132 loc) · 2.83 KB
/
selector_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
148
149
150
151
152
153
154
155
156
package dasel
import (
"errors"
"reflect"
"testing"
)
func collectAll(r SelectorResolver) ([]Selector, error) {
res := make([]Selector, 0)
for {
s, err := r.Next()
if err != nil {
return res, err
}
if s == nil {
break
}
res = append(res, *s)
}
return res, nil
}
func TestStandardSelectorResolver_Next(t *testing.T) {
r := NewSelectorResolver("index(1).property(user).name.property(first,last?)", nil)
got, err := collectAll(r)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
exp := []Selector{
{
funcName: "index",
funcArgs: []string{"1"},
},
{
funcName: "property",
funcArgs: []string{"user"},
},
{
funcName: "property",
funcArgs: []string{"name"},
},
{
funcName: "property",
funcArgs: []string{"first", "last?"},
},
}
if !reflect.DeepEqual(exp, got) {
t.Errorf("exp: %v, got: %v", exp, got)
}
}
func TestStandardSelectorResolver_Next_Nested(t *testing.T) {
r := NewSelectorResolver("nested(a().b(),c(),d()).nested(a().b(),c(),d())", nil)
got, err := collectAll(r)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
exp := []Selector{
{
funcName: "nested",
funcArgs: []string{"a().b()", "c()", "d()"},
},
{
funcName: "nested",
funcArgs: []string{"a().b()", "c()", "d()"},
},
}
if !reflect.DeepEqual(exp, got) {
t.Errorf("exp: %v, got: %v", exp, got)
}
}
func TestStandardSelectorResolver_Next_ExtraClosingBracket(t *testing.T) {
r := NewSelectorResolver("all().filter(not(equal(x,true))))", nil)
expErr := &ErrBadSelectorSyntax{
Part: "filter(not(equal(x,true))))",
Message: "too many closing brackets",
}
_, err := collectAll(r)
if !errors.Is(err, expErr) {
t.Errorf("expected error: %v, got %v", expErr, err)
return
}
}
func TestStandardSelectorResolver_Next_EscapedDot(t *testing.T) {
r := NewSelectorResolver("plugins.io\\.containerd\\.grpc\\.v1\\.cri.registry", nil)
got, err := collectAll(r)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
exp := []Selector{
{
funcName: "property",
funcArgs: []string{"plugins"},
},
{
funcName: "property",
funcArgs: []string{"io.containerd.grpc.v1.cri"},
},
{
funcName: "property",
funcArgs: []string{"registry"},
},
}
if !reflect.DeepEqual(exp, got) {
t.Errorf("exp: %v, got: %v", exp, got)
}
}
func TestStandardSelectorResolver_Next_EscapedEverything(t *testing.T) {
r := NewSelectorResolver("a.b\\(\\.asdw\\\\\\].c(\\))", nil)
got, err := collectAll(r)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
exp := []Selector{
{
funcName: "property",
funcArgs: []string{"a"},
},
{
funcName: "property",
funcArgs: []string{"b(.asdw\\]"},
},
{
funcName: "c",
funcArgs: []string{")"},
},
}
if !reflect.DeepEqual(exp, got) {
t.Errorf("exp: %v, got: %v", exp, got)
}
}