-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
150 lines (117 loc) · 3.2 KB
/
resource.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package mocks
import (
"fmt"
. "github.com/konjoot/treacy/matchers"
"github.com/onsi/gomega/matchers"
"github.com/onsi/gomega/types"
"net/http/httptest"
"reflect"
)
func Resource() *ResourceMock {
return &ResourceMock{form: &resourceFormMock{}}
}
type ResourceMock struct {
created bool
form *resourceFormMock
}
type resourceFormMock struct {
Name string `binding:"required"`
Desc string `binding:"required"`
}
func (f *resourceFormMock) ValOf(name string) string {
r := reflect.ValueOf(f)
return reflect.Indirect(r).FieldByName(name).String()
}
func (r *ResourceMock) Form() interface{} {
return r.form
}
func (r *ResourceMock) Save() (ok bool) {
ok, r.created = true, true
return
}
func (r *ResourceMock) Url() string {
return "/tests/1"
}
func (r *ResourceMock) IsCreated() bool {
return r.created
}
func (r *ResourceMock) IsBindedWith(m map[string]string) (ok bool) {
for key, val := range m {
if ok = r.form.ValOf(key) == val; !ok {
return
}
}
return
}
func (r *ResourceMock) String() string {
return fmt.Sprintf("&ResourceMock{ created: %t }", r.created)
}
// BeCreated matcher
func BeCreated() *BaseMatcher {
return Matcher(&beCreatedMatcher{})
}
type beCreatedMatcher struct{}
func (_ *beCreatedMatcher) Matcher() types.GomegaMatcher {
return &matchers.BeTrueMatcher{}
}
func (_ *beCreatedMatcher) Prepare(actual interface{}) interface{} {
return actual.(*ResourceMock).IsCreated()
}
func (_ *beCreatedMatcher) Format(actual interface{}) string {
return actual.(*ResourceMock).String()
}
func (_ *beCreatedMatcher) Message() string {
return "to be created"
}
func (_ *beCreatedMatcher) String() (s string) {
return
}
// HaveHeader matcher
func HaveHeader(name string) *haveHeaderMatcher {
return &haveHeaderMatcher{Name: name}
}
type haveHeaderMatcher struct {
Name string
Expected string
}
func (m *haveHeaderMatcher) With(url string) *BaseMatcher {
m.Expected = url
return Matcher(m)
}
func (m *haveHeaderMatcher) Matcher() types.GomegaMatcher {
return &matchers.EqualMatcher{Expected: m.Expected}
}
func (m *haveHeaderMatcher) Prepare(actual interface{}) interface{} {
return actual.(*httptest.ResponseRecorder).Header().Get(m.Name)
}
func (_ *haveHeaderMatcher) Format(actual interface{}) string {
return fmt.Sprintf("%#v", actual.(*httptest.ResponseRecorder).HeaderMap)
}
func (m *haveHeaderMatcher) Message() string {
return "to have header"
}
func (m *haveHeaderMatcher) String() string {
return fmt.Sprintf("\"%s\" with \"%s\"", m.Name, m.Expected)
}
// BindedWith matcher
func BindedWith(expected map[string]string) *BaseMatcher {
return Matcher(&bindedWithMatcher{Expected: expected})
}
type bindedWithMatcher struct {
Expected map[string]string
}
func (m *bindedWithMatcher) Matcher() types.GomegaMatcher {
return &matchers.BeTrueMatcher{}
}
func (m *bindedWithMatcher) Prepare(actual interface{}) interface{} {
return actual.(*ResourceMock).IsBindedWith(m.Expected)
}
func (_ *bindedWithMatcher) Format(actual interface{}) string {
return fmt.Sprintf("%#v", actual.(*ResourceMock).Form())
}
func (_ *bindedWithMatcher) Message() string {
return "to have been binded with"
}
func (m *bindedWithMatcher) String() string {
return fmt.Sprintf("%#v", m.Expected)
}