errors_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package errors
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestNew(t *testing.T) {
  11. tests := []struct {
  12. err string
  13. want error
  14. }{
  15. {"", fmt.Errorf("")},
  16. {"foo", fmt.Errorf("foo")},
  17. {"foo", New("foo")},
  18. {"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
  19. }
  20. for _, tt := range tests {
  21. got := New(tt.err)
  22. if got.Error() != tt.want.Error() {
  23. t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
  24. }
  25. }
  26. }
  27. func TestWrapNil(t *testing.T) {
  28. got := Wrap(nil, "no error")
  29. if got != nil {
  30. t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)
  31. }
  32. }
  33. func TestWrap(t *testing.T) {
  34. tests := []struct {
  35. err error
  36. message string
  37. want string
  38. }{
  39. {io.EOF, "read error", "read error: EOF"},
  40. {Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},
  41. }
  42. for _, tt := range tests {
  43. got := Wrap(tt.err, tt.message).Error()
  44. if got != tt.want {
  45. t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
  46. }
  47. }
  48. }
  49. type nilError struct{}
  50. func (nilError) Error() string { return "nil error" }
  51. type causeError struct {
  52. cause error
  53. }
  54. func (e *causeError) Error() string { return "cause error" }
  55. func (e *causeError) Cause() error { return e.cause }
  56. func TestCause(t *testing.T) {
  57. x := New("error")
  58. tests := []struct {
  59. err error
  60. want error
  61. }{{
  62. // nil error is nil
  63. err: nil,
  64. want: nil,
  65. }, {
  66. // explicit nil error is nil
  67. err: (error)(nil),
  68. want: nil,
  69. }, {
  70. // typed nil is nil
  71. err: (*nilError)(nil),
  72. want: (*nilError)(nil),
  73. }, {
  74. // uncaused error is unaffected
  75. err: io.EOF,
  76. want: io.EOF,
  77. }, {
  78. // caused error returns cause
  79. err: &causeError{cause: io.EOF},
  80. want: io.EOF,
  81. }, {
  82. err: x, // return from errors.New
  83. want: x,
  84. }}
  85. for i, tt := range tests {
  86. got := Cause(tt.err)
  87. if !reflect.DeepEqual(got, tt.want) {
  88. t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
  89. }
  90. }
  91. }
  92. func TestFprintError(t *testing.T) {
  93. x := New("error")
  94. tests := []struct {
  95. err error
  96. want string
  97. }{{
  98. // nil error is nil
  99. err: nil,
  100. }, {
  101. // explicit nil error is nil
  102. err: (error)(nil),
  103. }, {
  104. // uncaused error is unaffected
  105. err: io.EOF,
  106. want: "EOF\n",
  107. }, {
  108. // caused error returns cause
  109. err: &causeError{cause: io.EOF},
  110. want: "EOF\n" +
  111. "cause error\n",
  112. }, {
  113. err: x, // return from errors.New
  114. want: "github.com/pkg/errors/errors_test.go:106: error\n",
  115. }, {
  116. err: Wrap(x, "message"),
  117. want: "github.com/pkg/errors/errors_test.go:106: error\n" +
  118. "github.com/pkg/errors/errors_test.go:129: message\n",
  119. }, {
  120. err: Wrap(io.EOF, "message"),
  121. want: "EOF\n" +
  122. "github.com/pkg/errors/errors_test.go:133: message\n",
  123. }, {
  124. err: Wrap(Wrap(x, "message"), "another message"),
  125. want: "github.com/pkg/errors/errors_test.go:106: error\n" +
  126. "github.com/pkg/errors/errors_test.go:137: message\n" +
  127. "github.com/pkg/errors/errors_test.go:137: another message\n",
  128. }, {
  129. err: Wrapf(x, "message"),
  130. want: "github.com/pkg/errors/errors_test.go:106: error\n" +
  131. "github.com/pkg/errors/errors_test.go:142: message\n",
  132. }}
  133. for i, tt := range tests {
  134. var w bytes.Buffer
  135. Fprint(&w, tt.err)
  136. got := w.String()
  137. if got != tt.want {
  138. t.Errorf("test %d: Fprint(w, %q): got %q, want %q", i+1, tt.err, got, tt.want)
  139. }
  140. }
  141. }
  142. func TestWrapfNil(t *testing.T) {
  143. got := Wrapf(nil, "no error")
  144. if got != nil {
  145. t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got)
  146. }
  147. }
  148. func TestWrapf(t *testing.T) {
  149. tests := []struct {
  150. err error
  151. message string
  152. want string
  153. }{
  154. {io.EOF, "read error", "read error: EOF"},
  155. {Wrapf(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"},
  156. {Wrapf(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},
  157. }
  158. for _, tt := range tests {
  159. got := Wrapf(tt.err, tt.message).Error()
  160. if got != tt.want {
  161. t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
  162. }
  163. }
  164. }
  165. func TestErrorf(t *testing.T) {
  166. tests := []struct {
  167. err error
  168. want string
  169. }{
  170. {Errorf("read error without format specifiers"), "read error without format specifiers"},
  171. {Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},
  172. }
  173. for _, tt := range tests {
  174. got := tt.err.Error()
  175. if got != tt.want {
  176. t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want)
  177. }
  178. }
  179. }
  180. // errors.New, etc values are not expected to be compared by value
  181. // but the change in errors#27 made them incomparable. Assert that
  182. // various kinds of errors have a functional equality operator, even
  183. // if the result of that equality is always false.
  184. func TestErrorEquality(t *testing.T) {
  185. tests := []struct {
  186. err1, err2 error
  187. }{
  188. {io.EOF, io.EOF},
  189. {io.EOF, nil},
  190. {io.EOF, errors.New("EOF")},
  191. {io.EOF, New("EOF")},
  192. {New("EOF"), New("EOF")},
  193. {New("EOF"), Errorf("EOF")},
  194. {New("EOF"), Wrap(io.EOF, "EOF")},
  195. }
  196. for _, tt := range tests {
  197. _ = tt.err1 == tt.err2 // mustn't panic
  198. }
  199. }