errors_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package errors
  2. import (
  3. "errors"
  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. {"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
  18. }
  19. for _, tt := range tests {
  20. got := New(tt.err)
  21. if got.Error() != tt.want.Error() {
  22. t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
  23. }
  24. }
  25. }
  26. func TestWrapNil(t *testing.T) {
  27. got := Wrap(nil, "no error")
  28. if got != nil {
  29. t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)
  30. }
  31. }
  32. func TestWrap(t *testing.T) {
  33. tests := []struct {
  34. err error
  35. message string
  36. want string
  37. }{
  38. {io.EOF, "read error", "read error: EOF"},
  39. {Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},
  40. }
  41. for _, tt := range tests {
  42. got := Wrap(tt.err, tt.message).Error()
  43. if got != tt.want {
  44. t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
  45. }
  46. }
  47. }
  48. type nilError struct{}
  49. func (nilError) Error() string { return "nil error" }
  50. func TestCause(t *testing.T) {
  51. x := New("error")
  52. tests := []struct {
  53. err error
  54. want error
  55. }{{
  56. // nil error is nil
  57. err: nil,
  58. want: nil,
  59. }, {
  60. // explicit nil error is nil
  61. err: (error)(nil),
  62. want: nil,
  63. }, {
  64. // typed nil is nil
  65. err: (*nilError)(nil),
  66. want: (*nilError)(nil),
  67. }, {
  68. // uncaused error is unaffected
  69. err: io.EOF,
  70. want: io.EOF,
  71. }, {
  72. // caused error returns cause
  73. err: Wrap(io.EOF, "ignored"),
  74. want: io.EOF,
  75. }, {
  76. err: x, // return from errors.New
  77. want: x,
  78. }}
  79. for i, tt := range tests {
  80. got := Cause(tt.err)
  81. if !reflect.DeepEqual(got, tt.want) {
  82. t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
  83. }
  84. }
  85. }
  86. func TestWrapfNil(t *testing.T) {
  87. got := Wrapf(nil, "no error")
  88. if got != nil {
  89. t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got)
  90. }
  91. }
  92. func TestWrapf(t *testing.T) {
  93. tests := []struct {
  94. err error
  95. message string
  96. want string
  97. }{
  98. {io.EOF, "read error", "read error: EOF"},
  99. {Wrapf(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"},
  100. {Wrapf(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},
  101. }
  102. for _, tt := range tests {
  103. got := Wrapf(tt.err, tt.message).Error()
  104. if got != tt.want {
  105. t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
  106. }
  107. }
  108. }
  109. func TestErrorf(t *testing.T) {
  110. tests := []struct {
  111. err error
  112. want string
  113. }{
  114. {Errorf("read error without format specifiers"), "read error without format specifiers"},
  115. {Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},
  116. }
  117. for _, tt := range tests {
  118. got := tt.err.Error()
  119. if got != tt.want {
  120. t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want)
  121. }
  122. }
  123. }
  124. // errors.New, etc values are not expected to be compared by value
  125. // but the change in errors#27 made them incomparable. Assert that
  126. // various kinds of errors have a functional equality operator, even
  127. // if the result of that equality is always false.
  128. func TestErrorEquality(t *testing.T) {
  129. tests := []struct {
  130. err1, err2 error
  131. }{
  132. {io.EOF, io.EOF},
  133. {io.EOF, nil},
  134. {io.EOF, errors.New("EOF")},
  135. {io.EOF, New("EOF")},
  136. {New("EOF"), New("EOF")},
  137. {New("EOF"), Errorf("EOF")},
  138. {New("EOF"), Wrap(io.EOF, "EOF")},
  139. }
  140. for _, tt := range tests {
  141. _ = tt.err1 == tt.err2 // mustn't panic
  142. }
  143. }