forked from manythumbed/checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
equals.go
46 lines (39 loc) · 1.06 KB
/
equals.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
package checkers
import (
"fmt"
"reflect"
gc "gopkg.in/check.v1"
)
// -----------------------------------------------------------------------
type strEquals struct {
*gc.CheckerInfo
}
func (c *strEquals) Check(params []interface{}, names []string) (result bool, error string) {
p1, p2 := params[0], params[1]
p1Nil, p2Nil := isNil(p1), isNil(p2)
if p1Nil || p2Nil {
if p1Nil == p2Nil {
return true, ""
}
if p2Nil {
return false, "expecting nil, got not-nil value"
}
return false, "expecting not-nil value, got nil"
}
return fmt.Sprint(p1) == fmt.Sprint(p2), ""
}
// StrEquals checks if fmt.Sprint values of objects are equal
var StrEquals gc.Checker = &strEquals{
&gc.CheckerInfo{Name: "StrEquals", Params: []string{"obtained", "expected"}}}
// copied from gopkg.in/check.v1
func isNil(obtained interface{}) (result bool) {
if obtained == nil {
result = true
} else {
switch v := reflect.ValueOf(obtained); v.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return v.IsNil()
}
}
return
}