testing_go19.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // +build go1.9
  2. // NOTE: This is a temporary copy of testing.go for Go 1.9 with the addition
  3. // of "Helper" to the T interface. Go 1.9 at the time of typing is in RC
  4. // and is set for release shortly. We'll support this on master as the default
  5. // as soon as 1.9 is released.
  6. package testing
  7. import (
  8. "fmt"
  9. "log"
  10. )
  11. // T is the interface that mimics the standard library *testing.T.
  12. //
  13. // In unit tests you can just pass a *testing.T struct. At runtime, outside
  14. // of tests, you can pass in a RuntimeT struct from this package.
  15. type T interface {
  16. Error(args ...interface{})
  17. Errorf(format string, args ...interface{})
  18. Fail()
  19. FailNow()
  20. Failed() bool
  21. Fatal(args ...interface{})
  22. Fatalf(format string, args ...interface{})
  23. Log(args ...interface{})
  24. Logf(format string, args ...interface{})
  25. Name() string
  26. Skip(args ...interface{})
  27. SkipNow()
  28. Skipf(format string, args ...interface{})
  29. Skipped() bool
  30. Helper()
  31. }
  32. // RuntimeT implements T and can be instantiated and run at runtime to
  33. // mimic *testing.T behavior. Unlike *testing.T, this will simply panic
  34. // for calls to Fatal. For calls to Error, you'll have to check the errors
  35. // list to determine whether to exit yourself.
  36. type RuntimeT struct {
  37. skipped bool
  38. failed bool
  39. }
  40. func (t *RuntimeT) Error(args ...interface{}) {
  41. log.Println(fmt.Sprintln(args...))
  42. t.Fail()
  43. }
  44. func (t *RuntimeT) Errorf(format string, args ...interface{}) {
  45. log.Printf(format, args...)
  46. t.Fail()
  47. }
  48. func (t *RuntimeT) Fail() {
  49. t.failed = true
  50. }
  51. func (t *RuntimeT) FailNow() {
  52. panic("testing.T failed, see logs for output (if any)")
  53. }
  54. func (t *RuntimeT) Failed() bool {
  55. return t.failed
  56. }
  57. func (t *RuntimeT) Fatal(args ...interface{}) {
  58. log.Print(args...)
  59. t.FailNow()
  60. }
  61. func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
  62. log.Printf(format, args...)
  63. t.FailNow()
  64. }
  65. func (t *RuntimeT) Log(args ...interface{}) {
  66. log.Println(fmt.Sprintln(args...))
  67. }
  68. func (t *RuntimeT) Logf(format string, args ...interface{}) {
  69. log.Println(fmt.Sprintf(format, args...))
  70. }
  71. func (t *RuntimeT) Name() string {
  72. return ""
  73. }
  74. func (t *RuntimeT) Skip(args ...interface{}) {
  75. log.Print(args...)
  76. t.SkipNow()
  77. }
  78. func (t *RuntimeT) SkipNow() {
  79. t.skipped = true
  80. }
  81. func (t *RuntimeT) Skipf(format string, args ...interface{}) {
  82. log.Printf(format, args...)
  83. t.SkipNow()
  84. }
  85. func (t *RuntimeT) Skipped() bool {
  86. return t.skipped
  87. }
  88. func (t *RuntimeT) Helper() {}