testing_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package lumberjack
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "reflect"
  6. "runtime"
  7. "testing"
  8. )
  9. // assert will log the given message if condition is false.
  10. func assert(condition bool, t testing.TB, msg string, v ...interface{}) {
  11. assertUp(condition, t, 1, msg, v...)
  12. }
  13. // assertUp is like assert, but used inside helper functions, to ensure that
  14. // the file and line number reported by failures corresponds to one or more
  15. // levels up the stack.
  16. func assertUp(condition bool, t testing.TB, caller int, msg string, v ...interface{}) {
  17. if !condition {
  18. _, file, line, _ := runtime.Caller(caller + 1)
  19. v = append([]interface{}{filepath.Base(file), line}, v...)
  20. fmt.Printf("%s:%d: "+msg+"\n", v...)
  21. t.FailNow()
  22. }
  23. }
  24. // equals tests that the two values are equal according to reflect.DeepEqual.
  25. func equals(exp, act interface{}, t testing.TB) {
  26. equalsUp(exp, act, t, 1)
  27. }
  28. // equalsUp is like equals, but used inside helper functions, to ensure that the
  29. // file and line number reported by failures corresponds to one or more levels
  30. // up the stack.
  31. func equalsUp(exp, act interface{}, t testing.TB, caller int) {
  32. if !reflect.DeepEqual(exp, act) {
  33. _, file, line, _ := runtime.Caller(caller + 1)
  34. fmt.Printf("%s:%d: exp: %v (%T), got: %v (%T)\n",
  35. filepath.Base(file), line, exp, exp, act, act)
  36. t.FailNow()
  37. }
  38. }
  39. // isNil reports a failure if the given value is not nil. Note that values
  40. // which cannot be nil will always fail this check.
  41. func isNil(obtained interface{}, t testing.TB) {
  42. isNilUp(obtained, t, 1)
  43. }
  44. // isNilUp is like isNil, but used inside helper functions, to ensure that the
  45. // file and line number reported by failures corresponds to one or more levels
  46. // up the stack.
  47. func isNilUp(obtained interface{}, t testing.TB, caller int) {
  48. if !_isNil(obtained) {
  49. _, file, line, _ := runtime.Caller(caller + 1)
  50. fmt.Printf("%s:%d: expected nil, got: %v\n", filepath.Base(file), line, obtained)
  51. t.FailNow()
  52. }
  53. }
  54. // notNil reports a failure if the given value is nil.
  55. func notNil(obtained interface{}, t testing.TB) {
  56. notNilUp(obtained, t, 1)
  57. }
  58. // notNilUp is like notNil, but used inside helper functions, to ensure that the
  59. // file and line number reported by failures corresponds to one or more levels
  60. // up the stack.
  61. func notNilUp(obtained interface{}, t testing.TB, caller int) {
  62. if _isNil(obtained) {
  63. _, file, line, _ := runtime.Caller(caller + 1)
  64. fmt.Printf("%s:%d: expected non-nil, got: %v\n", filepath.Base(file), line, obtained)
  65. t.FailNow()
  66. }
  67. }
  68. // _isNil is a helper function for isNil and notNil, and should not be used
  69. // directly.
  70. func _isNil(obtained interface{}) bool {
  71. if obtained == nil {
  72. return true
  73. }
  74. switch v := reflect.ValueOf(obtained); v.Kind() {
  75. case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  76. return v.IsNil()
  77. }
  78. return false
  79. }