github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/utils/error_collection_test.go (about) 1 /* 2 * Copyright (C) 2019 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package utils 19 20 import ( 21 "testing" 22 23 "github.com/pkg/errors" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 var ( 28 errorNone error 29 errorFirst = errors.New("First") 30 errorSecond = errors.New("Second") 31 ) 32 33 func Test_ErrorCollector_Add(t *testing.T) { 34 err := ErrorCollection{} 35 assert.Len(t, err, 0) 36 37 err = ErrorCollection{} 38 err.Add(errorFirst) 39 assert.Len(t, err, 1) 40 assert.Equal(t, ErrorCollection([]error{errorFirst}), err) 41 42 err = ErrorCollection{} 43 err.Add(errorFirst, errorSecond) 44 assert.Len(t, err, 2) 45 assert.Equal(t, ErrorCollection([]error{errorFirst, errorSecond}), err) 46 47 err = ErrorCollection{} 48 err.Add(errorFirst, errorNone, errorSecond) 49 assert.Len(t, err, 2) 50 assert.Equal(t, ErrorCollection([]error{errorFirst, errorSecond}), err) 51 } 52 53 func Test_ErrorCollector_String(t *testing.T) { 54 err := ErrorCollection{} 55 assert.Equal(t, "ErrorCollection: ", err.String()) 56 57 err = ErrorCollection{} 58 err.Add(errorFirst) 59 assert.Equal(t, "ErrorCollection: First", err.String()) 60 61 err = ErrorCollection{} 62 err.Add(errorFirst, errorSecond) 63 assert.Equal(t, "ErrorCollection: First, Second", err.String()) 64 } 65 66 func Test_ErrorCollector_Stringf(t *testing.T) { 67 err := ErrorCollection{} 68 assert.Equal(t, "Failed! ", err.Stringf("Failed! %s", ". ")) 69 70 err = ErrorCollection{} 71 err.Add(errorFirst) 72 assert.Equal(t, "Failed! First", err.Stringf("Failed! %s", ". ")) 73 74 err = ErrorCollection{} 75 err.Add(errorFirst, errorSecond) 76 assert.Equal(t, "Failed! First. Second", err.Stringf("Failed! %s", ". ")) 77 } 78 79 func Test_ErrorCollector_Error(t *testing.T) { 80 err := ErrorCollection{} 81 assert.NoError(t, err.Error()) 82 83 err = ErrorCollection{} 84 err.Add(errorFirst) 85 assert.EqualError(t, err.Error(), "ErrorCollection: First") 86 87 err = ErrorCollection{} 88 err.Add(errorFirst, errorSecond) 89 assert.EqualError(t, err.Error(), "ErrorCollection: First, Second") 90 } 91 92 func Test_ErrorCollector_Errorf(t *testing.T) { 93 err := ErrorCollection{} 94 assert.NoError(t, err.Errorf("Failed! %s", ". ")) 95 96 err = ErrorCollection{} 97 err.Add(errorFirst) 98 assert.EqualError(t, err.Errorf("Failed! %s", ". "), "Failed! First") 99 100 err = ErrorCollection{} 101 err.Add(errorFirst, errorSecond) 102 assert.EqualError(t, err.Errorf("Failed! %s", ". "), "Failed! First. Second") 103 }