go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/errors/walk_test.go (about) 1 // Copyright 2015 The LUCI 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 errors 16 17 import ( 18 "errors" 19 "fmt" 20 "testing" 21 22 . "github.com/smartystreets/goconvey/convey" 23 ) 24 25 func TestWalk(t *testing.T) { 26 t.Parallel() 27 28 Convey(`Testing the Walk function`, t, func() { 29 count := 0 30 keepWalking := true 31 walkFn := func(err error) bool { 32 count++ 33 return keepWalking 34 } 35 36 Convey(`Will not walk at all for a nil error.`, func() { 37 Walk(nil, walkFn) 38 So(count, ShouldEqual, 0) 39 }) 40 41 Convey(`Will fully traverse a wrapped MultiError.`, func() { 42 Walk(MultiError{nil, testWrap(New("sup")), nil}, walkFn) 43 So(count, ShouldEqual, 3) 44 }) 45 46 Convey(`Will unwrap a Wrapped error.`, func() { 47 Walk(testWrap(New("sup")), walkFn) 48 So(count, ShouldEqual, 2) 49 }) 50 51 Convey(`Will short-circuit if the walk function returns false.`, func() { 52 keepWalking = false 53 Walk(testWrap(New("sup")), walkFn) 54 So(count, ShouldEqual, 1) 55 }) 56 }) 57 58 Convey(`Testing the WalkLeaves function`, t, func() { 59 count := 0 60 keepWalking := true 61 walkFn := func(err error) bool { 62 count++ 63 return keepWalking 64 } 65 66 Convey(`Will not walk at all for a nil error.`, func() { 67 WalkLeaves(nil, walkFn) 68 So(count, ShouldEqual, 0) 69 }) 70 71 Convey(`Will traverse leaves of a wrapped MultiError.`, func() { 72 WalkLeaves(MultiError{nil, testWrap(New("sup")), New("sup")}, walkFn) 73 So(count, ShouldEqual, 2) 74 }) 75 76 Convey(`Will unwrap a Wrapped error.`, func() { 77 WalkLeaves(testWrap(New("sup")), walkFn) 78 So(count, ShouldEqual, 1) 79 }) 80 81 Convey(`Will short-circuit if the walk function returns false.`, func() { 82 keepWalking = false 83 WalkLeaves(MultiError{testWrap(New("sup")), New("foo")}, walkFn) 84 So(count, ShouldEqual, 1) 85 }) 86 }) 87 } 88 89 type intError int 90 91 func (i *intError) Is(err error) bool { 92 if e, ok := err.(*intError); ok { 93 return int(*i)/2 == int(*e)/2 94 } 95 return false 96 } 97 98 func (i *intError) Error() string { 99 return fmt.Sprintf("%d", int(*i)) 100 } 101 102 func TestAny(t *testing.T) { 103 t.Parallel() 104 105 Convey(`Testing the Any function`, t, func() { 106 testErr := errors.New("test error") 107 filter := func(err error) bool { return err == testErr } 108 109 for _, err := range []error{ 110 nil, 111 Reason("error test: foo").Err(), 112 errors.New("other error"), 113 } { 114 Convey(fmt.Sprintf(`Registers false for %T %v`, err, err), func() { 115 So(Any(err, filter), ShouldBeFalse) 116 }) 117 } 118 119 for _, err := range []error{ 120 testErr, 121 MultiError{errors.New("other error"), MultiError{testErr, nil}}, 122 Annotate(testErr, "error test").Err(), 123 } { 124 Convey(fmt.Sprintf(`Registers true for %T %v`, err, err), func() { 125 So(Any(err, filter), ShouldBeTrue) 126 }) 127 } 128 }) 129 } 130 131 func TestContains(t *testing.T) { 132 t.Parallel() 133 134 Convey(`Testing the Contains function`, t, func() { 135 testErr := errors.New("test error") 136 137 for _, err := range []error{ 138 nil, 139 Reason("error test: foo").Err(), 140 errors.New("other error"), 141 } { 142 Convey(fmt.Sprintf(`Registers false for %T %v`, err, err), func() { 143 So(Contains(err, testErr), ShouldBeFalse) 144 }) 145 } 146 147 for _, err := range []error{ 148 testErr, 149 MultiError{errors.New("other error"), MultiError{testErr, nil}}, 150 Annotate(testErr, "error test").Err(), 151 } { 152 Convey(fmt.Sprintf(`Registers true for %T %v`, err, err), func() { 153 So(Contains(err, testErr), ShouldBeTrue) 154 }) 155 } 156 157 Convey(`Support Is`, func() { 158 e0 := intError(0) 159 e1 := intError(1) 160 e2 := intError(2) 161 wrapped0 := testWrap(&e0) 162 So(Contains(wrapped0, &e0), ShouldBeTrue) 163 So(Contains(wrapped0, &e1), ShouldBeTrue) 164 So(Contains(wrapped0, &e2), ShouldBeFalse) 165 }) 166 }) 167 }