Skip to content

Commit fa9e3e8

Browse files
committed
refactor: test pkg -> require pkg + new helpers
1 parent 3fc2088 commit fa9e3e8

File tree

10 files changed

+167
-153
lines changed

10 files changed

+167
-153
lines changed

err2_test.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99

1010
"github.com/lainio/err2"
11-
"github.com/lainio/err2/internal/test"
11+
"github.com/lainio/err2/internal/require"
1212
"github.com/lainio/err2/try"
1313
)
1414

@@ -62,7 +62,7 @@ func TestHandle_noerrHandler(t *testing.T) {
6262
var err error
6363
var handlerCalled bool
6464
defer func() {
65-
test.Require(t, handlerCalled)
65+
require.That(t, handlerCalled)
6666
}()
6767
// This is the handler we are testing!
6868
defer err2.Handle(&err, func(noerr bool) {
@@ -77,12 +77,12 @@ func TestHandle_noerrHandler(t *testing.T) {
7777
var err error
7878
var handlerCalled bool
7979
defer func() {
80-
test.Require(t, handlerCalled)
80+
require.That(t, handlerCalled)
8181
}()
8282
defer err2.Handle(&err, func(err error) error {
8383
// this should not be called, so lets try to fuckup things...
8484
handlerCalled = false
85-
test.Require(t, false)
85+
require.That(t, false)
8686
return err
8787
})
8888

@@ -99,20 +99,20 @@ func TestHandle_noerrHandler(t *testing.T) {
9999
var err error
100100
var handlerCalled bool
101101
defer func() {
102-
test.Require(t, !handlerCalled)
102+
require.ThatNot(t, handlerCalled)
103103
}()
104104

105105
// This is the handler we are testing!
106106
defer err2.Handle(&err, func(err error) error {
107-
test.Require(t, !handlerCalled)
107+
require.ThatNot(t, handlerCalled)
108108
handlerCalled = false
109-
test.Require(t, true, "error should be handled")
109+
require.That(t, true, "error should be handled")
110110
return err
111111
})
112112

113113
// This is the handler we are testing! AND it's not called in error.
114114
defer err2.Handle(&err, func(bool) {
115-
test.Require(t, false, "when error this is not called")
115+
require.That(t, false, "when error this is not called")
116116
})
117117

118118
try.To1(throw())
@@ -127,32 +127,32 @@ func TestHandle_noerrHandler(t *testing.T) {
127127
callCount int
128128
)
129129
defer func() {
130-
test.Require(t, !handlerCalled)
131-
test.Require(t, callCount == 2)
132-
test.RequireEqual(t, err.Error(), finalAnnotatedErr.Error())
130+
require.ThatNot(t, handlerCalled)
131+
require.Equal(t, callCount, 2)
132+
require.Equal(t, err.Error(), finalAnnotatedErr.Error())
133133
}()
134134

135135
// This is the handler we are testing! AND it's not called in error.
136136
defer err2.Handle(&err, func(noerr bool) {
137-
test.Require(t, false, "if error occurs/reset, this cannot happen")
137+
require.That(t, false, "if error occurs/reset, this cannot happen")
138138
handlerCalled = noerr
139139
})
140140

141141
// important! test that our handler doesn't change the current error
142142
// and it's not nil
143143
defer err2.Handle(&err, func(er error) error {
144-
test.Require(t, er != nil, "er val: ", er, err)
145-
test.Require(t, callCount == 1, "this is called in sencond")
144+
require.That(t, er != nil, "er val: ", er, err)
145+
require.Equal(t, callCount, 1, "this is called in sencond")
146146
callCount++
147147
return er
148148
})
149149

150150
defer err2.Handle(&err, func(err error) error {
151151
// this should not be called, so lets try to fuckup things...
152-
test.Require(t, callCount == 0, "this is called in first")
152+
require.Equal(t, callCount, 0, "this is called in first")
153153
callCount++
154154
handlerCalled = false
155-
test.Require(t, err != nil)
155+
require.That(t, err != nil)
156156
return finalAnnotatedErr
157157
})
158158
try.To1(throw())
@@ -163,17 +163,17 @@ func TestHandle_noerrHandler(t *testing.T) {
163163
var err error
164164
var handlerCalled bool
165165
defer func() {
166-
test.Require(t, handlerCalled)
166+
require.That(t, handlerCalled)
167167
}()
168168

169169
// This is the handler we are testing!
170170
defer err2.Handle(&err, func(noerr bool) {
171-
test.Require(t, noerr)
171+
require.That(t, noerr)
172172
handlerCalled = noerr
173173
})
174174

175175
defer err2.Handle(&err, func(err error) error {
176-
test.Require(t, false, "no error to handle!")
176+
require.That(t, false, "no error to handle!")
177177
// this should not be called, so lets try to fuckup things...
178178
handlerCalled = false // see first deferred function
179179
return err
@@ -186,27 +186,27 @@ func TestHandle_noerrHandler(t *testing.T) {
186186
var err error
187187
var handlerCalled bool
188188
defer func() {
189-
test.Require(t, handlerCalled)
189+
require.That(t, handlerCalled)
190190
}()
191191

192192
// This is the handler we are testing!
193193
defer err2.Handle(&err, func(noerr bool) {
194-
test.Require(t, true)
195-
test.Require(t, noerr)
194+
require.That(t, true)
195+
require.That(t, noerr)
196196
handlerCalled = noerr
197197
})
198198

199199
defer err2.Handle(&err)
200200

201201
defer err2.Handle(&err, func(err error) error {
202-
test.Require(t, false, "no error to handle!")
202+
require.That(t, false, "no error to handle!")
203203
// this should not be called, so lets try to fuckup things...
204204
handlerCalled = false // see first deferred function
205205
return err
206206
})
207207

208208
defer err2.Handle(&err, func(err error) error {
209-
test.Require(t, false, "no error to handle!")
209+
require.That(t, false, "no error to handle!")
210210
// this should not be called, so lets try to fuckup things...
211211
handlerCalled = false // see first deferred function
212212
return err
@@ -219,20 +219,20 @@ func TestHandle_noerrHandler(t *testing.T) {
219219
var err error
220220
var noerrHandlerCalled, errHandlerCalled bool
221221
defer func() {
222-
test.Require(t, noerrHandlerCalled)
223-
test.Require(t, errHandlerCalled)
222+
require.That(t, noerrHandlerCalled)
223+
require.That(t, errHandlerCalled)
224224
}()
225225

226226
// This is the handler we are testing!
227227
defer err2.Handle(&err, func(noerr bool) {
228-
test.Require(t, true) // we are here, for debugging
229-
test.Require(t, noerr)
228+
require.That(t, true) // we are here, for debugging
229+
require.That(t, noerr)
230230
noerrHandlerCalled = noerr
231231
})
232232

233233
// this is the err handler that -- RESETS -- the error to nil
234234
defer err2.Handle(&err, func(err error) error {
235-
test.Require(t, err != nil) // helps fast debugging
235+
require.That(t, err != nil) // helps fast debugging
236236

237237
// this should not be called, so lets try to fuckup things...
238238
noerrHandlerCalled = false // see first deferred function
@@ -242,7 +242,7 @@ func TestHandle_noerrHandler(t *testing.T) {
242242
})
243243

244244
defer err2.Handle(&err, func(err error) error {
245-
test.Require(t, err != nil) // helps fast debugging
245+
require.That(t, err != nil) // helps fast debugging
246246
// this should not be called, so lets try to fuckup things...
247247
noerrHandlerCalled = false // see first deferred function
248248

@@ -257,28 +257,28 @@ func TestHandle_noerrHandler(t *testing.T) {
257257
var err error
258258
var handlerCalled bool
259259
defer func() {
260-
test.Require(t, handlerCalled)
260+
require.That(t, handlerCalled)
261261
}()
262262

263263
defer err2.Handle(&err)
264264
defer err2.Handle(&err)
265265

266266
defer err2.Handle(&err, func(err error) error {
267-
test.Require(t, false, "no error to handle!")
267+
require.That(t, false, "no error to handle!")
268268
// this should not be called, so lets try to fuckup things...
269269
handlerCalled = false // see first deferred function
270270
return err
271271
})
272272

273273
// This is the handler we are testing!
274274
defer err2.Handle(&err, func(noerr bool) {
275-
test.Require(t, true, "this must be called")
276-
test.Require(t, noerr)
275+
require.That(t, true, "this must be called")
276+
require.That(t, noerr)
277277
handlerCalled = noerr
278278
})
279279

280280
defer err2.Handle(&err, func(err error) error {
281-
test.Require(t, false, "no error to handle!")
281+
require.That(t, false, "no error to handle!")
282282
// this should not be called, so lets try to fuckup things...
283283
handlerCalled = false // see first deferred function
284284
return err
@@ -348,7 +348,7 @@ func TestPanickingCatchAll(t *testing.T) {
348348
t.Run(tt.name, func(t *testing.T) {
349349
t.Parallel()
350350
defer func() {
351-
test.Require(t, recover() == nil, "panics should NOT carry on")
351+
require.That(t, recover() == nil, "panics should NOT carry on")
352352
}()
353353
tt.args.f()
354354
})
@@ -392,7 +392,7 @@ func TestPanickingCarryOn_Handle(t *testing.T) {
392392
t.Run(tt.name, func(t *testing.T) {
393393
t.Parallel()
394394
defer func() {
395-
test.Require(t, recover() != nil, "panics should went thru when not our errors")
395+
require.That(t, recover() != nil, "panics should went thru when not our errors")
396396
}()
397397
tt.args.f()
398398
})
@@ -518,12 +518,12 @@ func TestPanicking_Handle(t *testing.T) {
518518
defer func() {
519519
r := recover()
520520
if tt.wants == nil {
521-
test.Require(t, r != nil, "wants err, then panic")
521+
require.That(t, r != nil, "wants err, then panic")
522522
}
523523
}()
524524
err := tt.args.f()
525525
if err != nil {
526-
test.RequireEqual(t, err.Error(), tt.wants.Error())
526+
require.Equal(t, err.Error(), tt.wants.Error())
527527
}
528528
})
529529
}
@@ -564,7 +564,7 @@ func TestPanicking_Catch(t *testing.T) {
564564
t.Run(tt.name, func(t *testing.T) {
565565
t.Parallel()
566566
defer func() {
567-
test.Require(t, recover() == nil, "panics should NOT carry on")
567+
require.That(t, recover() == nil, "panics should NOT carry on")
568568
}()
569569
tt.args.f()
570570
})
@@ -583,7 +583,7 @@ func TestCatch_Error(t *testing.T) {
583583
func Test_TryOutError(t *testing.T) {
584584
t.Parallel()
585585
defer err2.Catch(func(err error) error {
586-
test.RequireEqual(t, err.Error(), "fails: test: this is an ERROR",
586+
require.Equal(t, err.Error(), "fails: test: this is an ERROR",
587587
"=> we should catch right error str here")
588588
return err
589589
})
@@ -593,7 +593,7 @@ func Test_TryOutError(t *testing.T) {
593593
// let's test try.Out1() and it's throw capabilities here, even try.To1()
594594
// is the preferred way.
595595
retVal = try.Out1(noThrow()).Handle().Val1
596-
test.Require(t, retVal == "test", "if no error happens, we get value")
596+
require.Equal(t, retVal, "test", "if no error happens, we get value")
597597

598598
_ = try.Out1(throw()).Handle("fails: %v", retVal).Val1
599599
t.Fail() // If everything works in Handle we are never here.
@@ -625,11 +625,11 @@ func TestCatch_Panic(t *testing.T) {
625625
func TestSetErrorTracer(t *testing.T) {
626626
t.Parallel()
627627
w := err2.ErrorTracer()
628-
test.Require(t, w == nil, "error tracer should be nil")
628+
require.That(t, w == nil, "error tracer should be nil")
629629
var w1 io.Writer
630630
err2.SetErrorTracer(w1)
631631
w = err2.ErrorTracer()
632-
test.Require(t, w == nil, "error tracer should be nil")
632+
require.That(t, w == nil, "error tracer should be nil")
633633
}
634634

635635
func ExampleCatch_withFmt() {

0 commit comments

Comments
 (0)