-
Notifications
You must be signed in to change notification settings - Fork 101
/
assert.go
137 lines (117 loc) · 3.05 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package qbs
import (
"fmt"
"reflect"
"runtime"
)
type Assert struct {
tester
}
type tester interface {
Fail()
Failed() bool
FailNow()
Log(args ...interface{})
Logf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Skip(args ...interface{})
Skipf(format string, args ...interface{})
SkipNow()
Skipped() bool
}
type benchmarker interface {
StartTimer()
StopTimer()
}
func NewAssert(t tester) *Assert {
return &Assert{t}
}
func (ast *Assert) Nil(value interface{}, logs ...interface{}) {
ast.nilAssert(false, true, value, logs...)
}
func (ast *Assert) MustNil(value interface{}, logs ...interface{}) {
ast.nilAssert(true, true, value, logs...)
}
func (ast *Assert) NotNil(value interface{}, logs ...interface{}) {
ast.nilAssert(false, false, value, logs...)
}
func (ast *Assert) MustNotNil(value interface{}, logs ...interface{}) {
ast.nilAssert(true, false, value, logs...)
}
func (ast *Assert) nilAssert(fatal bool, isNil bool, value interface{}, logs ...interface{}) {
if isNil != (value == nil || reflect.ValueOf(value).IsNil()) {
ast.logCaller()
if len(logs) > 0 {
ast.Log(logs...)
} else {
if isNil {
ast.Log("value is not nil:", value)
} else {
ast.Log("value is nil")
}
}
ast.failIt(fatal)
}
}
func (ast *Assert) True(boolValue bool, logs ...interface{}) {
ast.trueAssert(false, boolValue, logs...)
}
func (ast *Assert) MustTrue(boolValue bool, logs ...interface{}) {
ast.trueAssert(true, boolValue, logs...)
}
func (ast *Assert) trueAssert(fatal bool, value bool, logs ...interface{}) {
if !value {
ast.logCaller()
if len(logs) > 0 {
ast.Log(logs...)
} else {
ast.Logf("value is not true")
}
ast.failIt(fatal)
}
}
func (ast *Assert) Equal(expected, actual interface{}, logs ...interface{}) {
ast.equalSprintAssert(false, true, expected, actual, logs...)
}
func (ast *Assert) MustEqual(expected, actual interface{}, logs ...interface{}) {
ast.equalSprintAssert(true, true, expected, actual, logs...)
}
func (ast *Assert) NotEqual(expected, actual interface{}, logs ...interface{}) {
ast.equalSprintAssert(false, false, expected, actual, logs...)
}
func (ast *Assert) MustNotEqual(expected, actual interface{}, logs ...interface{}) {
ast.equalSprintAssert(true, false, expected, actual, logs...)
}
func (ast *Assert) equalSprintAssert(fatal bool, isEqual bool, expected, actual interface{}, logs ...interface{}) {
expectedStr := fmt.Sprint(expected)
actualStr := fmt.Sprint(actual)
if isEqual != (expectedStr == actualStr) {
ast.logCaller()
if len(logs) > 0 {
ast.Log(logs...)
} else {
if isEqual {
ast.Log("Values not equal")
} else {
ast.Log("Values equal")
}
}
ast.Log("Expected: ", expected)
ast.Log("Actual: ", actual)
ast.failIt(fatal)
}
}
func (ast *Assert) logCaller() {
_, file, line, _ := runtime.Caller(3)
ast.Logf("Caller: %v:%d", file, line)
}
func (ast *Assert) failIt(fatal bool) {
if fatal {
ast.FailNow()
} else {
ast.Fail()
}
}