Skip to content

Commit c1538a0

Browse files
committed
better tests for noerr case and chaingin handlers
1 parent 9cf5db4 commit c1538a0

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

err2_test.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"github.com/lainio/err2/try"
1212
)
1313

14+
const errStringInThrow = "this is an ERROR"
15+
1416
func throw() (string, error) {
15-
return "", fmt.Errorf("this is an ERROR")
17+
return "", fmt.Errorf(errStringInThrow)
1618
}
1719

1820
func twoStrNoThrow() (string, string, error) { return "test", "test", nil }
@@ -113,12 +115,16 @@ func TestHandle_noerrHandler(t *testing.T) {
113115
try.To1(throw())
114116
})
115117

116-
t.Run("noerr is first and error happens", func(t *testing.T) {
118+
t.Run("noerr is first and error happens with many handlers", func(t *testing.T) {
117119
t.Parallel()
118-
var err error
119-
var handlerCalled bool
120+
var (
121+
err error
122+
finalAnnotatedErr = fmt.Errorf("err: %v", errStringInThrow)
123+
handlerCalled bool
124+
)
120125
defer func() {
121126
test.Require(t, !handlerCalled)
127+
test.RequireEqual(t, err.Error(), finalAnnotatedErr.Error())
122128
}()
123129

124130
// This is the handler we are thesting!
@@ -127,11 +133,18 @@ func TestHandle_noerrHandler(t *testing.T) {
127133
handlerCalled = noerr
128134
})
129135

136+
// important! test that our handler doesn't change the current error
137+
// and it's not nil
138+
defer err2.Handle(&err, func(er error) error {
139+
test.Require(t, er != nil, "er val: ", er, err)
140+
return er
141+
})
142+
130143
defer err2.Handle(&err, func(err error) error {
131144
// this should not be called, so lets try to fuckup things...
132145
handlerCalled = false
133146
test.Require(t, err != nil)
134-
return err
147+
return finalAnnotatedErr
135148
})
136149
try.To1(throw())
137150
})
@@ -191,7 +204,7 @@ func TestHandle_noerrHandler(t *testing.T) {
191204
try.To(noErr())
192205
})
193206

194-
t.Run("noerr handler is first of MANY and error happens UNTIL reset", func(t *testing.T) {
207+
t.Run("noerr handler is first of MANY and error happens UNTIL RESET", func(t *testing.T) {
195208
t.Parallel()
196209
var err error
197210
var noerrHandlerCalled, errHandlerCalled bool
@@ -202,14 +215,14 @@ func TestHandle_noerrHandler(t *testing.T) {
202215

203216
// This is the handler we are thesting!
204217
defer err2.Handle(&err, func(noerr bool) {
205-
test.Require(t, true)
218+
test.Require(t, true) // we are here, for debugging
206219
test.Require(t, noerr)
207220
noerrHandlerCalled = noerr
208221
})
209222

210-
// this is the err handler that resets the error to nil
223+
// this is the err handler that -- RESETS -- the error to nil
211224
defer err2.Handle(&err, func(err error) error {
212-
test.Require(t, true) // helps fast debugging
225+
test.Require(t, err != nil) // helps fast debugging
213226

214227
// this should not be called, so lets try to fuckup things...
215228
noerrHandlerCalled = false // see first deferred function
@@ -219,7 +232,7 @@ func TestHandle_noerrHandler(t *testing.T) {
219232
})
220233

221234
defer err2.Handle(&err, func(err error) error {
222-
test.Require(t, true) // helps fast debugging
235+
test.Require(t, err != nil) // helps fast debugging
223236
// this should not be called, so lets try to fuckup things...
224237
noerrHandlerCalled = false // see first deferred function
225238

@@ -237,6 +250,7 @@ func TestHandle_noerrHandler(t *testing.T) {
237250
test.Require(t, handlerCalled)
238251
}()
239252

253+
defer err2.Handle(&err)
240254
defer err2.Handle(&err)
241255

242256
defer err2.Handle(&err, func(err error) error {

0 commit comments

Comments
 (0)