-
Notifications
You must be signed in to change notification settings - Fork 2
/
router_test.go
92 lines (78 loc) · 1.87 KB
/
router_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
package urlrouter
import (
"reflect"
"testing"
)
type testRouter struct {
ur URLRouter
}
func (r *testRouter) New() URLRouter {
return r.ur
}
type testURLRouter struct {
name string
}
func (r *testURLRouter) Lookup(path string) (data interface{}, params []Param) {
return nil, nil
}
func (r *testURLRouter) Build(records []Record) error {
return nil
}
func Test_Register(t *testing.T) {
defer func() {
routers = make(map[string]Router)
}()
var actual interface{} = len(routers)
var expected interface{} = 0
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
router := &testRouter{}
Register("testrouter", router)
actual = routers
expected = map[string]Router{"testrouter": router}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
}
func Test_NewURLRouter(t *testing.T) {
defer func() {
routers = make(map[string]Router)
}()
router1, router2 := &testURLRouter{name: "1"}, &testURLRouter{name: "2"}
routers["router1"] = &testRouter{router1}
routers["router2"] = &testRouter{router2}
func() {
defer func() {
if err := recover(); err == nil {
t.Errorf("Expect error, but nil")
}
}()
NewURLRouter("")
}()
func() {
defer func() {
if err := recover(); err == nil {
t.Errorf("Expect error, but nil")
}
}()
NewURLRouter("missing")
}()
actual := NewURLRouter("router1")
expected := router1
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
actual = NewURLRouter("router2")
expected = router2
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
}
func Test_NewRecord(t *testing.T) {
actual := NewRecord("testkey", 100)
expected := Record{Key: "testkey", Value: 100}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
}