errors_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package errors
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "testing"
  8. )
  9. func TestNew(t *testing.T) {
  10. tests := []struct {
  11. err string
  12. want error
  13. }{
  14. {"", fmt.Errorf("")},
  15. {"foo", fmt.Errorf("foo")},
  16. {"foo", New("foo")},
  17. }
  18. for _, tt := range tests {
  19. got := New(tt.err)
  20. if got.Error() != tt.want.Error() {
  21. t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
  22. }
  23. }
  24. }
  25. func TestWrapNil(t *testing.T) {
  26. got := Wrap(nil, "no error")
  27. if got != nil {
  28. t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)
  29. }
  30. }
  31. func TestWrap(t *testing.T) {
  32. tests := []struct {
  33. err error
  34. message string
  35. want string
  36. }{
  37. {io.EOF, "read error", "read error: EOF"},
  38. {Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},
  39. }
  40. for _, tt := range tests {
  41. got := Wrap(tt.err, tt.message).Error()
  42. if got != tt.want {
  43. t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
  44. }
  45. }
  46. }
  47. type nilError struct{}
  48. func (nilError) Error() string { return "nil error" }
  49. type causeError struct {
  50. cause error
  51. }
  52. func (e *causeError) Error() string { return "cause error" }
  53. func (e *causeError) Cause() error { return e.cause }
  54. func TestCause(t *testing.T) {
  55. x := New("error")
  56. tests := []struct {
  57. err error
  58. want error
  59. }{{
  60. // nil error is nil
  61. err: nil,
  62. want: nil,
  63. }, {
  64. // explicit nil error is nil
  65. err: (error)(nil),
  66. want: nil,
  67. }, {
  68. // typed nil is nil
  69. err: (*nilError)(nil),
  70. want: (*nilError)(nil),
  71. }, {
  72. // uncaused error is unaffected
  73. err: io.EOF,
  74. want: io.EOF,
  75. }, {
  76. // caused error returns cause
  77. err: &causeError{cause: io.EOF},
  78. want: io.EOF,
  79. }, {
  80. err: x, // return from errors.New
  81. want: x,
  82. }}
  83. for i, tt := range tests {
  84. got := Cause(tt.err)
  85. if !reflect.DeepEqual(got, tt.want) {
  86. t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
  87. }
  88. }
  89. }
  90. func TestFprint(t *testing.T) {
  91. x := New("error")
  92. tests := []struct {
  93. err error
  94. want string
  95. }{{
  96. // nil error is nil
  97. err: nil,
  98. }, {
  99. // explicit nil error is nil
  100. err: (error)(nil),
  101. }, {
  102. // uncaused error is unaffected
  103. err: io.EOF,
  104. want: "EOF\n",
  105. }, {
  106. // caused error returns cause
  107. err: &causeError{cause: io.EOF},
  108. want: "cause error\nEOF\n",
  109. }, {
  110. err: x, // return from errors.New
  111. want: "github.com/pkg/errors/errors_test.go:104: error\n",
  112. }, {
  113. err: Wrap(x, "message"),
  114. want: "github.com/pkg/errors/errors_test.go:126: message\ngithub.com/pkg/errors/errors_test.go:104: error\n",
  115. }, {
  116. err: Wrap(Wrap(x, "message"), "another message"),
  117. want: "github.com/pkg/errors/errors_test.go:129: another message\ngithub.com/pkg/errors/errors_test.go:129: message\ngithub.com/pkg/errors/errors_test.go:104: error\n",
  118. }}
  119. for i, tt := range tests {
  120. var w bytes.Buffer
  121. Fprint(&w, tt.err)
  122. got := w.String()
  123. if got != tt.want {
  124. t.Errorf("test %d: Fprint(w, %q): got %q, want %q", i+1, tt.err, got, tt.want)
  125. }
  126. }
  127. }