go113_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2019 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. // +build go1.13
  21. package multierr_test
  22. import (
  23. "errors"
  24. "os"
  25. "testing"
  26. "github.com/stretchr/testify/assert"
  27. "github.com/stretchr/testify/require"
  28. "go.uber.org/multierr"
  29. )
  30. type errGreatSadness struct{ id int }
  31. func (errGreatSadness) Error() string {
  32. return "great sadness"
  33. }
  34. type errUnprecedentedFailure struct{ id int }
  35. func (errUnprecedentedFailure) Error() string {
  36. return "unprecedented failure"
  37. }
  38. func (e errUnprecedentedFailure) Unwrap() error {
  39. return errRootCause{e.id}
  40. }
  41. type errRootCause struct{ i int }
  42. func (errRootCause) Error() string {
  43. return "root cause"
  44. }
  45. func TestErrorsWrapping(t *testing.T) {
  46. err := multierr.Append(
  47. errGreatSadness{42},
  48. errUnprecedentedFailure{43},
  49. )
  50. t.Run("left", func(t *testing.T) {
  51. t.Run("As", func(t *testing.T) {
  52. var got errGreatSadness
  53. require.True(t, errors.As(err, &got))
  54. assert.Equal(t, 42, got.id)
  55. })
  56. t.Run("Is", func(t *testing.T) {
  57. assert.False(t, errors.Is(err, errGreatSadness{41}))
  58. assert.True(t, errors.Is(err, errGreatSadness{42}))
  59. })
  60. })
  61. t.Run("right", func(t *testing.T) {
  62. t.Run("As", func(t *testing.T) {
  63. var got errUnprecedentedFailure
  64. require.True(t, errors.As(err, &got))
  65. assert.Equal(t, 43, got.id)
  66. })
  67. t.Run("Is", func(t *testing.T) {
  68. assert.False(t, errors.Is(err, errUnprecedentedFailure{42}))
  69. assert.True(t, errors.Is(err, errUnprecedentedFailure{43}))
  70. })
  71. })
  72. t.Run("top-level", func(t *testing.T) {
  73. t.Run("As", func(t *testing.T) {
  74. var got interface{ Errors() []error }
  75. require.True(t, errors.As(err, &got))
  76. assert.Len(t, got.Errors(), 2)
  77. })
  78. t.Run("Is", func(t *testing.T) {
  79. assert.True(t, errors.Is(err, err))
  80. })
  81. })
  82. t.Run("root cause", func(t *testing.T) {
  83. t.Run("As", func(t *testing.T) {
  84. var got errRootCause
  85. require.True(t, errors.As(err, &got))
  86. assert.Equal(t, 43, got.i)
  87. })
  88. t.Run("Is", func(t *testing.T) {
  89. assert.False(t, errors.Is(err, errRootCause{42}))
  90. assert.True(t, errors.Is(err, errRootCause{43}))
  91. })
  92. })
  93. t.Run("mismatch", func(t *testing.T) {
  94. t.Run("As", func(t *testing.T) {
  95. var got *os.PathError
  96. assert.False(t, errors.As(err, &got))
  97. })
  98. t.Run("Is", func(t *testing.T) {
  99. assert.False(t, errors.Is(err, errors.New("great sadness")))
  100. })
  101. })
  102. }
  103. func TestErrorsWrappingSameType(t *testing.T) {
  104. err := multierr.Combine(
  105. errGreatSadness{1},
  106. errGreatSadness{2},
  107. errGreatSadness{3},
  108. )
  109. t.Run("As returns first", func(t *testing.T) {
  110. var got errGreatSadness
  111. require.True(t, errors.As(err, &got))
  112. assert.Equal(t, 1, got.id)
  113. })
  114. t.Run("Is matches all", func(t *testing.T) {
  115. assert.True(t, errors.Is(err, errGreatSadness{1}))
  116. assert.True(t, errors.Is(err, errGreatSadness{2}))
  117. assert.True(t, errors.Is(err, errGreatSadness{3}))
  118. })
  119. }