istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/util/errs_test.go (about) 1 // Copyright Istio Authors 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 15 package util 16 17 import ( 18 "fmt" 19 "testing" 20 ) 21 22 var ( 23 testErrs = Errors{fmt.Errorf("err1"), fmt.Errorf("err2")} 24 wantStr = "err1, err2" 25 ) 26 27 func TestError(t *testing.T) { 28 if got, want := testErrs.Error(), wantStr; got != want { 29 t.Errorf("got: %s, want: %s", got, want) 30 } 31 } 32 33 func TestString(t *testing.T) { 34 if got, want := testErrs.String(), wantStr; got != want { 35 t.Errorf("got: %s, want: %s", got, want) 36 } 37 } 38 39 func TestToString(t *testing.T) { 40 if got, want := ToString(testErrs, defaultSeparator), wantStr; got != want { 41 t.Errorf("got: %s, want: %s", got, want) 42 } 43 } 44 45 func TestNewErrs(t *testing.T) { 46 errs := NewErrs(nil) 47 if errs != nil { 48 t.Errorf("got: %s, want: nil", errs) 49 } 50 51 errs = NewErrs(fmt.Errorf("err1")) 52 if got, want := errs.String(), "err1"; got != want { 53 t.Errorf("got: %s, want: %s", got, want) 54 } 55 } 56 57 func TestAppendErr(t *testing.T) { 58 var errs Errors 59 if got, want := errs.String(), ""; got != want { 60 t.Errorf("got: %s, want: %s", got, want) 61 } 62 63 errs = AppendErr(errs, nil) 64 if got, want := errs.String(), ""; got != want { 65 t.Errorf("got: %s, want: %s", got, want) 66 } 67 68 errs = AppendErr(errs, fmt.Errorf("err1")) 69 if got, want := errs.String(), "err1"; got != want { 70 t.Errorf("got: %s, want: %s", got, want) 71 } 72 73 errs = AppendErr(errs, nil) 74 errs = AppendErr(errs, fmt.Errorf("err2")) 75 if got, want := errs.String(), "err1, err2"; got != want { 76 t.Errorf("got: %s, want: %s", got, want) 77 } 78 } 79 80 func TestAppendErrs(t *testing.T) { 81 var errs Errors 82 83 errs = AppendErrs(errs, []error{nil}) 84 if got, want := errs.String(), ""; got != want { 85 t.Errorf("got: %s, want: %s", got, want) 86 } 87 88 errs = AppendErrs(errs, testErrs) 89 errs = AppendErrs(errs, []error{nil}) 90 if got, want := errs.String(), wantStr; got != want { 91 t.Errorf("got: %s, want: %s", got, want) 92 } 93 } 94 95 func TestAppendErrsInFunction(t *testing.T) { 96 myAppendErrFunc := func() (errs Errors) { 97 errs = AppendErr(errs, fmt.Errorf("err1")) 98 errs = AppendErr(errs, fmt.Errorf("err2")) 99 return 100 } 101 if got, want := myAppendErrFunc().String(), wantStr; got != want { 102 t.Errorf("got: %s, want: %s", got, want) 103 } 104 105 myAppendErrsFunc := func() (errs Errors) { 106 errs = AppendErrs(errs, testErrs) 107 return 108 } 109 if got, want := myAppendErrsFunc().String(), wantStr; got != want { 110 t.Errorf("got: %s, want: %s", got, want) 111 } 112 113 myErrorSliceFunc := func() (errs []error) { 114 errs = AppendErrs(errs, testErrs) 115 return 116 } 117 118 if got, want := Errors(myErrorSliceFunc()).String(), wantStr; got != want { 119 t.Errorf("got: %s, want: %s", got, want) 120 } 121 }