errors_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. func TestStack(t *testing.T) {
  181. type fileline struct {
  182. file string
  183. line int
  184. }
  185. tests := []struct {
  186. err error
  187. want []fileline
  188. }{{
  189. New("ooh"), []fileline{
  190. {"github.com/pkg/errors/errors_test.go", 209},
  191. },
  192. }, {
  193. Wrap(New("ooh"), "ahh"), []fileline{
  194. {"github.com/pkg/errors/errors_test.go", 213}, // this is the stack of Wrap, not New
  195. },
  196. }, {
  197. Cause(Wrap(New("ooh"), "ahh")), []fileline{
  198. {"github.com/pkg/errors/errors_test.go", 217}, // this is the stack of New
  199. },
  200. }, {
  201. func() error { return New("ooh") }(), []fileline{
  202. {"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of New
  203. {"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of New's caller
  204. },
  205. }, {
  206. Cause(func() error {
  207. return func() error {
  208. return Errorf("hello %s", fmt.Sprintf("world"))
  209. }()
  210. }()), []fileline{
  211. {"github.com/pkg/errors/errors_test.go", 228}, // this is the stack of Errorf
  212. {"github.com/pkg/errors/errors_test.go", 229}, // this is the stack of Errorf's caller
  213. {"github.com/pkg/errors/errors_test.go", 230}, // this is the stack of Errorf's caller's caller
  214. },
  215. }}
  216. for _, tt := range tests {
  217. x, ok := tt.err.(interface {
  218. Stack() []uintptr
  219. })
  220. if !ok {
  221. t.Errorf("expected %#v to implement Stack()", tt.err)
  222. continue
  223. }
  224. st := x.Stack()
  225. for i, want := range tt.want {
  226. frame := Frame(st[i])
  227. file, line := fmt.Sprintf("%+s", frame), frame.line()
  228. if file != want.file || line != want.line {
  229. t.Errorf("frame %d: expected %s:%d, got %s:%d", i, want.file, want.line, file, line)
  230. }
  231. }
  232. }
  233. }
  234. // errors.New, etc values are not expected to be compared by value
  235. // but the change in errors#27 made them incomparable. Assert that
  236. // various kinds of errors have a functional equality operator, even
  237. // if the result of that equality is always false.
  238. func TestErrorEquality(t *testing.T) {
  239. tests := []struct {
  240. err1, err2 error
  241. }{
  242. {io.EOF, io.EOF},
  243. {io.EOF, nil},
  244. {io.EOF, errors.New("EOF")},
  245. {io.EOF, New("EOF")},
  246. {New("EOF"), New("EOF")},
  247. {New("EOF"), Errorf("EOF")},
  248. {New("EOF"), Wrap(io.EOF, "EOF")},
  249. }
  250. for _, tt := range tests {
  251. _ = tt.err1 == tt.err2 // mustn't panic
  252. }
  253. }