testing_go19.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. Fatal(args ...interface{})
  19. Fatalf(format string, args ...interface{})
  20. Fail()
  21. FailNow()
  22. Failed() bool
  23. Helper()
  24. Log(args ...interface{})
  25. Logf(format string, args ...interface{})
  26. }
  27. // RuntimeT implements T and can be instantiated and run at runtime to
  28. // mimic *testing.T behavior. Unlike *testing.T, this will simply panic
  29. // for calls to Fatal. For calls to Error, you'll have to check the errors
  30. // list to determine whether to exit yourself.
  31. type RuntimeT struct {
  32. failed bool
  33. }
  34. func (t *RuntimeT) Error(args ...interface{}) {
  35. log.Println(fmt.Sprintln(args...))
  36. t.Fail()
  37. }
  38. func (t *RuntimeT) Errorf(format string, args ...interface{}) {
  39. log.Println(fmt.Sprintf(format, args...))
  40. t.Fail()
  41. }
  42. func (t *RuntimeT) Fatal(args ...interface{}) {
  43. log.Println(fmt.Sprintln(args...))
  44. t.FailNow()
  45. }
  46. func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
  47. log.Println(fmt.Sprintf(format, args...))
  48. t.FailNow()
  49. }
  50. func (t *RuntimeT) Fail() {
  51. t.failed = true
  52. }
  53. func (t *RuntimeT) FailNow() {
  54. panic("testing.T failed, see logs for output (if any)")
  55. }
  56. func (t *RuntimeT) Failed() bool {
  57. return t.failed
  58. }
  59. func (t *RuntimeT) Helper() {}
  60. func (t *RuntimeT) Log(args ...interface{}) {
  61. log.Println(fmt.Sprintln(args...))
  62. }
  63. func (t *RuntimeT) Logf(format string, args ...interface{}) {
  64. log.Println(fmt.Sprintf(format, args...))
  65. }