-
Notifications
You must be signed in to change notification settings - Fork 4
/
traverse_test.go
70 lines (64 loc) · 1.68 KB
/
traverse_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
package flexbuffers
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func mustLookup(r Raw, path ...string) Reference {
res, err := r.Lookup(path...)
if err != nil {
panic(err)
}
return res
}
func TestTraverser_Lookup(t *testing.T) {
a := assert.New(t)
b := NewBuilder()
b.Map(func(b *Builder) {
b.MapField([]byte("a"), func(b *Builder) {
b.IntField([]byte("a"), 10)
b.IntField([]byte("b"), 20)
})
b.MapField([]byte("b"), func(b *Builder) {
b.IntField([]byte("c"), 30)
b.IntField([]byte("d"), 40)
b.StringValueField([]byte("e"), "foo")
})
b.IntField([]byte("c"), 123)
b.MapField([]byte("d"), func(b *Builder) {
b.MapField([]byte("a"), func(b *Builder) {
b.StringValueField([]byte("b"), "bar")
})
})
})
if err := b.Finish(); err != nil {
t.Fatal(err)
}
root := b.Buffer()
a.Equal(int64(10), mustLookup(root, "a", "a").AsInt64())
a.Equal(int64(40), mustLookup(root, "b", "d").AsInt64())
a.Equal("foo", mustLookup(root, "b", "e").AsStringRef().StringValueOrEmpty())
a.Equal(int64(123), mustLookup(root, "c").AsInt64())
a.Equal("bar", mustLookup(root, "d", "a", "b").AsStringRef().StringValueOrEmpty())
// empty data
_, err := root.Lookup("foo")
a.Equal(ErrNotFound, err)
}
func TestTraverser_LookupLargeData(t *testing.T) {
a := assert.New(t)
b := NewBuilder()
b.Map(func(b *Builder) {
for i := 0; i < 100; i++ {
b.MapField([]byte(fmt.Sprintf("a-%d", i)), func(b *Builder) {
for j := 0; j < 100; j++ {
b.IntField([]byte(fmt.Sprintf("b-%d", j)), int64(j)*10)
}
})
}
})
if err := b.Finish(); err != nil {
t.Fatal(err)
}
root := b.Buffer()
a.Equal(int64(900), mustLookup(root, "a-50", "b-90").AsInt64())
}