-
Notifications
You must be signed in to change notification settings - Fork 39
/
assert.go
76 lines (66 loc) · 1.62 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
package assert
// Testing helpers for doozer.
import (
"github.com/kr/pretty"
"reflect"
"testing"
"runtime"
"fmt"
)
func assert(t *testing.T, result bool, f func(), cd int) {
if !result {
_, file, line, _ := runtime.Caller(cd + 1)
t.Errorf("%s:%d", file, line)
f()
t.FailNow()
}
}
func equal(t *testing.T, exp, got interface{}, cd int, args ...interface{}) {
fn := func() {
for _, desc := range pretty.Diff(exp, got) {
t.Error("!", desc)
}
if len(args) > 0 {
t.Error("!", " -", fmt.Sprint(args...))
}
}
result := reflect.DeepEqual(exp, got)
assert(t, result, fn, cd+1)
}
func tt(t *testing.T, result bool, cd int, args ...interface{}) {
fn := func() {
t.Errorf("! Failure")
if len(args) > 0 {
t.Error("!", " -", fmt.Sprint(args...))
}
}
assert(t, result, fn, cd+1)
}
func T(t *testing.T, result bool, args ...interface{}) {
tt(t, result, 1, args...)
}
func Tf(t *testing.T, result bool, format string, args ...interface{}) {
tt(t, result, 1, fmt.Sprintf(format, args...))
}
func Equal(t *testing.T, exp, got interface{}, args ...interface{}) {
equal(t, exp, got, 1, args...)
}
func Equalf(t *testing.T, exp, got interface{}, format string, args ...interface{}) {
equal(t, exp, got, 1, fmt.Sprintf(format, args...))
}
func NotEqual(t *testing.T, exp, got interface{}, args ...interface{}) {
fn := func() {
t.Errorf("! Unexpected: <%#v>", exp)
if len(args) > 0 {
t.Error("!", " -", fmt.Sprint(args...))
}
}
result := !reflect.DeepEqual(exp, got)
assert(t, result, fn, 1)
}
func Panic(t *testing.T, err interface{}, fn func()) {
defer func() {
equal(t, err, recover(), 3)
}()
fn()
}