forked from thatisuday/clapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
assertions_test.go
63 lines (55 loc) · 1.28 KB
/
assertions_test.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
package clapper
import (
"fmt"
"reflect"
"runtime"
"testing"
)
func fail(t *testing.T) {
_, f, l, _ := runtime.Caller(2)
fmt.Printf("%s:%d\n", f, l)
t.FailNow()
}
func toErrorS(msg ...interface{}) (s string) {
args := msg[0].([]interface{})
if len(args) > 0 {
if fs, ok := args[0].(string); ok {
if len(args) > 1 {
s = fmt.Sprintf(": %s", fmt.Sprintf(fs, args[1:]...))
} else {
s = fs
}
}
}
return s
}
func assertError(t *testing.T, v error, msg ...interface{}) {
if v == nil {
t.Errorf("expected error%s", toErrorS(msg))
fail(t)
}
}
func assertNoError(t *testing.T, v error, msg ...interface{}) {
if v != nil {
t.Errorf("expected no error, got: %v%s", v, toErrorS(msg))
fail(t)
}
}
func assertEqual(t *testing.T, a interface{}, b interface{}, msg ...interface{}) {
if !reflect.DeepEqual(a, b) {
t.Errorf("expected %v, got %v%s", a, b, toErrorS(msg))
fail(t)
}
}
func assertNil(t *testing.T, a interface{}, msg ...interface{}) {
if a != nil {
t.Errorf("expected nil, got %v%s", a, toErrorS(msg))
fail(t)
}
}
func assertNotNil(t *testing.T, a interface{}, msg ...interface{}) {
if a == nil || (reflect.ValueOf(a).Kind() == reflect.Ptr && reflect.ValueOf(a).IsNil()) {
t.Errorf("expected not nil, got %v%s", a, toErrorS(msg))
fail(t)
}
}