-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
66 lines (60 loc) · 1.37 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
package oakmux
import "testing"
func TestTreeCreation(t *testing.T) {
cases := []struct {
Route string
Matching []string
Failing []string
}{
{
Route: "/1/2",
Matching: []string{"/1/2"},
Failing: []string{"/1/22"},
},
}
// "/1/2/3/4/5/6",
// "/good/routes",
// "/a/b/c/",
tree := &Node{}
for _, testCase := range cases {
r, err := NewRoute("test", testCase.Route)
if err != nil {
t.Fatal("cannot make route", testCase.Route, err)
}
if err = tree.Grow(r, r.segments); err != nil {
t.Fatal("cannot grow tree node:", testCase.Route, err)
}
}
}
func TestNodeWalk(t *testing.T) {
handler := newTestHandler(t)
router, err := New(
WithLimitlessRequestBytes(), // for type assertion
WithRouteHandler(
"firstRoute", "/test/[pattern]/yep/1/2/3/4",
handler,
),
WithRouteHandler(
"secondRoute", "/test/[wild]/[pattern1]/last/",
handler,
),
WithRouteHandler(
"thirdRoute", "/test/[pattern]/1/2/3/4",
handler,
),
)
if err != nil {
t.Fatal(err)
}
nodeCount := 0
if err = router.(*mux).tree.Walk(func(n *Node) (ok bool, err error) {
nodeCount++
return true, nil
}); err != nil {
t.Fatal("failed to walk the Node tree:", err)
}
const expectedToVisit = 14
if nodeCount != expectedToVisit {
t.Fatalf("walk function was unable to visit every tree node: visited %d out of %d", nodeCount, expectedToVisit)
}
}