-
Notifications
You must be signed in to change notification settings - Fork 1
/
operand.go
231 lines (215 loc) · 6.54 KB
/
operand.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
package assert
// Copyright 2015 Ernest Micklei. All rights reserved.
// Use of this source code is governed by a license
// that can be found in the LICENSE file.
import (
"reflect"
"time"
)
// Operand represent a value
type Operand struct {
// this reference is used to report a test failure
a testingA
// description of the value, typically a variable or field name
label string
// actual value of any type
value interface{}
// used to operate on two values
operator RelationalOperator
}
// With returns a copy Operand that will use the RelationalOperator.
func (o Operand) With(r RelationalOperator) Operand {
return Operand{o.a, o.label, o.value, r}
}
// Equals checks whether the value we have got is equal to the value we want.
func (o Operand) Equals(want interface{}) {
o.a.t.Helper()
if !o.operator.Apply(o.value, want) {
if reflect.DeepEqual(o.value, want) {
return
}
logCall(o.a.t, "Equals")
Fatalf(o.a.t, "\ngot [%v] (%T) for \"%s\" but want [%v] (%T)",
o.value, o.value,
o.label,
want, want)
} else {
Logf(o.a.t, "%s = %v", o.label, o.value)
}
}
// GreaterThan checks whether the value we have got is greater than to the value we want.
func (o Operand) GreaterThan(want interface{}) {
o.a.t.Helper()
left, ok := o.value.(int)
if !ok {
logCall(o.a.t, "GreaterThan")
Fatalf(o.a.t, "got [%v](%T) for \"%s\" but want an int", o.value, o.value, o.label)
}
if !ok {
logCall(o.a.t, "GreaterThan")
Fatalf(o.a.t, "got [%v](%T) for \"%s\" but want an int", o.value, o.value, o.label)
}
right, ok := want.(int)
if !ok {
logCall(o.a.t, "GreaterThan")
Fatalf(o.a.t, "GreaterThen works for int only. Pull requests are welcome")
}
if left > right {
Logf(o.a.t, "%d is greater then %v", left, right)
} else {
logCall(o.a.t, "GreaterThan")
Fatalf(o.a.t, "got [%v] for \"%s\" but want it greater than [%v]", o.value, o.label, right)
}
}
// Before checks whether the value we have got is before a moment.
func (o Operand) Before(moment time.Time) {
o.a.t.Helper()
left, ok := o.value.(time.Time)
if !ok {
logCall(o.a.t, "Before")
Fatalf(o.a.t, "got [%v](%T) for \"%s\" but want a Time", o.value, o.value, o.label)
}
if left.Before(moment) {
Logf(o.a.t, "%s is before %v", o.label, moment)
} else {
logCall(o.a.t, "Before")
Fatalf(o.a.t, "got [%v] for \"%s\" but want it before [%v]", o.value, o.label, moment)
}
}
// After checks whether the value we have got is after a moment.
func (o Operand) After(moment time.Time) {
o.a.t.Helper()
left, ok := o.value.(time.Time)
if !ok {
logCall(o.a.t, "After")
Fatalf(o.a.t, "got [%v](%T) for \"%s\" but want a Time", o.value, o.value, o.label)
}
if left.After(moment) {
Logf(o.a.t, "%s is after %v", o.label, moment)
} else {
logCall(o.a.t, "After")
Fatalf(o.a.t, "got [%v] for \"%s\" but want it after [%v]", o.value, o.label, moment)
}
}
// IsKindOf checks whether the values are of the same type
func (o Operand) IsKindOf(v interface{}) {
o.a.t.Helper()
leftType := reflect.TypeOf(o.value)
rightType := reflect.TypeOf(v)
if leftType != rightType {
logCall(o.a.t, "IsKindOf")
Fatalf(o.a.t, "got [%v] for \"%s\" but want [%v]", leftType, o.label, rightType)
} else {
Logf(o.a.t, "%s is kind of %v", o.label, rightType)
}
}
// IsNil checks whether the value is nil
func (o Operand) IsNil() {
o.a.t.Helper()
if o.operator.Apply(o.value, nil) {
Logf(o.a.t, "%s is nil", o.label)
return
} else {
// from github.com/go-check/check
switch v := reflect.ValueOf(o.value); v.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
if v.IsNil() {
return
}
}
}
logCall(o.a.t, "IsNil")
Fatalf(o.a.t, "got [%v] for \"%s\" but want [nil]", o.value, o.label)
}
// IsNotNil checks whether the value is nil
func (o Operand) IsNotNil() {
o.a.t.Helper()
if o.operator.Apply(o.value, nil) {
logCall(o.a.t, "IsNotNil")
Fatalf(o.a.t, "got unexpected [%v] for \"%s\"", o.value, o.label)
} else {
Logf(o.a.t, "%s is not nil", o.value)
}
}
// IsTrue checks whether the value is true
func (o Operand) IsTrue() {
if o.operator.Apply(o.value, false) { // i.e fail if !true
logCall(o.a.t, "IsTrue")
Fatalf(o.a.t, "got [%v] for \"%s\" but want [true]", o.value, o.label)
} else {
Logf(o.a.t, "%s is true", o.label)
}
}
// IsFalse checks whether the value is false
func (o Operand) IsFalse() {
if o.operator.Apply(o.value, true) { // i.e fail if !false
logCall(o.a.t, "IsFalse")
Fatalf(o.a.t, "got [%v] for \"%s\" but want [false]", o.value, o.label)
} else {
Logf(o.a.t, "%s is false", o.label)
}
}
// Not creates a new Operand with a negated version of its comparator.
func (o Operand) Not() Operand {
return Operand{o.a, o.label, o.value, not{o.operator}}
}
// Len checks that len(value) or value.Len() is equals to the given length.
// It operates on Array, Chan, Map, Slice, or String and objects that implement Len() int.
func (o Operand) Len(want int) {
// panic catcher
defer func() {
if err := recover(); err != nil {
// try calling Len
rt := reflect.TypeOf(o.value)
rf, ok := rt.MethodByName("Len")
if !ok {
logCall(o.a.t, "Len")
Fatalf(o.a.t, "got [%v] for \"%s\" but it does not implement Len() int", o.value, o.label)
return
}
rv := reflect.ValueOf(o.value)
gotvs := rf.Func.Call([]reflect.Value{rv})
got := int(gotvs[0].Int())
if !o.operator.Apply(got, want) {
logCall(o.a.t, "Len")
Fatalf(o.a.t, "got [%v] for \"%s\" but want [%d]", got, o.label, want)
}
}
}()
rv := reflect.ValueOf(o.value)
got := rv.Len()
if !o.operator.Apply(got, want) {
logCall(o.a.t, "Len")
Fatalf(o.a.t, "got [%v] for \"%s\" but want [%d]", got, o.label, want)
}
}
// IsEmpty checks that len(value) or value.Len() is zero.
// It operates on Array, Chan, Map, Slice, or String and objects that implement Len() int.
func (o Operand) IsEmpty() {
// panic catcher
defer func() {
if err := recover(); err != nil {
// try calling Len
rt := reflect.TypeOf(o.value)
rf, ok := rt.MethodByName("Len")
if !ok {
logCall(o.a.t, "IsEmpty")
Fatalf(o.a.t, "got [%v] for \"%s\" but it does not implement Len() int", o.value, o.label)
return
}
rv := reflect.ValueOf(o.value)
gotvs := rf.Func.Call([]reflect.Value{rv})
got := int(gotvs[0].Int())
if !o.operator.Apply(got, 0) {
logCall(o.a.t, "IsEmpty")
Fatalf(o.a.t, "got [%v] for len(\"%s\") but want > 0", got, o.label)
}
}
}()
rv := reflect.ValueOf(o.value)
got := rv.Len()
if !o.operator.Apply(got, 0) {
logCall(o.a.t, "IsEmpty")
Fatalf(o.a.t, "got [%v] for len(\"%s\") but want > 0", got, o.label)
}
}