helpers_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package errdefs
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. )
  7. var errTest = errors.New("this is a test")
  8. type causal interface {
  9. Cause() error
  10. }
  11. func TestNotFound(t *testing.T) {
  12. if IsNotFound(errTest) {
  13. t.Fatalf("did not expect not found error, got %T", errTest)
  14. }
  15. e := NotFound(errTest)
  16. if !IsNotFound(e) {
  17. t.Fatalf("expected not found error, got: %T", e)
  18. }
  19. if cause := e.(causal).Cause(); cause != errTest {
  20. t.Fatalf("causual should be errTest, got: %v", cause)
  21. }
  22. if !errors.Is(e, errTest) {
  23. t.Fatalf("expected not found error to match errTest")
  24. }
  25. wrapped := fmt.Errorf("foo: %w", e)
  26. if !IsNotFound(wrapped) {
  27. t.Fatalf("expected not found error, got: %T", wrapped)
  28. }
  29. }
  30. func TestConflict(t *testing.T) {
  31. if IsConflict(errTest) {
  32. t.Fatalf("did not expect conflict error, got %T", errTest)
  33. }
  34. e := Conflict(errTest)
  35. if !IsConflict(e) {
  36. t.Fatalf("expected conflict error, got: %T", e)
  37. }
  38. if cause := e.(causal).Cause(); cause != errTest {
  39. t.Fatalf("causual should be errTest, got: %v", cause)
  40. }
  41. if !errors.Is(e, errTest) {
  42. t.Fatalf("expected conflict error to match errTest")
  43. }
  44. wrapped := fmt.Errorf("foo: %w", e)
  45. if !IsConflict(wrapped) {
  46. t.Fatalf("expected conflict error, got: %T", wrapped)
  47. }
  48. }
  49. func TestForbidden(t *testing.T) {
  50. if IsForbidden(errTest) {
  51. t.Fatalf("did not expect forbidden error, got %T", errTest)
  52. }
  53. e := Forbidden(errTest)
  54. if !IsForbidden(e) {
  55. t.Fatalf("expected forbidden error, got: %T", e)
  56. }
  57. if cause := e.(causal).Cause(); cause != errTest {
  58. t.Fatalf("causual should be errTest, got: %v", cause)
  59. }
  60. if !errors.Is(e, errTest) {
  61. t.Fatalf("expected forbidden error to match errTest")
  62. }
  63. wrapped := fmt.Errorf("foo: %w", e)
  64. if !IsForbidden(wrapped) {
  65. t.Fatalf("expected forbidden error, got: %T", wrapped)
  66. }
  67. }
  68. func TestInvalidParameter(t *testing.T) {
  69. if IsInvalidParameter(errTest) {
  70. t.Fatalf("did not expect invalid argument error, got %T", errTest)
  71. }
  72. e := InvalidParameter(errTest)
  73. if !IsInvalidParameter(e) {
  74. t.Fatalf("expected invalid argument error, got %T", e)
  75. }
  76. if cause := e.(causal).Cause(); cause != errTest {
  77. t.Fatalf("causual should be errTest, got: %v", cause)
  78. }
  79. if !errors.Is(e, errTest) {
  80. t.Fatalf("expected invalid argument error to match errTest")
  81. }
  82. wrapped := fmt.Errorf("foo: %w", e)
  83. if !IsInvalidParameter(wrapped) {
  84. t.Fatalf("expected invalid argument error, got: %T", wrapped)
  85. }
  86. }
  87. func TestNotImplemented(t *testing.T) {
  88. if IsNotImplemented(errTest) {
  89. t.Fatalf("did not expect not implemented error, got %T", errTest)
  90. }
  91. e := NotImplemented(errTest)
  92. if !IsNotImplemented(e) {
  93. t.Fatalf("expected not implemented error, got %T", e)
  94. }
  95. if cause := e.(causal).Cause(); cause != errTest {
  96. t.Fatalf("causual should be errTest, got: %v", cause)
  97. }
  98. if !errors.Is(e, errTest) {
  99. t.Fatalf("expected not implemented error to match errTest")
  100. }
  101. wrapped := fmt.Errorf("foo: %w", e)
  102. if !IsNotImplemented(wrapped) {
  103. t.Fatalf("expected not implemented error, got: %T", wrapped)
  104. }
  105. }
  106. func TestNotModified(t *testing.T) {
  107. if IsNotModified(errTest) {
  108. t.Fatalf("did not expect not modified error, got %T", errTest)
  109. }
  110. e := NotModified(errTest)
  111. if !IsNotModified(e) {
  112. t.Fatalf("expected not modified error, got %T", e)
  113. }
  114. if cause := e.(causal).Cause(); cause != errTest {
  115. t.Fatalf("causual should be errTest, got: %v", cause)
  116. }
  117. if !errors.Is(e, errTest) {
  118. t.Fatalf("expected not modified error to match errTest")
  119. }
  120. wrapped := fmt.Errorf("foo: %w", e)
  121. if !IsNotModified(wrapped) {
  122. t.Fatalf("expected not modified error, got: %T", wrapped)
  123. }
  124. }
  125. func TestUnauthorized(t *testing.T) {
  126. if IsUnauthorized(errTest) {
  127. t.Fatalf("did not expect unauthorized error, got %T", errTest)
  128. }
  129. e := Unauthorized(errTest)
  130. if !IsUnauthorized(e) {
  131. t.Fatalf("expected unauthorized error, got %T", e)
  132. }
  133. if cause := e.(causal).Cause(); cause != errTest {
  134. t.Fatalf("causual should be errTest, got: %v", cause)
  135. }
  136. if !errors.Is(e, errTest) {
  137. t.Fatalf("expected unauthorized error to match errTest")
  138. }
  139. wrapped := fmt.Errorf("foo: %w", e)
  140. if !IsUnauthorized(wrapped) {
  141. t.Fatalf("expected unauthorized error, got: %T", wrapped)
  142. }
  143. }
  144. func TestUnknown(t *testing.T) {
  145. if IsUnknown(errTest) {
  146. t.Fatalf("did not expect unknown error, got %T", errTest)
  147. }
  148. e := Unknown(errTest)
  149. if !IsUnknown(e) {
  150. t.Fatalf("expected unknown error, got %T", e)
  151. }
  152. if cause := e.(causal).Cause(); cause != errTest {
  153. t.Fatalf("causual should be errTest, got: %v", cause)
  154. }
  155. if !errors.Is(e, errTest) {
  156. t.Fatalf("expected unknown error to match errTest")
  157. }
  158. wrapped := fmt.Errorf("foo: %w", e)
  159. if !IsUnknown(wrapped) {
  160. t.Fatalf("expected unknown error, got: %T", wrapped)
  161. }
  162. }
  163. func TestCancelled(t *testing.T) {
  164. if IsCancelled(errTest) {
  165. t.Fatalf("did not expect cancelled error, got %T", errTest)
  166. }
  167. e := Cancelled(errTest)
  168. if !IsCancelled(e) {
  169. t.Fatalf("expected cancelled error, got %T", e)
  170. }
  171. if cause := e.(causal).Cause(); cause != errTest {
  172. t.Fatalf("causual should be errTest, got: %v", cause)
  173. }
  174. if !errors.Is(e, errTest) {
  175. t.Fatalf("expected cancelled error to match errTest")
  176. }
  177. wrapped := fmt.Errorf("foo: %w", e)
  178. if !IsCancelled(wrapped) {
  179. t.Fatalf("expected cancelled error, got: %T", wrapped)
  180. }
  181. }
  182. func TestDeadline(t *testing.T) {
  183. if IsDeadline(errTest) {
  184. t.Fatalf("did not expect deadline error, got %T", errTest)
  185. }
  186. e := Deadline(errTest)
  187. if !IsDeadline(e) {
  188. t.Fatalf("expected deadline error, got %T", e)
  189. }
  190. if cause := e.(causal).Cause(); cause != errTest {
  191. t.Fatalf("causual should be errTest, got: %v", cause)
  192. }
  193. if !errors.Is(e, errTest) {
  194. t.Fatalf("expected deadline error to match errTest")
  195. }
  196. wrapped := fmt.Errorf("foo: %w", e)
  197. if !IsDeadline(wrapped) {
  198. t.Fatalf("expected deadline error, got: %T", wrapped)
  199. }
  200. }
  201. func TestDataLoss(t *testing.T) {
  202. if IsDataLoss(errTest) {
  203. t.Fatalf("did not expect data loss error, got %T", errTest)
  204. }
  205. e := DataLoss(errTest)
  206. if !IsDataLoss(e) {
  207. t.Fatalf("expected data loss error, got %T", e)
  208. }
  209. if cause := e.(causal).Cause(); cause != errTest {
  210. t.Fatalf("causual should be errTest, got: %v", cause)
  211. }
  212. if !errors.Is(e, errTest) {
  213. t.Fatalf("expected data loss error to match errTest")
  214. }
  215. wrapped := fmt.Errorf("foo: %w", e)
  216. if !IsDataLoss(wrapped) {
  217. t.Fatalf("expected data loss error, got: %T", wrapped)
  218. }
  219. }
  220. func TestUnavailable(t *testing.T) {
  221. if IsUnavailable(errTest) {
  222. t.Fatalf("did not expect unavaillable error, got %T", errTest)
  223. }
  224. e := Unavailable(errTest)
  225. if !IsUnavailable(e) {
  226. t.Fatalf("expected unavaillable error, got %T", e)
  227. }
  228. if cause := e.(causal).Cause(); cause != errTest {
  229. t.Fatalf("causual should be errTest, got: %v", cause)
  230. }
  231. if !errors.Is(e, errTest) {
  232. t.Fatalf("expected unavaillable error to match errTest")
  233. }
  234. wrapped := fmt.Errorf("foo: %w", e)
  235. if !IsUnavailable(wrapped) {
  236. t.Fatalf("expected unavaillable error, got: %T", wrapped)
  237. }
  238. }
  239. func TestSystem(t *testing.T) {
  240. if IsSystem(errTest) {
  241. t.Fatalf("did not expect system error, got %T", errTest)
  242. }
  243. e := System(errTest)
  244. if !IsSystem(e) {
  245. t.Fatalf("expected system error, got %T", e)
  246. }
  247. if cause := e.(causal).Cause(); cause != errTest {
  248. t.Fatalf("causual should be errTest, got: %v", cause)
  249. }
  250. if !errors.Is(e, errTest) {
  251. t.Fatalf("expected system error to match errTest")
  252. }
  253. wrapped := fmt.Errorf("foo: %w", e)
  254. if !IsSystem(wrapped) {
  255. t.Fatalf("expected system error, got: %T", wrapped)
  256. }
  257. }