github.com/dolotech/hongbao@v0.0.0-20191130105438-fd59d7a5dda5/src/utils/assert/assert.go (about) 1 package assert 2 // Testing helpers for doozer. 3 4 import ( 5 "github.com/kr/pretty" 6 "reflect" 7 "testing" 8 "runtime" 9 "fmt" 10 ) 11 12 func assert(t *testing.T, result bool, f func(), cd int) { 13 if !result { 14 _, file, line, _ := runtime.Caller(cd + 1) 15 t.Errorf("%s:%d", file, line) 16 f() 17 t.FailNow() 18 } 19 } 20 21 func equal(t *testing.T, exp, got interface{}, cd int, args ...interface{}) { 22 fn := func() { 23 for _, desc := range pretty.Diff(exp, got) { 24 t.Error("!", desc) 25 } 26 if len(args) > 0 { 27 t.Error("!", " -", fmt.Sprint(args...)) 28 } 29 } 30 result := reflect.DeepEqual(exp, got) 31 assert(t, result, fn, cd+1) 32 } 33 34 func tt(t *testing.T, result bool, cd int, args ...interface{}) { 35 fn := func() { 36 t.Errorf("! Failure") 37 if len(args) > 0 { 38 t.Error("!", " -", fmt.Sprint(args...)) 39 } 40 } 41 assert(t, result, fn, cd+1) 42 } 43 44 func T(t *testing.T, result bool, args ...interface{}) { 45 tt(t, result, 1, args...) 46 } 47 48 func Tf(t *testing.T, result bool, format string, args ...interface{}) { 49 tt(t, result, 1, fmt.Sprintf(format, args...)) 50 } 51 52 func Equal(t *testing.T, exp, got interface{}, args ...interface{}) { 53 equal(t, exp, got, 1, args...) 54 } 55 56 func Equalf(t *testing.T, exp, got interface{}, format string, args ...interface{}) { 57 equal(t, exp, got, 1, fmt.Sprintf(format, args...)) 58 } 59 60 func NotEqual(t *testing.T, exp, got interface{}, args ...interface{}) { 61 fn := func() { 62 t.Errorf("! Unexpected: <%#v>", exp) 63 if len(args) > 0 { 64 t.Error("!", " -", fmt.Sprint(args...)) 65 } 66 } 67 result := !reflect.DeepEqual(exp, got) 68 assert(t, result, fn, 1) 69 } 70 71 func Panic(t *testing.T, err interface{}, fn func()) { 72 defer func() { 73 equal(t, err, recover(), 3) 74 }() 75 fn() 76 }