-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertions.go
More file actions
62 lines (52 loc) · 1.14 KB
/
assertions.go
File metadata and controls
62 lines (52 loc) · 1.14 KB
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
package texp
import (
"reflect"
)
// Not method is useful to express negative assertions
func (e *exp) Not() *exp {
e.modF = negationModifierFunc
return e
}
// ToBeTrue method match the true value of the sample
func (e *exp) ToBeTrue(msgs ...interface{}) *exp {
if !e.modF(isTrue)(e.sample) {
e.logAndFail(msgs...)
}
e.modF = neutralModifierFunc
return e
}
// ToEqual method test the equality between sample and expectedValue
func (e *exp) ToEqual(expValue interface{}, msgs ...interface{}) *exp {
if !reflect.DeepEqual(e.sample, expValue) {
e.logAndFail(msgs...)
}
return e
}
// ToBeNil method returns true if the sample can be
// considered Nil
func (e *exp) ToBeNil(msgs ...interface{}) *exp {
if !e.modF(isNil)(e.sample) {
e.logAndFail(msgs...)
}
e.modF = neutralModifierFunc
return e
}
func isNil(o interface{}) bool {
if o == nil {
return true
}
v := reflect.ValueOf(o)
if canUseIsNilByKind(v.Kind()) && v.IsNil() {
return true
}
return false
}
func isTrue(o interface{}) bool {
return o == true
}
func canUseIsNilByKind(k reflect.Kind) bool {
if k >= reflect.Chan && k <= reflect.Slice {
return true
}
return false
}