Skip to content

Commit c038678

Browse files
committed
Merge branch 'bench-tests' of ssh://g.yxqyang.asia-lainio/lainio/err2 into bench-tests
2 parents 4ad8245 + 05a0684 commit c038678

File tree

12 files changed

+196
-163
lines changed

12 files changed

+196
-163
lines changed

.golangci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ linters-settings:
3333
# Default: 5
3434
min-complexity: 8
3535
gomnd:
36-
settings:
37-
mnd:
38-
# don't include the "operation" and "assign"
39-
checks: argument,case,condition,return
4036
govet:
41-
check-shadowing: true
4237
lll:
4338
line-length: 140
4439
maligned:

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PKGS := $(PKG_ERR2) $(PKG_ASSERT) $(PKG_TRY) $(PKG_DEBUG) $(PKG_HANDLER) $(PKG_S
1111

1212
SRCDIRS := $(shell go list -f '{{.Dir}}' $(PKGS))
1313

14+
MAX_LINE ?= 80
1415
GO ?= go
1516
TEST_ARGS ?= -benchmem
1617
# -"gcflags '-N -l'" both optimization & inlining disabled
@@ -106,6 +107,12 @@ bench_x:
106107
vet: | test
107108
$(GO) vet $(PKGS)
108109

110+
fmt:
111+
@golines -t 5 -w -m $(MAX_LINE) --ignore-generated .
112+
113+
dry-fmt:
114+
@golines -t 5 --dry-run -m $(MAX_LINE) --ignore-generated .
115+
109116
gofmt:
110117
@echo Checking code is gofmted
111118
@test -z "$(shell gofmt -s -l -d -e $(SRCDIRS) | tee /dev/stderr)"

err2_test.go

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package err2_test
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
78
"runtime"
89
"testing"
910

1011
"github.com/lainio/err2"
11-
"github.com/lainio/err2/internal/test"
12+
"github.com/lainio/err2/internal/require"
1213
"github.com/lainio/err2/try"
1314
)
1415

1516
const errStringInThrow = "this is an ERROR"
1617

17-
func throw() (string, error) {
18-
return "", fmt.Errorf(errStringInThrow)
19-
}
18+
var (
19+
errToTest = errors.New(errStringInThrow)
20+
)
21+
22+
func throw() (string, error) { return "", errToTest }
23+
func noThrow() (string, error) { return "test", nil }
24+
func noErr() error { return nil }
2025

2126
func twoStrNoThrow() (string, string, error) { return "test", "test", nil }
2227
func intStrNoThrow() (int, string, error) { return 1, "test", nil }
2328
func boolIntStrNoThrow() (bool, int, string, error) { return true, 1, "test", nil }
24-
func noThrow() (string, error) { return "test", nil }
25-
26-
func noErr() error {
27-
return nil
28-
}
2929

3030
func TestTry_noError(t *testing.T) {
3131
t.Parallel()
@@ -62,9 +62,9 @@ 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
}()
67-
// This is the handler we are thesting!
67+
// This is the handler we are testing!
6868
defer err2.Handle(&err, func(noerr bool) {
6969
handlerCalled = noerr
7070
})
@@ -77,16 +77,16 @@ 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

89-
// This is the handler we are thesting!
89+
// This is the handler we are testing!
9090
defer err2.Handle(&err, func(noerr bool) {
9191
handlerCalled = noerr
9292
})
@@ -99,18 +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
}()
104+
105+
// This is the handler we are testing!
104106
defer err2.Handle(&err, func(err error) error {
107+
require.ThatNot(t, handlerCalled)
105108
handlerCalled = false
106-
test.Require(t, true, "error should be handled")
109+
require.That(t, true, "error should be handled")
107110
return err
108111
})
109112

