...

Source file src/go.uber.org/multierr/error_ext_test.go

Documentation: go.uber.org/multierr

     1  // Copyright (c) 2020 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  
    21  package multierr_test
    22  
    23  import (
    24  	"errors"
    25  	"os"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  	"go.uber.org/multierr"
    31  )
    32  
    33  type errGreatSadness struct{ id int }
    34  
    35  func (errGreatSadness) Error() string {
    36  	return "great sadness"
    37  }
    38  
    39  type errUnprecedentedFailure struct{ id int }
    40  
    41  func (errUnprecedentedFailure) Error() string {
    42  	return "unprecedented failure"
    43  }
    44  
    45  func (e errUnprecedentedFailure) Unwrap() error {
    46  	return errRootCause{e.id}
    47  }
    48  
    49  type errRootCause struct{ i int }
    50  
    51  func (errRootCause) Error() string {
    52  	return "root cause"
    53  }
    54  
    55  func TestErrorsWrapping(t *testing.T) {
    56  	err := multierr.Append(
    57  		errGreatSadness{42},
    58  		errUnprecedentedFailure{43},
    59  	)
    60  
    61  	t.Run("left", func(t *testing.T) {
    62  		t.Run("As", func(t *testing.T) {
    63  			var got errGreatSadness
    64  			require.True(t, errors.As(err, &got))
    65  			assert.Equal(t, 42, got.id)
    66  		})
    67  
    68  		t.Run("Is", func(t *testing.T) {
    69  			assert.False(t, errors.Is(err, errGreatSadness{41}))
    70  			assert.True(t, errors.Is(err, errGreatSadness{42}))
    71  		})
    72  	})
    73  
    74  	t.Run("right", func(t *testing.T) {
    75  		t.Run("As", func(t *testing.T) {
    76  			var got errUnprecedentedFailure
    77  			require.True(t, errors.As(err, &got))
    78  			assert.Equal(t, 43, got.id)
    79  		})
    80  
    81  		t.Run("Is", func(t *testing.T) {
    82  			assert.False(t, errors.Is(err, errUnprecedentedFailure{42}))
    83  			assert.True(t, errors.Is(err, errUnprecedentedFailure{43}))
    84  		})
    85  	})
    86  
    87  	t.Run("top-level", func(t *testing.T) {
    88  		t.Run("As", func(t *testing.T) {
    89  			var got interface{ Errors() []error }
    90  			require.True(t, errors.As(err, &got))
    91  			assert.Len(t, got.Errors(), 2)
    92  		})
    93  
    94  		t.Run("Is", func(t *testing.T) {
    95  			assert.True(t, errors.Is(err, err))
    96  		})
    97  	})
    98  
    99  	t.Run("root cause", func(t *testing.T) {
   100  		t.Run("As", func(t *testing.T) {
   101  			var got errRootCause
   102  			require.True(t, errors.As(err, &got))
   103  			assert.Equal(t, 43, got.i)
   104  		})
   105  
   106  		t.Run("Is", func(t *testing.T) {
   107  			assert.False(t, errors.Is(err, errRootCause{42}))
   108  			assert.True(t, errors.Is(err, errRootCause{43}))
   109  		})
   110  	})
   111  
   112  	t.Run("mismatch", func(t *testing.T) {
   113  		t.Run("As", func(t *testing.T) {
   114  			var got *os.PathError
   115  			assert.False(t, errors.As(err, &got))
   116  		})
   117  
   118  		t.Run("Is", func(t *testing.T) {
   119  			assert.False(t, errors.Is(err, errors.New("great sadness")))
   120  		})
   121  	})
   122  }
   123  
   124  func TestErrorsWrappingSameType(t *testing.T) {
   125  	err := multierr.Combine(
   126  		errGreatSadness{1},
   127  		errGreatSadness{2},
   128  		errGreatSadness{3},
   129  	)
   130  
   131  	t.Run("As returns first", func(t *testing.T) {
   132  		var got errGreatSadness
   133  		require.True(t, errors.As(err, &got))
   134  		assert.Equal(t, 1, got.id)
   135  	})
   136  
   137  	t.Run("Is matches all", func(t *testing.T) {
   138  		assert.True(t, errors.Is(err, errGreatSadness{1}))
   139  		assert.True(t, errors.Is(err, errGreatSadness{2}))
   140  		assert.True(t, errors.Is(err, errGreatSadness{3}))
   141  	})
   142  }
   143  

View as plain text