go113_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // +build go1.13
  2. package errors
  3. import (
  4. stderrors "errors"
  5. "fmt"
  6. "reflect"
  7. "testing"
  8. )
  9. func TestCauseErrorChainCompat(t *testing.T) {
  10. err := stderrors.New("the cause!")
  11. // Wrap error using the standard library
  12. wrapped := fmt.Errorf("wrapped with stdlib: %w", err)
  13. if Cause(wrapped) != err {
  14. t.Errorf("Cause does not support Go 1.13 error chains")
  15. }
  16. // Wrap in another layer using pkg/errors
  17. wrapped = WithMessage(wrapped, "wrapped with pkg/errors")
  18. if Cause(wrapped) != err {
  19. t.Errorf("Cause does not support Go 1.13 error chains")
  20. }
  21. // Wrap in another layer using the standard library
  22. wrapped = fmt.Errorf("wrapped with stdlib: %w", wrapped)
  23. if Cause(wrapped) != err {
  24. t.Errorf("Cause does not support Go 1.13 error chains")
  25. }
  26. }
  27. func TestWrapErrorChainCompat(t *testing.T) {
  28. err := stderrors.New("error that gets wrapped")
  29. wrapped := Wrap(err, "wrapped up")
  30. if !stderrors.Is(wrapped, err) {
  31. t.Errorf("Wrap does not support Go 1.13 error chains")
  32. }
  33. }
  34. func TestIs(t *testing.T) {
  35. err := New("test")
  36. type args struct {
  37. err error
  38. target error
  39. }
  40. tests := []struct {
  41. name string
  42. args args
  43. want bool
  44. }{
  45. {
  46. name: "with stack",
  47. args: args{
  48. err: WithStack(err),
  49. target: err,
  50. },
  51. want: true,
  52. },
  53. {
  54. name: "with message",
  55. args: args{
  56. err: WithMessage(err, "test"),
  57. target: err,
  58. },
  59. want: true,
  60. },
  61. {
  62. name: "with message format",
  63. args: args{
  64. err: WithMessagef(err, "%s", "test"),
  65. target: err,
  66. },
  67. want: true,
  68. },
  69. {
  70. name: "std errors compatibility",
  71. args: args{
  72. err: fmt.Errorf("wrap it: %w", err),
  73. target: err,
  74. },
  75. want: true,
  76. },
  77. }
  78. for _, tt := range tests {
  79. t.Run(tt.name, func(t *testing.T) {
  80. if got := Is(tt.args.err, tt.args.target); got != tt.want {
  81. t.Errorf("Is() = %v, want %v", got, tt.want)
  82. }
  83. })
  84. }
  85. }
  86. type customErr struct {
  87. msg string
  88. }
  89. func (c customErr) Error() string { return c.msg }
  90. func TestAs(t *testing.T) {
  91. var err = customErr{msg: "test message"}
  92. type args struct {
  93. err error
  94. target interface{}
  95. }
  96. tests := []struct {
  97. name string
  98. args args
  99. want bool
  100. }{
  101. {
  102. name: "with stack",
  103. args: args{
  104. err: WithStack(err),
  105. target: new(customErr),
  106. },
  107. want: true,
  108. },
  109. {
  110. name: "with message",
  111. args: args{
  112. err: WithMessage(err, "test"),
  113. target: new(customErr),
  114. },
  115. want: true,
  116. },
  117. {
  118. name: "with message format",
  119. args: args{
  120. err: WithMessagef(err, "%s", "test"),
  121. target: new(customErr),
  122. },
  123. want: true,
  124. },
  125. {
  126. name: "std errors compatibility",
  127. args: args{
  128. err: fmt.Errorf("wrap it: %w", err),
  129. target: new(customErr),
  130. },
  131. want: true,
  132. },
  133. }
  134. for _, tt := range tests {
  135. t.Run(tt.name, func(t *testing.T) {
  136. if got := As(tt.args.err, tt.args.target); got != tt.want {
  137. t.Errorf("As() = %v, want %v", got, tt.want)
  138. }
  139. ce := tt.args.target.(*customErr)
  140. if !reflect.DeepEqual(err, *ce) {
  141. t.Errorf("set target error failed, target error is %v", *ce)
  142. }
  143. })
  144. }
  145. }
  146. func TestUnwrap(t *testing.T) {
  147. err := New("test")
  148. type args struct {
  149. err error
  150. }
  151. tests := []struct {
  152. name string
  153. args args
  154. want error
  155. }{
  156. {
  157. name: "with stack",
  158. args: args{err: WithStack(err)},
  159. want: err,
  160. },
  161. {
  162. name: "with message",
  163. args: args{err: WithMessage(err, "test")},
  164. want: err,
  165. },
  166. {
  167. name: "with message format",
  168. args: args{err: WithMessagef(err, "%s", "test")},
  169. want: err,
  170. },
  171. {
  172. name: "std errors compatibility",
  173. args: args{err: fmt.Errorf("wrap: %w", err)},
  174. want: err,
  175. },
  176. }
  177. for _, tt := range tests {
  178. t.Run(tt.name, func(t *testing.T) {
  179. if err := Unwrap(tt.args.err); !reflect.DeepEqual(err, tt.want) {
  180. t.Errorf("Unwrap() error = %v, want %v", err, tt.want)
  181. }
  182. })
  183. }
  184. }