5 "github.com/pelletier/go-toml"
13 type queryTestNode struct {
15 position toml.Position
18 func valueString(root interface{}) string {
19 result := "" //fmt.Sprintf("%T:", root)
20 switch node := root.(type) {
23 for i, v := range node.Values() {
24 items = append(items, fmt.Sprintf("%s:%s",
25 node.Positions()[i].String(), valueString(v)))
28 result = "[" + strings.Join(items, ", ") + "]"
30 result = fmt.Sprintf("%s:%s",
31 node.position.String(), valueString(node.value))
34 for _, v := range node {
35 items = append(items, valueString(v))
38 result = "[" + strings.Join(items, ", ") + "]"
40 // workaround for unreliable map key ordering
42 for _, k := range node.Keys() {
43 v := node.GetPath([]string{k})
44 items = append(items, k+":"+valueString(v))
47 result = "{" + strings.Join(items, ", ") + "}"
48 case map[string]interface{}:
49 // workaround for unreliable map key ordering
51 for k, v := range node {
52 items = append(items, k+":"+valueString(v))
55 result = "{" + strings.Join(items, ", ") + "}"
57 result += fmt.Sprintf("%d", node)
59 result += "'" + node + "'"
61 result += fmt.Sprintf("%f", node)
63 result += fmt.Sprintf("%t", node)
65 result += fmt.Sprintf("'%v'", node)
70 func assertValue(t *testing.T, result, ref interface{}) {
71 pathStr := valueString(result)
72 refStr := valueString(ref)
73 if pathStr != refStr {
74 t.Errorf("values do not match")
75 t.Log("test:", pathStr)
76 t.Log("ref: ", refStr)
80 func assertQueryPositions(t *testing.T, tomlDoc string, query string, ref []interface{}) {
81 tree, err := toml.Load(tomlDoc)
83 t.Errorf("Non-nil toml parse error: %v", err)
86 q, err := Compile(query)
91 results := q.Execute(tree)
92 assertValue(t, results, ref)
95 func TestQueryRoot(t *testing.T) {
96 assertQueryPositions(t,
101 map[string]interface{}{
103 }, toml.Position{1, 1},
108 func TestQueryKey(t *testing.T) {
109 assertQueryPositions(t,
114 int64(42), toml.Position{2, 1},
119 func TestQueryKeyString(t *testing.T) {
120 assertQueryPositions(t,
125 int64(42), toml.Position{2, 1},
130 func TestQueryIndex(t *testing.T) {
131 assertQueryPositions(t,
132 "[foo]\na = [1,2,3,4,5,6,7,8,9,0]",
136 int64(6), toml.Position{2, 1},
141 func TestQuerySliceRange(t *testing.T) {
142 assertQueryPositions(t,
143 "[foo]\na = [1,2,3,4,5,6,7,8,9,0]",
147 int64(1), toml.Position{2, 1},
150 int64(2), toml.Position{2, 1},
153 int64(3), toml.Position{2, 1},
156 int64(4), toml.Position{2, 1},
159 int64(5), toml.Position{2, 1},
164 func TestQuerySliceStep(t *testing.T) {
165 assertQueryPositions(t,
166 "[foo]\na = [1,2,3,4,5,6,7,8,9,0]",
170 int64(1), toml.Position{2, 1},
173 int64(3), toml.Position{2, 1},
176 int64(5), toml.Position{2, 1},
181 func TestQueryAny(t *testing.T) {
182 assertQueryPositions(t,
183 "[foo.bar]\na=1\nb=2\n[foo.baz]\na=3\nb=4",
187 map[string]interface{}{
190 }, toml.Position{1, 1},
193 map[string]interface{}{
196 }, toml.Position{4, 1},
200 func TestQueryUnionSimple(t *testing.T) {
201 assertQueryPositions(t,
202 "[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6",
206 map[string]interface{}{
209 }, toml.Position{1, 1},
212 map[string]interface{}{
215 }, toml.Position{4, 1},
218 map[string]interface{}{
221 }, toml.Position{7, 1},
226 func TestQueryRecursionAll(t *testing.T) {
227 assertQueryPositions(t,
228 "[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6",
232 map[string]interface{}{
233 "foo": map[string]interface{}{
234 "bar": map[string]interface{}{
239 "baz": map[string]interface{}{
240 "foo": map[string]interface{}{
245 "gorf": map[string]interface{}{
246 "foo": map[string]interface{}{
251 }, toml.Position{1, 1},
254 map[string]interface{}{
255 "bar": map[string]interface{}{
259 }, toml.Position{1, 1},
262 map[string]interface{}{
265 }, toml.Position{1, 1},
268 int64(1), toml.Position{2, 1},
271 int64(2), toml.Position{3, 1},
274 map[string]interface{}{
275 "foo": map[string]interface{}{
279 }, toml.Position{4, 1},
282 map[string]interface{}{
285 }, toml.Position{4, 1},
288 int64(3), toml.Position{5, 1},
291 int64(4), toml.Position{6, 1},
294 map[string]interface{}{
295 "foo": map[string]interface{}{
299 }, toml.Position{7, 1},
302 map[string]interface{}{
305 }, toml.Position{7, 1},
308 int64(5), toml.Position{8, 1},
311 int64(6), toml.Position{9, 1},
316 func TestQueryRecursionUnionSimple(t *testing.T) {
317 assertQueryPositions(t,
318 "[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6",
322 map[string]interface{}{
323 "bar": map[string]interface{}{
327 }, toml.Position{1, 1},
330 map[string]interface{}{
333 }, toml.Position{4, 1},
336 map[string]interface{}{
339 }, toml.Position{1, 1},
342 map[string]interface{}{
345 }, toml.Position{7, 1},
350 func TestQueryFilterFn(t *testing.T) {
351 buff, err := ioutil.ReadFile("../example.toml")
357 assertQueryPositions(t, string(buff),
361 int64(8001), toml.Position{13, 1},
364 int64(8001), toml.Position{13, 1},
367 int64(8002), toml.Position{13, 1},
370 int64(5000), toml.Position{14, 1},
374 assertQueryPositions(t, string(buff),
378 "TOML Example", toml.Position{3, 1},
381 "Tom Preston-Werner", toml.Position{6, 1},
384 "GitHub", toml.Position{7, 1},
387 "GitHub Cofounder & CEO\nLikes tater tots and beer.",
391 "192.168.1.1", toml.Position{12, 1},
394 "10.0.0.1", toml.Position{21, 3},
397 "eqdc10", toml.Position{22, 3},
400 "10.0.0.2", toml.Position{25, 3},
403 "eqdc10", toml.Position{26, 3},
407 assertQueryPositions(t, string(buff),
410 // no float values in document
413 tv, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
414 assertQueryPositions(t, string(buff),
418 map[string]interface{}{
419 "name": "Tom Preston-Werner",
420 "organization": "GitHub",
421 "bio": "GitHub Cofounder & CEO\nLikes tater tots and beer.",
423 }, toml.Position{5, 1},
426 map[string]interface{}{
427 "server": "192.168.1.1",
428 "ports": []interface{}{int64(8001), int64(8001), int64(8002)},
429 "connection_max": int64(5000),
431 }, toml.Position{11, 1},
434 map[string]interface{}{
435 "alpha": map[string]interface{}{
439 "beta": map[string]interface{}{
443 }, toml.Position{17, 1},
446 map[string]interface{}{
449 }, toml.Position{20, 3},
452 map[string]interface{}{
455 }, toml.Position{24, 3},
458 map[string]interface{}{
459 "data": []interface{}{
460 []interface{}{"gamma", "delta"},
461 []interface{}{int64(1), int64(2)},
463 }, toml.Position{28, 1},
467 assertQueryPositions(t, string(buff),
471 tv, toml.Position{9, 1},
475 assertQueryPositions(t, string(buff),
479 true, toml.Position{15, 1},