110-
// This is the handler we are thesting!
111-
defer err2.Handle(&err, func(noerr bool) {
112-
test.Require(t, noerr)
113-
handlerCalled = noerr
113+
// This is the handler we are testing! AND it's not called in error.
114+
defer err2.Handle(&err, func(bool) {
115+
require.That(t, false, "when error this is not called")
114116
})
115117

116118
try.To1(throw())
@@ -122,29 +124,35 @@ func TestHandle_noerrHandler(t *testing.T) {
122124
err error
123125
finalAnnotatedErr = fmt.Errorf("err: %v", errStringInThrow)
124126
handlerCalled bool
127+
callCount int
125128
)
126129
defer func() {
127-
test.Require(t, !handlerCalled)
128-
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())
129133
}()
130134

131-
// This is the handler we are thesting!
135+
// This is the handler we are testing! AND it's not called in error.
132136
defer err2.Handle(&err, func(noerr bool) {
133-
test.Require(t, false, "if error occurs/reset, this cannot happen")
137+
require.That(t, false, "if error occurs/reset, this cannot happen")
134138
handlerCalled = noerr
135139
})
136140

137141
// important! test that our handler doesn't change the current error
138142
// and it's not nil
139143
defer err2.Handle(&err, func(er error) error {
140-
test.Require(t, er != nil, "er val: ", er, err)
144+
require.That(t, er != nil, "er val: ", er, err)
145+
require.Equal(t, callCount, 1, "this is called in sencond")
146+
callCount++
141147
return er
142148
})
143149

144150
defer err2.Handle(&err, func(err error) error {
145151
// this should not be called, so lets try to fuckup things...
152+
require.Equal(t, callCount, 0, "this is called in first")
153+
callCount++
146154
handlerCalled = false
147-
test.Require(t, err != nil)
155+
require.That(t, err != nil)
148156
return finalAnnotatedErr
149157
})
150158
try.To1(throw())
@@ -155,17 +163,17 @@ func TestHandle_noerrHandler(t *testing.T) {
155163
var err error
156164
var handlerCalled bool
157165
defer func() {
158-
test.Require(t, handlerCalled)
166+
require.That(t, handlerCalled)
159167
}()
160168

161-
// This is the handler we are thesting!
169+
// This is the handler we are testing!
162170
defer err2.Handle(&err, func(noerr bool) {
163-
test.Require(t, noerr)
171+
require.That(t, noerr)
164172
handlerCalled = noerr
165173
})
166174

167175
defer err2.Handle(&err, func(err error) error {
168-
test.Require(t, false, "no error to handle!")
176+
require.That(t, false, "no error to handle!")
169177
// this should not be called, so lets try to fuckup things...
170178
handlerCalled = false // see first deferred function
171179
return err
@@ -178,26 +186,27 @@ func TestHandle_noerrHandler(t *testing.T) {
178186
var err error
179187
var handlerCalled bool
180188
defer func() {
181-
test.Require(t, handlerCalled)
189+
require.That(t, handlerCalled)
182190
}()
183191

184-
// This is the handler we are thesting!
192+
// This is the handler we are testing!
185193
defer err2.Handle(&err, func(noerr bool) {
186-
test.Require(t, noerr)
194+
require.That(t, true)
195+
require.That(t, noerr)
187196
handlerCalled = noerr
188197
})
189198

190199
defer err2.Handle(&err)
191200

192201
defer err2.Handle(&err, func(err error) error {
193-
test.Require(t, false, "no error to handle!")
202+
require.That(t, false, "no error to handle!")
194203
// this should not be called, so lets try to fuckup things...
195204
handlerCalled = false // see first deferred function
196205
return err
197206
})
198207

199208
defer err2.Handle(&err, func(err error) error {
200-
test.Require(t, false, "no error to handle!")
209+
require.That(t, false, "no error to handle!")
201210
// this should not be called, so lets try to fuckup things...
202211
handlerCalled = false // see first deferred function
203212
return err
@@ -210,20 +219,20 @@ func TestHandle_noerrHandler(t *testing.T) {
210219
var err error
211220
var noerrHandlerCalled, errHandlerCalled bool
212221
defer func() {
213-
test.Require(t, noerrHandlerCalled)
214-
test.Require(t, errHandlerCalled)
222+
require.That(t, noerrHandlerCalled)
223+
require.That(t, errHandlerCalled)
215224
}()
216225

217-
// This is the handler we are thesting!
226+
// This is the handler we are testing!
218227
defer err2.Handle(&err, func(noerr bool) {
219-
test.Require(t, true) // we are here, for debugging
220-
test.Require(t, noerr)
228+
require.That(t, true) // we are here, for debugging
229+
require.That(t, noerr)
221230
noerrHandlerCalled = noerr
222231
})
223232

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

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

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

@@ -248,27 +257,28 @@ func TestHandle_noerrHandler(t *testing.T) {
248257
var err error
249258
var handlerCalled bool
250259
defer func() {
251-
test.Require(t, handlerCalled)
260+
require.That(t, handlerCalled)
252261
}()
253262

254263
defer err2.Handle(&err)
255264
defer err2.Handle(&err)
256265

257266
defer err2.Handle(&err, func(err error) error {
258-
test.Require(t, false, "no error to handle!")
267+
require.That(t, false, "no error to handle!")
259268
// this should not be called, so lets try to fuckup things...
260269
handlerCalled = false // see first deferred function
261270
return err
262271
})
263272

264-
// This is the handler we are thesting!
273+
// This is the handler we are testing!
265274
defer err2.Handle(&err, func(noerr bool) {
266-
test.Require(t, noerr)
275+
require.That(t, true, "this must be called")
276+
require.That(t, noerr)
267277
handlerCalled = noerr
268278
})
269279

270280
defer err2.Handle(&err, func(err error) error {
271-
test.Require(t, false, "no error to handle!")
281+
require.That(t, false, "no error to handle!")
272282
// this should not be called, so lets try to fuckup things...
273283
handlerCalled = false // see first deferred function
274284
return err
@@ -338,7 +348,7 @@ func TestPanickingCatchAll(t *testing.T) {
338348
t.Run(tt.name, func(t *testing.T) {
339349
t.Parallel()
340350
defer func() {
341-
test.Require(t, recover() == nil, "panics should NOT carry on")
351+
require.That(t, recover() == nil, "panics should NOT carry on")
342352
}()
343353
tt.args.f()
344354
})
@@ -382,7 +392,7 @@ func TestPanickingCarryOn_Handle(t *testing.T) {
382392
t.Run(tt.name, func(t *testing.T) {
383393
t.Parallel()
384394
defer func() {
385-
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")
386396
}()
387397
tt.args.f()
388398
})
@@ -508,12 +518,12 @@ func TestPanicking_Handle(t *testing.T) {
508518
defer func() {
509519
r := recover()
510520
if tt.wants == nil {
511-
test.Require(t, r != nil, "wants err, then panic")
521+
require.That(t, r != nil, "wants err, then panic")
512522
}
513523
}()
514524
err := tt.args.f()
515525
if err != nil {
516-
test.RequireEqual(t, err.Error(), tt.wants.Error())
526+
require.Equal(t, err.Error(), tt.wants.Error())
517527
}
518528
})
519529
}
@@ -554,7 +564,7 @@ func TestPanicking_Catch(t *testing.T) {
554564
t.Run(tt.name, func(t *testing.T) {
555565
t.Parallel()
556566
defer func() {
557-
test.Require(t, recover() == nil, "panics should NOT carry on")
567+
require.That(t, recover() == nil, "panics should NOT carry on")
558568
}()
559569
tt.args.f()
560570
})
@@ -573,7 +583,7 @@ func TestCatch_Error(t *testing.T) {
573583
func Test_TryOutError(t *testing.T) {
574584
t.Parallel()
575585
defer err2.Catch(func(err error) error {
576-
test.RequireEqual(t, err.Error(), "fails: test: this is an ERROR",
586+
require.Equal(t, err.Error(), "fails: test: this is an ERROR",
577587
"=> we should catch right error str here")
578588
return err
579589
})
@@ -583,7 +593,7 @@ func Test_TryOutError(t *testing.T) {
583593
// let's test try.Out1() and it's throw capabilities here, even try.To1()
584594
// is the preferred way.
585595
retVal = try.Out1(noThrow()).Handle().Val1
586-
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")
587597

588598
_ = try.Out1(throw()).Handle("fails: %v", retVal).Val1
589599
t.Fail() // If everything works in Handle we are never here.
@@ -615,11 +625,11 @@ func TestCatch_Panic(t *testing.T) {
615625
func TestSetErrorTracer(t *testing.T) {
616626
t.Parallel()
617627
w := err2.ErrorTracer()
618-
test.Require(t, w == nil, "error tracer should be nil")
628+
require.That(t, w == nil, "error tracer should be nil")
619629
var w1 io.Writer
620630
err2.SetErrorTracer(w1)
621631
w = err2.ErrorTracer()
622-
test.Require(t, w == nil, "error tracer should be nil")
632+
require.That(t, w == nil, "error tracer should be nil")
623633
}
624634

625635
func ExampleCatch_withFmt() {

0 commit comments

Comments
 (0)