-
Notifications
You must be signed in to change notification settings - Fork 9
/
assert.go
71 lines (58 loc) · 1.64 KB
/
assert.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
package hsm
import "reflect"
import "fmt"
// AssertEqual asserts the equality of actual and expected.
func AssertEqual(expected, actual interface{}) {
if !ObjectAreEqual(expected, actual) {
panic(fmt.Sprintf("Equal(%#v, %#v) fail", expected, actual))
}
}
// AssertEqual asserts the inequality of actual and expected.
func AssertNotEqual(expected, actual interface{}) {
if ObjectAreEqual(expected, actual) {
panic(fmt.Sprintf("NotEqual(%#v, %#v) fail", expected, actual))
}
}
// ObjectAreEqual test whether actual is equal to expected.
// It returns true when equal, otherwise returns false.
func ObjectAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
if reflect.DeepEqual(expected, actual) {
return true
}
expectedValue := reflect.ValueOf(expected)
actualValue := reflect.ValueOf(actual)
if expectedValue == actualValue {
return true
}
if actualValue.Type().ConvertibleTo(expectedValue.Type()) &&
expectedValue == actualValue.Convert(expectedValue.Type()) {
return true
}
if fmt.Sprintf("%#v", expected) == fmt.Sprintf("%#v", actual) {
return true
}
return false
}
// AssertTrue asserts on truth of value.
func AssertTrue(value bool) {
if !value {
panic(fmt.Sprintf("True(value=%#v) fail", value))
}
}
// AssertFalse asserts on falsehood of value.
func AssertFalse(value bool) {
if value {
panic(fmt.Sprintf("False(value=%#v) fail", value))
}
}
// AssertNil asserts on nullability of value.
func AssertNil(value interface{}) {
AssertEqual(nil, value)
}
// AssertNotNil is opposite to AssertNil.
func AssertNotNil(value interface{}) {
AssertNotEqual(nil, value)
}