-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontracts_test.go
69 lines (56 loc) · 2.33 KB
/
contracts_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
package httprouter
import (
"reflect"
"testing"
)
func TestParseMethods(t *testing.T) {
assertParsedMethods(t, "GET|PUT|OPTIONS", MethodGet|MethodPut|MethodOptions)
assertParsedMethods(t, "head | post | trace", MethodHead|MethodPost|MethodTrace)
assertParsedMethods(t, "\tconnect\n | \n\tunknown \n|\n delete", MethodConnect|MethodNone|MethodDelete)
method := MethodPatch | MethodHead | MethodOptions
Assert(t).That(method.String()).Equals("HEAD|OPTIONS|PATCH")
Assert(t).That(method.String()).Equals(method.GoString())
Assert(t).That(MethodNone.String()).Equals("")
}
func assertParsedMethods(t *testing.T, raw string, expected Method) {
parsed := ParseMethods(raw)
Assert(t).That(parsed).Equals(expected)
}
func TestParseRoutes(t *testing.T) {
assertParsedRoutes(t, "GET|PUT", "/resource|/another/resource",
Route{AllowedMethods: MethodGet | MethodPut, Path: "/resource"},
Route{AllowedMethods: MethodGet | MethodPut, Path: "/another/resource"})
assertParsedRoutes(t, "HEAD|OPTIONS", " \n\t /Path/To/Document | \n\t /Document/* \t\n",
Route{AllowedMethods: MethodHead | MethodOptions, Path: "/Path/To/Document"},
Route{AllowedMethods: MethodHead | MethodOptions, Path: "/Document/*"})
route := ParseRoute("GET|HEAD", "/document", nil)
Assert(t).That(route.String()).Equals("GET|HEAD /document")
Assert(t).That(route.String()).Equals(route.GoString())
}
func assertParsedRoutes(t *testing.T, methods, paths string, expectedRoutes ...Route) {
parsed := ParseRoutes(methods, paths, nil)
Assert(t).That(len(parsed)).Equals(len(expectedRoutes))
for i := range parsed {
Assert(t).That(parsed[i]).Equals(expectedRoutes[i])
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type That struct{ t *testing.T }
type Assertion struct {
*testing.T
actual interface{}
}
func Assert(t *testing.T) *That { return &That{t: t} }
func (this *That) That(actual interface{}) *Assertion { return &Assertion{T: this.t, actual: actual} }
func (this *Assertion) IsNil() {
this.Helper()
if this.actual != nil && !reflect.ValueOf(this.actual).IsNil() {
this.Equals(nil)
}
}
func (this *Assertion) Equals(expected interface{}) {
this.Helper()
if !reflect.DeepEqual(this.actual, expected) {
this.Errorf("\nExpected: %#v\nActual: %#v", expected, this.actual)
}
}