11
// assert will log the given message if condition is false.
12
func assert(condition bool, t testing.TB, msg string, v ...interface{}) {
13
assertUp(condition, t, 1, msg, v...)
16
// assertUp is like assert, but used inside helper functions, to ensure that
17
// the file and line number reported by failures corresponds to one or more
18
// levels up the stack.
19
func assertUp(condition bool, t testing.TB, caller int, msg string, v ...interface{}) {
21
_, file, line, _ := runtime.Caller(caller + 1)
22
v = append([]interface{}{filepath.Base(file), line}, v...)
23
fmt.Printf("%s:%d: "+msg+"\n", v...)
28
// equals tests that the two values are equal according to reflect.DeepEqual.
29
func equals(exp, act interface{}, t testing.TB) {
30
equalsUp(exp, act, t, 1)
33
// equalsUp is like equals, but used inside helper functions, to ensure that the
34
// file and line number reported by failures corresponds to one or more levels
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)
45
// isNil reports a failure if the given value is not nil. Note that values
46
// which cannot be nil will always fail this check.
47
func isNil(obtained interface{}, t testing.TB) {
48
isNilUp(obtained, t, 1)
51
// isNilUp is like isNil, but used inside helper functions, to ensure that the
52
// file and line number reported by failures corresponds to one or more levels
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)
62
// notNil reports a failure if the given value is nil.
63
func notNil(obtained interface{}, t testing.TB) {
64
notNilUp(obtained, t, 1)
67
// notNilUp is like notNil, but used inside helper functions, to ensure that the
68
// file and line number reported by failures corresponds to one or more levels
70
func notNilUp(obtained interface{}, t testing.TB, caller int) {
72
_, file, line, _ := runtime.Caller(caller + 1)
73
fmt.Printf("%s:%d: expected non-nil, got: %v\n", filepath.Base(file), line, obtained)
78
// _isNil is a helper function for isNil and notNil, and should not be used
80
func _isNil(obtained interface{}) bool {
85
switch v := reflect.ValueOf(obtained); v.Kind() {
86
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: