errors_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 TestFprint(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: "cause error\nEOF\n",
  111. }, {
  112. err: x, // return from errors.New
  113. want: "github.com/pkg/errors/errors_test.go:106: error\n",
  114. }, {
  115. err: Wrap(x, "message"),
  116. want: "github.com/pkg/errors/errors_test.go:128: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
  117. }, {
  118. err: Wrap(Wrap(x, "message"), "another message"),
  119. want: "github.com/pkg/errors/errors_test.go:131: another message\ngithub.com/pkg/errors/errors_test.go:131: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
  120. }}
  121. for i, tt := range tests {
  122. var w bytes.Buffer
  123. Fprint(&w, tt.err)
  124. got := w.String()
  125. if got != tt.want {
  126. t.Errorf("test %d: Fprint(w, %q): got %q, want %q", i+1, tt.err, got, tt.want)
  127. }
  128. }
  129. }