-
Notifications
You must be signed in to change notification settings - Fork 5
/
object.go
60 lines (47 loc) · 1.05 KB
/
object.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
package gohamcrest
import (
"reflect"
)
type isEqual struct {
BaseMatcher
}
func (this *isEqual)Match(actual interface{}) bool {
return reflect.DeepEqual(this.Expected, actual)
}
//Create a Matcher for match the actual object is equal excepted object
//example:
//int:gohamcrest.Assert(t,2,Equal(2))
//string:gohamcrest.Assert(t,"joe",Equal("joe"))
func Equal(expected interface{}) Matcher {
matcher := &isEqual{}
matcher.Expected=expected
matcher.Reason="%v %s equal %v"
return matcher
}
func NotEqual(expected interface{}) Matcher{
return Not(Equal(expected))
}
func NotNilVal() Matcher{
return Not(NilVal())
}
type isNil struct {
BaseMatcher
}
func (this *isNil)Match(actual interface{}) bool {
if actual == nil {
return true
}
value := reflect.ValueOf(actual)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}
//Create a Matcher for match the object is nil or not.
//example
func NilVal() Matcher {
matcher := &isNil{}
matcher.Reason="%v %s equal %v"
return matcher
}