OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / pelletier / go-toml / query / match_test.go
1 package query
2
3 import (
4         "fmt"
5         "github.com/pelletier/go-toml"
6         "testing"
7 )
8
9 // dump path tree to a string
10 func pathString(root pathFn) string {
11         result := fmt.Sprintf("%T:", root)
12         switch fn := root.(type) {
13         case *terminatingFn:
14                 result += "{}"
15         case *matchKeyFn:
16                 result += fmt.Sprintf("{%s}", fn.Name)
17                 result += pathString(fn.next)
18         case *matchIndexFn:
19                 result += fmt.Sprintf("{%d}", fn.Idx)
20                 result += pathString(fn.next)
21         case *matchSliceFn:
22                 result += fmt.Sprintf("{%d:%d:%d}",
23                         fn.Start, fn.End, fn.Step)
24                 result += pathString(fn.next)
25         case *matchAnyFn:
26                 result += "{}"
27                 result += pathString(fn.next)
28         case *matchUnionFn:
29                 result += "{["
30                 for _, v := range fn.Union {
31                         result += pathString(v) + ", "
32                 }
33                 result += "]}"
34         case *matchRecursiveFn:
35                 result += "{}"
36                 result += pathString(fn.next)
37         case *matchFilterFn:
38                 result += fmt.Sprintf("{%s}", fn.Name)
39                 result += pathString(fn.next)
40         }
41         return result
42 }
43
44 func assertPathMatch(t *testing.T, path, ref *Query) bool {
45         pathStr := pathString(path.root)
46         refStr := pathString(ref.root)
47         if pathStr != refStr {
48                 t.Errorf("paths do not match")
49                 t.Log("test:", pathStr)
50                 t.Log("ref: ", refStr)
51                 return false
52         }
53         return true
54 }
55
56 func assertPath(t *testing.T, query string, ref *Query) {
57         path, _ := parseQuery(lexQuery(query))
58         assertPathMatch(t, path, ref)
59 }
60
61 func buildPath(parts ...pathFn) *Query {
62         query := newQuery()
63         for _, v := range parts {
64                 query.appendPath(v)
65         }
66         return query
67 }
68
69 func TestPathRoot(t *testing.T) {
70         assertPath(t,
71                 "$",
72                 buildPath(
73                 // empty
74                 ))
75 }
76
77 func TestPathKey(t *testing.T) {
78         assertPath(t,
79                 "$.foo",
80                 buildPath(
81                         newMatchKeyFn("foo"),
82                 ))
83 }
84
85 func TestPathBracketKey(t *testing.T) {
86         assertPath(t,
87                 "$[foo]",
88                 buildPath(
89                         newMatchKeyFn("foo"),
90                 ))
91 }
92
93 func TestPathBracketStringKey(t *testing.T) {
94         assertPath(t,
95                 "$['foo']",
96                 buildPath(
97                         newMatchKeyFn("foo"),
98                 ))
99 }
100
101 func TestPathIndex(t *testing.T) {
102         assertPath(t,
103                 "$[123]",
104                 buildPath(
105                         newMatchIndexFn(123),
106                 ))
107 }
108
109 func TestPathSliceStart(t *testing.T) {
110         assertPath(t,
111                 "$[123:]",
112                 buildPath(
113                         newMatchSliceFn(123, maxInt, 1),
114                 ))
115 }
116
117 func TestPathSliceStartEnd(t *testing.T) {
118         assertPath(t,
119                 "$[123:456]",
120                 buildPath(
121                         newMatchSliceFn(123, 456, 1),
122                 ))
123 }
124
125 func TestPathSliceStartEndColon(t *testing.T) {
126         assertPath(t,
127                 "$[123:456:]",
128                 buildPath(
129                         newMatchSliceFn(123, 456, 1),
130                 ))
131 }
132
133 func TestPathSliceStartStep(t *testing.T) {
134         assertPath(t,
135                 "$[123::7]",
136                 buildPath(
137                         newMatchSliceFn(123, maxInt, 7),
138                 ))
139 }
140
141 func TestPathSliceEndStep(t *testing.T) {
142         assertPath(t,
143                 "$[:456:7]",
144                 buildPath(
145                         newMatchSliceFn(0, 456, 7),
146                 ))
147 }
148
149 func TestPathSliceStep(t *testing.T) {
150         assertPath(t,
151                 "$[::7]",
152                 buildPath(
153                         newMatchSliceFn(0, maxInt, 7),
154                 ))
155 }
156
157 func TestPathSliceAll(t *testing.T) {
158         assertPath(t,
159                 "$[123:456:7]",
160                 buildPath(
161                         newMatchSliceFn(123, 456, 7),
162                 ))
163 }
164
165 func TestPathAny(t *testing.T) {
166         assertPath(t,
167                 "$.*",
168                 buildPath(
169                         newMatchAnyFn(),
170                 ))
171 }
172
173 func TestPathUnion(t *testing.T) {
174         assertPath(t,
175                 "$[foo, bar, baz]",
176                 buildPath(
177                         &matchUnionFn{[]pathFn{
178                                 newMatchKeyFn("foo"),
179                                 newMatchKeyFn("bar"),
180                                 newMatchKeyFn("baz"),
181                         }},
182                 ))
183 }
184
185 func TestPathRecurse(t *testing.T) {
186         assertPath(t,
187                 "$..*",
188                 buildPath(
189                         newMatchRecursiveFn(),
190                 ))
191 }
192
193 func TestPathFilterExpr(t *testing.T) {
194         assertPath(t,
195                 "$[?('foo'),?(bar)]",
196                 buildPath(
197                         &matchUnionFn{[]pathFn{
198                                 newMatchFilterFn("foo", toml.Position{}),
199                                 newMatchFilterFn("bar", toml.Position{}),
200                         }},
201                 ))
202 }