-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
58 lines (48 loc) · 1.96 KB
/
index_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
package jq
import "testing"
func TestIndex(tb *testing.T) {
b := NewBuffer()
root := b.appendVal(arr{"a", "b", "c", "d"})
testOne(tb, Index(0), b, root, "a")
testOne(tb, Index(1), b, root, "b")
testOne(tb, Index(3), b, root, "d")
testOne(tb, Index(-1), b, root, "d")
testOne(tb, Index(-3), b, root, "b")
testOne(tb, Index(-4), b, root, "a")
testOne(tb, Index(-100), b, root, Null)
testOne(tb, Index(100), b, root, Null)
}
func TestKey(tb *testing.T) {
b := NewBuffer()
root := b.appendVal(obj{"a", 1, "b", 2, "c", 3, "d", 4})
testOne(tb, Key("a"), b, root, 1)
testOne(tb, Key("b"), b, root, 2)
testOne(tb, Key("d"), b, root, 4)
testOne(tb, Key("e"), b, root, Null)
}
func TestIndexPath(tb *testing.T) {
b := NewBuffer()
root := b.appendVal(arr{"a", "b", "c", "d"})
testOnePath(tb, Index(0), b, root, "a", NodePath{ps(root, 0)})
testOnePath(tb, Index(1), b, root, "b", NodePath{ps(root, 1)})
testOnePath(tb, Index(3), b, root, "d", NodePath{ps(root, 3)})
testOnePath(tb, Index(-1), b, root, "d", NodePath{ps(root, 3)})
testOnePath(tb, Index(-3), b, root, "b", NodePath{ps(root, 1)})
testOnePath(tb, Index(-4), b, root, "a", NodePath{ps(root, 0)})
testOnePath(tb, Index(-100), b, root, Null, NodePath{ps(root, -100)})
testOnePath(tb, Index(100), b, root, Null, NodePath{ps(root, 100)})
}
func TestKeyPath(tb *testing.T) {
b := NewBuffer()
ra := b.appendVal("a")
rb := b.appendVal("b")
rd := b.appendVal("d")
root := b.appendVal(obj{"a", 1, "b", 2, "c", 3, "d", 4})
ekey := b.appendVal("e")
testOnePath(tb, Key("a"), b, root, 1, NodePath{psk(root, 0, ra)})
testOnePath(tb, Key("b"), b, root, 2, NodePath{psk(root, 1, rb)})
testOnePath(tb, Key("d"), b, root, 4, NodePath{psk(root, 3, rd)})
testOnePath(tb, Key("e"), b, root, Null, NodePath{psk(root, -1, ekey)})
}
func ps(off Off, i int) NodePathSeg { return NodePathSeg{Off: off, Index: i, Key: None} }
func psk(off Off, i int, key Off) NodePathSeg { return NodePathSeg{Off: off, Index: i, Key: key} }