-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
60 lines (53 loc) · 1.57 KB
/
assert.go
File metadata and controls
60 lines (53 loc) · 1.57 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
// Package assert provides an assortment of assertions to be used in conjunction
// with golang's testing package to streamline tests.
package assert
import (
"testing"
)
// IsError fails the test if a given error is nil.
func IsError(t *testing.T, err error) {
if err == nil {
t.Error(msgIsError)
}
}
// FatalIsError fails the test immediately if a given error is nil.
func FatalIsError(t *testing.T, err error) {
if err == nil {
t.Fatal(msgIsError)
}
}
// NotError fails the test if a given error is not nil.
func NotError(t *testing.T, err error) {
if err != nil {
t.Errorf(formatIsNotError, err)
}
}
// FatalNotError fails the test immediately if a given error is not nil.
func FatalNotError(t *testing.T, err error) {
if err != nil {
t.Fatalf(formatIsNotError, err)
}
}
// CondError checks that a given error was expected or not.
//
// If expected is true and err is nil, the test will fail. Conversely, if
// expected is false and err is not nil, the test will also fail.
func CondError(t *testing.T, expected bool, err error) {
if expected && err == nil {
t.Error(msgIsError)
} else if !expected && err != nil {
t.Errorf(formatIsNotError, err)
}
}
// FatalCondError checks that a given error was expected or not.
//
// If expected is true and err is nil, the test will fail immediately.
// Conversely, if expected is false and err is not nil, the test will also fail
// immediately.
func FatalCondError(t *testing.T, expected bool, err error) {
if expected && err == nil {
t.Fatal(msgIsError)
} else if !expected && err != nil {
t.Fatalf(formatIsNotError, err)
}
}