go113_test.go 3.1 KB

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