-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommands_test.go
117 lines (97 loc) · 2.87 KB
/
commands_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
// Copyright 2013 Masterminds
package cookoo
import (
"bytes"
"regexp"
"testing"
)
func TestLogMessage(t *testing.T) {
registry, router, context := Cookoo()
logger := new(bytes.Buffer)
context.AddLogger("test", logger)
registry.Route("test", "Testing.").
Does(LogMessage, "logmsg").
Using("msg").WithDefault("a test").
Using("level").WithDefault("error")
e := router.HandleRequest("test", context, false)
if e != nil {
t.Error("! Unexpected error.")
}
line := logger.String()
line = line[0 : len(line)-1]
pattern := "a test$"
matched, err := regexp.MatchString(pattern, line)
if err != nil {
t.Fatal("! Regex Pattern did not compile:", err)
}
if !matched {
t.Errorf("! Message was not logged to first test logger: %q", line)
}
}
func TestAddToContext(t *testing.T) {
registry, router, context := Cookoo()
registry.Route("test", "Testing.").
Does(AddToContext, "addtocontext").
Using("foo").WithDefault("baz").
Using("bar").WithDefault("qux")
e := router.HandleRequest("test", context, false)
if e != nil {
t.Error("! Unexpected error.")
}
if context.Get("foo", "") != "baz" {
t.Error("! foo was not added to the context with a value of baz.")
}
if context.Get("bar", "") != "qux" {
t.Error("! bar was not added to the context with a value of qux.")
}
}
func TestForwardTo(t *testing.T) {
registry, router, context := Cookoo()
registry.
// Routes to test a basic working forward.
Route("test", "Testing.").
Does(ForwardTo, "forwardto").
Using("route").WithDefault("test2").
Route("test2", "A second test route").
Does(AddToContext, "addtocontext").
Using("bar").WithDefault("qux").
// Route to test forward when no route supplied.
Route("nope", "A test route to fatal error").
Does(ForwardTo, "forwardto").
Does(AddToContext, "addtocontext").
Using("foo").WithDefault("baz").
// Route to test with an ignored route.
Route("test3", "Testing.").
Does(ForwardTo, "forwardto").
Using("route").WithDefault("nope").
Using("ignoreRoutes").WithDefault([]string{"nope"}).
Does(AddToContext, "addtocontext").
Using("bar").WithDefault("qux")
// Testing the case the required route is not supplied.
e := router.HandleRequest("nope", context, false)
if e == nil {
t.Error("! Expected error executing nope")
}
v := context.Get("foo", nil)
if v != nil {
t.Error("! Expected AddToContext to not be executed.")
}
// Test a successful jump.
e = router.HandleRequest("test", context, false)
if e != nil {
t.Error("! Unexpected error.")
}
v = context.Get("bar", nil)
if v != "qux" {
t.Error("! Expected test route to forward to test to adding bar to the context.")
}
// Test an ignored route
e = router.HandleRequest("test3", context, false)
if e != nil {
t.Error("! Unexpected error.")
}
v = context.Get("bar", nil)
if v != "qux" {
t.Error("! Expected test3 route to forward to test to adding bar to the context.")
}
}