forked from manythumbed/checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkers.go
244 lines (206 loc) · 5.95 KB
/
checkers.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package checkers
import (
"fmt"
"reflect"
"strings"
"github.com/davecgh/go-spew/spew"
gc "gopkg.in/check.v1"
)
type comment struct {
args []interface{}
isSpew bool
}
// Comment is like Commentf but without formatting string
func Comment(args ...interface{}) gc.CommentInterface {
return comment{args, false}
}
// CommentSpew is like Commentf but preatty print all args using spew
func CommentSpew(args ...interface{}) gc.CommentInterface {
return comment{args, true}
}
func (c comment) CheckCommentString() string {
if c.isSpew {
args := make([]string, len(c.args))
for i := range c.args {
args[i] = spew.Sdump(c.args[i])
}
return strings.Join(args, " ")
}
return fmt.Sprint(c.args...)
}
// -----------------------------------------------------------------------
type isTrueChecker struct {
*gc.CheckerInfo
}
// IsTrue checks if value == true
// For example:
//
// c.Assert(v, IsTrue)
var IsTrue gc.Checker = &isTrueChecker{
&gc.CheckerInfo{Name: "IsTrue", Params: []string{"value"}},
}
func (checker *isTrueChecker) Check(params []interface{}, names []string) (result bool, error string) {
return params[0] == true, ""
}
// -----------------------------------------------------------------------
type isFalseChecker struct {
*gc.CheckerInfo
}
// IsFalse checks if value == false
// For example:
//
// c.Assert(v, IsFalse)
var IsFalse gc.Checker = &isFalseChecker{
&gc.CheckerInfo{Name: "IsFalse", Params: []string{"value"}},
}
func (checker *isFalseChecker) Check(params []interface{}, names []string) (result bool, error string) {
return params[0] == false, ""
}
// -----------------------------------------------------------------------
type isEmptyChecker struct {
*gc.CheckerInfo
}
func (checker *isEmptyChecker) Check(params []interface{}, names []string) (result bool, error string) {
result = true
value := params[0]
if value == nil {
return
} else if value == "" {
return
} else if value == 0 {
return
} else if value == false {
return
}
objValue := reflect.ValueOf(value)
switch objValue.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return objValue.Len() == 0, ""
case reflect.Ptr:
{
if objValue.IsNil() {
return
}
}
}
return false, ""
}
// IsEmpty asserts that the specified object is empty. I.e. nil, "", false, 0 or a slice with len == 0.
// For example:
//
// c.Assert(v, IsEmpty)
var IsEmpty gc.Checker = &isEmptyChecker{
&gc.CheckerInfo{Name: "IsEmpty", Params: []string{"value"}},
}
// -----------------------------------------------------------------------
type satisfiesChecker struct {
*gc.CheckerInfo
}
// Satisfies checks whether a value causes the argument
// function to return true. The function must be of
// type func(T) bool where the value being checked
// is assignable to T.
var Satisfies gc.Checker = &satisfiesChecker{
&gc.CheckerInfo{
Name: "Satisfies",
Params: []string{"obtained", "func(T) bool"},
},
}
func (checker *satisfiesChecker) Check(params []interface{}, names []string) (result bool, error string) {
f := reflect.ValueOf(params[1])
ft := f.Type()
if ft.Kind() != reflect.Func ||
ft.NumIn() != 1 ||
ft.NumOut() != 1 ||
ft.Out(0) != reflect.TypeOf(true) {
return false, fmt.Sprintf("expected func(T) bool, got %s", ft)
}
v := reflect.ValueOf(params[0])
if !v.IsValid() {
if !canBeNil(ft.In(0)) {
return false, fmt.Sprintf("cannot assign nil to argument %T", ft.In(0))
}
v = reflect.Zero(ft.In(0))
}
if !v.Type().AssignableTo(ft.In(0)) {
return false, fmt.Sprintf("wrong argument type %s for %s", v.Type(), ft)
}
return f.Call([]reflect.Value{v})[0].Interface().(bool), ""
}
func canBeNil(t reflect.Type) bool {
switch t.Kind() {
case reflect.Chan,
reflect.Func,
reflect.Interface,
reflect.Map,
reflect.Ptr,
reflect.Slice:
return true
}
return false
}
// -----------------------------------------------------------------------
type hasPrefixChecker struct {
*gc.CheckerInfo
}
// HasPrefix checker for checking strings
var HasPrefix gc.Checker = &hasPrefixChecker{
&gc.CheckerInfo{Name: "HasPrefix", Params: []string{"obtained", "expected"}},
}
func (checker *hasPrefixChecker) Check(params []interface{}, names []string) (result bool, error string) {
expected, ok := params[1].(string)
if !ok {
return false, "expected must be a string"
}
obtained, isString := stringOrStringer(params[0])
if isString {
return strings.HasPrefix(obtained, expected), ""
}
return false, "Obtained value is not a string and has no .String()"
}
// -----------------------------------------------------------------------
type hasSuffixChecker struct {
*gc.CheckerInfo
}
// HasSuffix Checker
var HasSuffix gc.Checker = &hasSuffixChecker{
&gc.CheckerInfo{Name: "HasSuffix", Params: []string{"obtained", "expected"}},
}
func (checker *hasSuffixChecker) Check(params []interface{}, names []string) (result bool, error string) {
expected, ok := params[1].(string)
if !ok {
return false, "expected must be a string"
}
obtained, isString := stringOrStringer(params[0])
if isString {
return strings.HasSuffix(obtained, expected), ""
}
return false, "Obtained value is not a string and has no .String()"
}
// -----------------------------------------------------------------------
type errorContains struct {
*gc.CheckerInfo
}
// ErrorContains checks if the error message (output of the .String() method)
// contains the given string.
var ErrorContains gc.Checker = &errorContains{
&gc.CheckerInfo{Name: "ErrorContains", Params: []string{"obtained", "expected"}},
}
type errorI interface {
Error() string
}
func (checker *errorContains) Check(params []interface{}, names []string) (result bool, error string) {
expected, ok := params[1].(string)
if !ok {
return false, "expected must be a string"
}
if params[0] == nil {
return false, "obtained nil"
}
err, isErr := params[0].(errorI)
if !isErr {
return false, "Obtained value doesn't implement error interface"
}
msg := err.Error()
return strings.Contains(msg, expected), ""
}