...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package testutil
16
17 import (
18 "fmt"
19 "reflect"
20 "testing"
21 )
22
23 func AssertEqual(t *testing.T, e, a interface{}, msg ...string) {
24 t.Helper()
25 if (e == nil || a == nil) && (isNil(e) && isNil(a)) {
26 return
27 }
28 if reflect.DeepEqual(e, a) {
29 return
30 }
31 s := ""
32 if len(msg) > 1 {
33 s = msg[0] + ": "
34 }
35 s = fmt.Sprintf("%sexpected %+v, got %+v", s, e, a)
36 FatalStack(t, s)
37 }
38
39 func AssertNil(t *testing.T, v interface{}) {
40 t.Helper()
41 AssertEqual(t, nil, v)
42 }
43
44 func AssertNotNil(t *testing.T, v interface{}) {
45 t.Helper()
46 if v == nil {
47 t.Fatalf("expected non-nil, got %+v", v)
48 }
49 }
50
51 func AssertTrue(t *testing.T, v bool, msg ...string) {
52 t.Helper()
53 AssertEqual(t, true, v, msg...)
54 }
55
56 func AssertFalse(t *testing.T, v bool, msg ...string) {
57 t.Helper()
58 AssertEqual(t, false, v, msg...)
59 }
60
61 func isNil(v interface{}) bool {
62 if v == nil {
63 return true
64 }
65 rv := reflect.ValueOf(v)
66 return rv.Kind() != reflect.Struct && rv.IsNil()
67 }
68
View as plain text