github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/output/lumberjack/testing_test.go (about) 1 package lumberjack 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "reflect" 7 "runtime" 8 "testing" 9 ) 10 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...) 14 } 15 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{}) { 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 // 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) 31 } 32 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 35 // up the stack. 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 // 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) 49 } 50 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 53 // up the stack. 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 // notNil reports a failure if the given value is nil. 63 func notNil(obtained interface{}, t testing.TB) { 64 notNilUp(obtained, t, 1) 65 } 66 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 69 // up the stack. 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 // _isNil is a helper function for isNil and notNil, and should not be used 79 // directly. 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 }