-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode_test.go
118 lines (104 loc) · 2.55 KB
/
node_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package abnf_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/ghettovoice/abnf"
)
var _ = Describe("ABNF", func() {
Describe("Node", func() {
var n *abnf.Node
BeforeEach(func() {
n = &abnf.Node{
Key: "ab",
Value: []byte("abcc"),
Children: abnf.Nodes{
{Key: "a", Value: []byte("a")},
{Key: "b", Value: []byte("b")},
{Key: "c", Value: []byte("c")},
{Key: "c", Value: []byte("c")},
},
}
})
It("should search starting from self", func() {
Expect(n.GetNode("ab")).Should(Equal(n))
Expect(n.GetNode("a")).Should(Equal(&abnf.Node{Key: "a", Value: []byte("a")}))
Expect(n.GetNode("d")).Should(BeZero())
})
})
Describe("Nodes", func() {
var ns abnf.Nodes
BeforeEach(func() {
ns = abnf.Nodes{
{
Key: "abc",
Value: []byte("abc"),
Children: abnf.Nodes{
{Key: "a", Value: []byte("a")},
{Key: "b", Value: []byte("b")},
{Key: "c", Value: []byte("c")},
},
},
{
Key: "abcd",
Value: []byte("abcd"),
Children: abnf.Nodes{
{Key: "a", Value: []byte("a")},
{Key: "b", Value: []byte("b")},
{
Key: "cd",
Value: []byte("cd"),
Children: abnf.Nodes{
{Key: "c", Value: []byte("c")},
{Key: "d", Value: []byte("d")},
},
},
},
},
}
})
It("should search one", func() {
Expect(ns.Get("d")).Should(Equal(&abnf.Node{Key: "d", Value: []byte("d")}))
Expect(ns.Get("h")).Should(BeZero())
})
It("should search all", func() {
Expect(ns.GetAll("c")).Should(Equal(abnf.Nodes{
{Key: "c", Value: []byte("c")},
{Key: "c", Value: []byte("c")},
}))
})
It("should search best", func() {
Expect(ns.Best()).Should(Equal(&abnf.Node{
Key: "abcd",
Value: []byte("abcd"),
Children: abnf.Nodes{
{Key: "a", Value: []byte("a")},
{Key: "b", Value: []byte("b")},
{
Key: "cd",
Value: []byte("cd"),
Children: abnf.Nodes{
{Key: "c", Value: []byte("c")},
{Key: "d", Value: []byte("d")},
},
},
},
}))
Expect(ns[:1].Best()).Should(Equal(&abnf.Node{
Key: "abc",
Value: []byte("abc"),
Children: abnf.Nodes{
{Key: "a", Value: []byte("a")},
{Key: "b", Value: []byte("b")},
{Key: "c", Value: []byte("c")},
},
}))
Expect(ns[:0].Best()).Should(BeZero())
})
It("should be comparable by best node", func() {
ns1 := ns[:1]
ns2 := ns[1:]
Expect(ns1.Compare(ns2)).Should(Equal(-1))
Expect(ns2.Compare(ns1)).Should(Equal(1))
})
})
})