-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
89 lines (72 loc) · 1.7 KB
/
assert.go
File metadata and controls
89 lines (72 loc) · 1.7 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
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
package assert
import (
"bytes"
"errors"
"fmt"
"reflect"
)
// Determines if two objects are not equal.
func NotEquals(expected, actual interface{}) error {
if err := validateEqualArgs(expected, actual); err != nil {
return err
}
if ObjectsAreEqual(expected, actual) == true {
return errors.New("the two objects are not equal")
}
return nil
}
// Determines if two objects are equal.
func Equals(expected, actual interface{}) error {
if err := validateEqualArgs(expected, actual); err != nil {
return err
}
if ObjectsAreEqual(expected, actual) == true {
return nil
}
return fmt.Errorf("expected %+v but %+v given", expected, actual)
}
// Determines if two objects are considered equal.
func ObjectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}
func Nil(object interface{}) error {
if !isNil(object) {
return fmt.Errorf("the object should be nil, but got %#v", object)
}
return nil
}
func NotNil(object interface{}) error {
if isNil(object) {
return fmt.Errorf("the object should NOT be nil")
}
return nil
}
func isNil(object interface{}) bool {
return object == nil
}
func isFunction(arg interface{}) bool {
if arg == nil {
return false
}
return reflect.TypeOf(arg).Kind() == reflect.Func
}
func validateEqualArgs(expected, actual interface{}) error {
if isFunction(expected) || isFunction(actual) {
return errors.New("cannot take func type as argument")
}
return nil
}