-
Notifications
You must be signed in to change notification settings - Fork 10
/
assert.go
174 lines (150 loc) · 3.54 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Package assert provides C-like assertions for Go. For more
// information, see assert(3) (e.g. `man 3 assert`).
//
// The T variants operating on *testing.T values; instead of killing
// the program, they call the Fatal method.
//
// If GOTRACEBACK is set to enable coredumps, assertions will generate
// coredumps.
package assert
import (
"fmt"
"os"
"runtime"
"strings"
"testing"
)
// NoDebug can be set to true to cause all asserts to be ignored.
var NoDebug bool
func die(what string, a ...string) {
_, file, line, ok := runtime.Caller(2)
if !ok {
panic(what)
}
if os.Getenv("GOTRACEBACK") == "crash" {
s := strings.Join(a, ", ")
if len(s) > 0 {
s = ": " + s
}
panic(what + s)
} else {
fmt.Fprintf(os.Stderr, "%s", what)
if len(a) > 0 {
s := strings.Join(a, ", ")
fmt.Fprintln(os.Stderr, ": "+s)
} else {
fmt.Fprintf(os.Stderr, "\n")
}
fmt.Fprintf(os.Stderr, "\t%s line %d\n", file, line)
os.Exit(1)
}
}
// Bool asserts that cond is false.
//
// For example, this would replace
// if x < 0 {
// log.Fatal("x is subzero")
// }
//
// The same assertion would be
// assert.Bool(x, "x is subzero")
func Bool(cond bool, s ...string) {
if NoDebug {
return
}
if !cond {
die("assert.Bool failed", s...)
}
}
// Error asserts that err is not nil, e.g. that an error has occurred.
//
// For example,
// if err == nil {
// log.Fatal("call to <something> should have failed")
// }
// // becomes
// assert.Error(err, "call to <something> should have failed")
func Error(err error, s ...string) {
if NoDebug {
return
} else if nil != err {
return
}
if len(s) == 0 {
die("error expected, but no error returned")
} else {
die(strings.Join(s, ", "))
}
}
// NoError asserts that err is nil, e.g. that no error has occurred.
func NoError(err error, s ...string) {
if NoDebug {
return
}
if nil != err {
die(err.Error())
}
}
// ErrorEq asserts that the actual error is the expected error.
func ErrorEq(expected, actual error) {
if NoDebug || (expected == actual) {
return
}
if expected == nil {
die(fmt.Sprintf("assert.ErrorEq: %s", actual.Error()))
}
var should string
if actual == nil {
should = "no error was returned"
} else {
should = fmt.Sprintf("have '%s'", actual)
}
die(fmt.Sprintf("assert.ErrorEq: expected '%s', but %s", expected, should))
}
// BoolT checks a boolean condition, calling Fatal on t if it is
// false.
func BoolT(t *testing.T, cond bool, s ...string) {
if !cond {
what := strings.Join(s, ", ")
if len(what) > 0 {
what = ": " + what
}
t.Fatalf("assert.Bool failed%s", what)
}
}
// ErrorT asserts that err is not nil, e.g. asserting that an error
// has occurred. See also NoErrorT.
func ErrorT(t *testing.T, err error, s ...string) {
if nil != err {
return
}
if len(s) == 0 {
t.Fatal("error expected, but no error returned")
} else {
t.Fatal(strings.Join(s, ", "))
}
}
// NoErrorT asserts that err is nil, e.g. asserting that no error has
// occurred. See also ErrorT.
func NoErrorT(t *testing.T, err error) {
if nil != err {
t.Fatalf("%s", err)
}
}
// ErrorEqT compares a pair of errors, calling Fatal on it if they
// don't match.
func ErrorEqT(t *testing.T, expected, actual error) {
if NoDebug || (expected == actual) {
return
}
if expected == nil {
die(fmt.Sprintf("assert.Error2: %s", actual.Error()))
}
var should string
if actual == nil {
should = "no error was returned"
} else {
should = fmt.Sprintf("have '%s'", actual)
}
die(fmt.Sprintf("assert.Error2: expected '%s', but %s", expected, should))
}