1 package lumberjack
2
3 import (
4 "fmt"
5 "path/filepath"
6 "reflect"
7 "runtime"
8 "testing"
9 )
10
11
12 func assert(condition bool, t testing.TB, msg string, v ...interface{}) {
13 assertUp(condition, t, 1, msg, v...)
14 }
15
16
17
18
19 func assertUp(condition bool, t testing.TB, caller int, msg string, v ...interface{}) {
20 if !condition {
21 _, file, line, _ := runtime.Caller(caller + 1)
22 v = append([]interface{}{filepath.Base(file), line}, v...)
23 fmt.Printf("%s:%d: "+msg+"\n", v...)
24 t.FailNow()
25 }
26 }
27
28
29 func equals(exp, act interface{}, t testing.TB) {
30 equalsUp(exp, act, t, 1)
31 }
32
33
34
35
36 func equalsUp(exp, act interface{}, t testing.TB, caller int) {
37 if !reflect.DeepEqual(exp, act) {
38 _, file, line, _ := runtime.Caller(caller + 1)
39 fmt.Printf("%s:%d: exp: %v (%T), got: %v (%T)\n",
40 filepath.Base(file), line, exp, exp, act, act)
41 t.FailNow()
42 }
43 }
44
45
46
47 func isNil(obtained interface{}, t testing.TB) {
48 isNilUp(obtained, t, 1)
49 }
50
51
52
53
54 func isNilUp(obtained interface{}, t testing.TB, caller int) {
55 if !_isNil(obtained) {
56 _, file, line, _ := runtime.Caller(caller + 1)
57 fmt.Printf("%s:%d: expected nil, got: %v\n", filepath.Base(file), line, obtained)
58 t.FailNow()
59 }
60 }
61
62
63 func notNil(obtained interface{}, t testing.TB) {
64 notNilUp(obtained, t, 1)
65 }
66
67
68
69
70 func notNilUp(obtained interface{}, t testing.TB, caller int) {
71 if _isNil(obtained) {
72 _, file, line, _ := runtime.Caller(caller + 1)
73 fmt.Printf("%s:%d: expected non-nil, got: %v\n", filepath.Base(file), line, obtained)
74 t.FailNow()
75 }
76 }
77
78
79
80 func _isNil(obtained interface{}) bool {
81 if obtained == nil {
82 return true
83 }
84
85 switch v := reflect.ValueOf(obtained); v.Kind() {
86 case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
87 return v.IsNil()
88 }
89
90 return false
91 }
92
View as plain text