This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
/
route_test.go
114 lines (80 loc) · 3.21 KB
/
route_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
package tori
import "testing"
import "github.com/shiroyuki/tameshigiri"
func localTestWebRoutingRouteNewRoute(t *testing.T, givenPattern string, reversible bool) {
var assertion tameshigiri.Assertion
var newRoute *Route
assertion = tameshigiri.NewAssertion(t)
newRoute = NewRoute(givenPattern, reversible)
assertion.IsTrue(
newRoute.Pattern == givenPattern,
"The registered pattern must be exactly the same as the given one.",
)
assertion.IsTrue(
newRoute.Reversible == reversible,
"The registered pattern must be reversible.",
)
assertion.IsTrue(
newRoute.RePattern == nil,
"The compiled pattern should not be compiled during initialization.",
)
}
func TestWebRoutingRouteNewRouteNormal(t *testing.T) {
localTestWebRoutingRouteNewRoute(t, "/", true)
localTestWebRoutingRouteNewRoute(t, "/", false)
}
func TestWebRoutingReversibleRouteGetCompiledPatternOkay(t *testing.T) {
var assertion tameshigiri.Assertion
var givenPattern string
var newRoute *Route
assertion = tameshigiri.NewAssertion(t)
givenPattern = "/user/<alias>"
newRoute = NewRoute(givenPattern, true)
compiled, err := newRoute.GetCompiledPattern()
assertion.IsTrue(compiled != nil, "Unexpected compiled pattern")
assertion.IsTrue(err == nil, "No error raised")
}
func TestWebRoutingReversibleRouteGetCompiledPatternFailed(t *testing.T) {
var assertion tameshigiri.Assertion
var givenPattern string
var newRoute *Route
assertion = tameshigiri.NewAssertion(t)
givenPattern = "/user/<alias>/<alias>"
newRoute = NewRoute(givenPattern, true)
compiled, err := newRoute.GetCompiledPattern()
assertion.IsTrue(compiled == nil, "The pattern should not be compiled.")
assertion.IsTrue(err != nil, "An error should be raised.")
}
func TestWebRoutingNonReversibleRouteGetCompiledPattern(t *testing.T) {
var assertion tameshigiri.Assertion
var givenPattern string
var newRoute *Route
assertion = tameshigiri.NewAssertion(t)
givenPattern = "/user/(?P<alias>[^/]+)"
newRoute = NewRoute(givenPattern, false)
compiled, err := newRoute.GetCompiledPattern()
assertion.IsTrue(compiled != nil, "Unexpected compiled pattern")
assertion.IsTrue(err == nil, "No error raised")
}
func TestRouteMatchOk(t *testing.T) {
var assertion tameshigiri.Assertion
var givenPattern string
var newRoute *Route
assertion = tameshigiri.NewAssertion(t)
givenPattern = "/user/<alias>"
newRoute = NewRoute(givenPattern, true)
matches := newRoute.Match("/user/shiroyuki")
assertion.IsTrue(matches != nil, "There should be a match.")
actual := (*matches.Key("alias"))[0]
assertion.Equals("shiroyuki", actual, "This should be 'shiroyuki'.")
}
func TestRouteMatchFails(t *testing.T) {
var assertion tameshigiri.Assertion
var givenPattern string
var newRoute *Route
assertion = tameshigiri.NewAssertion(t)
givenPattern = "/user/<alias>"
newRoute = NewRoute(givenPattern, true)
matches := newRoute.Match("/user/shiroyuki/asdf")
assertion.IsTrue(matches == nil, "There should not be a match.")
}