github.com/uber/kraken@v0.1.4/utils/errutil/errutil_test.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package errutil 15 16 import ( 17 "errors" 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestMultiError(t *testing.T) { 24 a := errors.New("a") 25 b := errors.New("b") 26 c := errors.New("c") 27 28 tests := []struct { 29 description string 30 errs []error 31 result string 32 }{ 33 {"empty", nil, ""}, 34 {"one error", []error{a}, "a"}, 35 {"many errors", []error{a, b, c}, "a, b, c"}, 36 } 37 for _, test := range tests { 38 t.Run(test.description, func(t *testing.T) { 39 require.Equal(t, test.result, MultiError(test.errs).Error()) 40 }) 41 } 42 } 43 44 func TestJoinNil(t *testing.T) { 45 f := func() error { 46 var errs []error 47 return Join(errs) 48 } 49 require.NoError(t, f()) 50 } 51 52 func TestJoinNonNil(t *testing.T) { 53 f := func() error { 54 var errs []error 55 errs = append(errs, errors.New("some error")) 56 return Join(errs) 57 } 58 require.Error(t, f()) 59 }