-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_test.go
72 lines (52 loc) · 1.57 KB
/
query_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
package jq
import (
"testing"
"nikand.dev/go/cbor"
)
func TestQuery(tb *testing.T) {
b := NewBuffer()
root := b.appendVal(obj{"a", 1, "b", obj{"c", arr{2, "3", obj{"d", 5}, true}}})
testOne(tb, NewQuery("a"), b, root, 1)
if tb.Failed() {
tb.Logf("buffer root %x\n%s", root, b.Dump())
return
}
testOne(tb, NewQuery("b", "c"), b, root, arr{2, "3", obj{"d", 5}, true})
}
func TestQueryIter1(tb *testing.T) {
b := NewBuffer()
root := b.appendVal(obj{"a", 1, "b", obj{"c", arr{2, "3", obj{"d", 5}, true}}})
testIter(tb, NewQuery("b", "c", Iter{}), b, root, []any{2, "3", obj{"d", 5}, true})
}
func TestQueryIter2(tb *testing.T) {
b := NewBuffer()
root := b.appendVal(arr{
obj{"a", 1, "b", lab{lab: 4, val: 2}, "c", "d"},
true,
})
// log.Printf("data %x\n%s", root, Dump(d))
testIter(tb, NewQuery(-2, Iter{}), b, root, []any{1, lab{lab: 4, val: 2}, "d"})
}
func TestQueryMultiIter(tb *testing.T) {
b := NewBuffer()
root := b.appendVal(arr{
obj{"q", obj{"a", 1, "b", 2}},
obj{"q", arr{}, "w", -5},
obj{"q", arr{3, 4}},
})
// log.Printf("data %x\n%s", root, Dump(d))
f := NewQuery(Iter{}, "q", Iter{})
testIter(tb, f, b, root, []any{1, 2, 3, 4})
}
func TestQueryIgnoreTypeError(tb *testing.T) {
b := NewBuffer()
root := b.appendVal(obj{"a", "b"})
testOne(tb, NewQuery("a"), b, root, "b")
testOne(tb, NewQuery("q"), b, root, nil)
root2 := b.appendVal(arr{"a", "b"})
testIter(tb, NewQuery(KeyNoError("a")), b, root2, []any{})
testError(tb, NewQuery("a"), b, root2, NewTypeError(cbor.Array, cbor.Map))
if tb.Failed() {
tb.Logf("buffer\n%s", b.Dump())
}
}