-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeypath_test.go
76 lines (69 loc) · 1.63 KB
/
keypath_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
73
74
75
76
package art
import (
"bytes"
"testing"
)
func Test_KeyPathAssign(t *testing.T) {
p := keyPath{}
k := []byte{4, 5, 7, 8, 1, 10}
p.assign(k)
if !bytes.Equal(k, p.asSlice()) {
t.Errorf("Assigned path %v, but got different path %v back", k, p)
}
}
func Test_KeyPathTrim(t *testing.T) {
p := keyPath{}
k := []byte{1, 2, 3, 4, 5}
p.assign(k)
p.trimPathStart(2)
exp := []byte{3, 4, 5}
if !bytes.Equal(exp, p.asSlice()) {
t.Errorf("Trimmed path expected to be %v but was %b", exp, p)
}
}
func Test_KeyPathCanExtendBy(t *testing.T) {
p := keyPath{}
k := []byte{1, 2, 3}
p.assign(k)
if !p.canExtendBy(20) {
t.Errorf("a 3 byte path should be extendable by another 20")
}
if p.canExtendBy(21) {
t.Errorf("a 3 byte path shouldn't be extendable by another 21")
}
}
func Test_KeyPathString(t *testing.T) {
p := keyPath{}
k := []byte{1, 0x42, 0, 0xFF}
p.assign(k)
exp := " 0x01 0x42 0x00 0xFF"
if exp != p.String() {
t.Errorf("Expecting '%s' for String() but got '%s'", exp, p.String())
}
}
type prependCase struct {
base string
prefix string
extra string
expected string
}
func Test_KeyPathPrepend(t *testing.T) {
cases := []prependCase{
{"qqq", "ppp", "x", "pppxqqq"},
{"qqq", "ppp", "", "pppqqq"},
{"qqq", "", "", "qqq"},
{"", "", "", ""},
{"", "", "x", "x"},
{"", "xx", "y", "xxy"},
}
for _, c := range cases {
t.Run(c.expected, func(t *testing.T) {
p := keyPath{}
p.assign([]byte(c.base))
p.prependPath([]byte(c.prefix), []byte(c.extra)...)
if !bytes.Equal([]byte(c.expected), p.asSlice()) {
t.Errorf("Expecting final path to be %s but was %s", c.expected, string(p.asSlice()))
}
})
}
}