Skip to content

Commit

Permalink
refactor: test pkg -> require pkg + new helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Aug 16, 2024
1 parent 3fc2088 commit fa9e3e8
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 153 deletions.
88 changes: 44 additions & 44 deletions err2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/lainio/err2"
"github.com/lainio/err2/internal/test"
"github.com/lainio/err2/internal/require"
"github.com/lainio/err2/try"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestHandle_noerrHandler(t *testing.T) {
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
require.That(t, handlerCalled)
}()
// This is the handler we are testing!
defer err2.Handle(&err, func(noerr bool) {
Expand All @@ -77,12 +77,12 @@ func TestHandle_noerrHandler(t *testing.T) {
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
require.That(t, handlerCalled)
}()
defer err2.Handle(&err, func(err error) error {
// this should not be called, so lets try to fuckup things...
handlerCalled = false
test.Require(t, false)
require.That(t, false)
return err
})

Expand All @@ -99,20 +99,20 @@ func TestHandle_noerrHandler(t *testing.T) {
var err error
var handlerCalled bool
defer func() {
test.Require(t, !handlerCalled)
require.ThatNot(t, handlerCalled)
}()

// This is the handler we are testing!
defer err2.Handle(&err, func(err error) error {
test.Require(t, !handlerCalled)
require.ThatNot(t, handlerCalled)
handlerCalled = false
test.Require(t, true, "error should be handled")
require.That(t, true, "error should be handled")
return err
})

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

try.To1(throw())
Expand All @@ -127,32 +127,32 @@ func TestHandle_noerrHandler(t *testing.T) {
callCount int
)
defer func() {
test.Require(t, !handlerCalled)
test.Require(t, callCount == 2)
test.RequireEqual(t, err.Error(), finalAnnotatedErr.Error())
require.ThatNot(t, handlerCalled)
require.Equal(t, callCount, 2)
require.Equal(t, err.Error(), finalAnnotatedErr.Error())
}()

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

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

defer err2.Handle(&err, func(err error) error {
// this should not be called, so lets try to fuckup things...
test.Require(t, callCount == 0, "this is called in first")
require.Equal(t, callCount, 0, "this is called in first")
callCount++
handlerCalled = false
test.Require(t, err != nil)
require.That(t, err != nil)
return finalAnnotatedErr
})
try.To1(throw())
Expand All @@ -163,17 +163,17 @@ func TestHandle_noerrHandler(t *testing.T) {
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
require.That(t, handlerCalled)
}()

// This is the handler we are testing!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, noerr)
require.That(t, noerr)
handlerCalled = noerr
})

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

// This is the handler we are testing!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, true)
test.Require(t, noerr)
require.That(t, true)
require.That(t, noerr)
handlerCalled = noerr
})

defer err2.Handle(&err)

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

defer err2.Handle(&err, func(err error) error {
test.Require(t, false, "no error to handle!")
require.That(t, false, "no error to handle!")
// this should not be called, so lets try to fuckup things...
handlerCalled = false // see first deferred function
return err
Expand All @@ -219,20 +219,20 @@ func TestHandle_noerrHandler(t *testing.T) {
var err error
var noerrHandlerCalled, errHandlerCalled bool
defer func() {
test.Require(t, noerrHandlerCalled)
test.Require(t, errHandlerCalled)
require.That(t, noerrHandlerCalled)
require.That(t, errHandlerCalled)
}()

// This is the handler we are testing!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, true) // we are here, for debugging
test.Require(t, noerr)
require.That(t, true) // we are here, for debugging
require.That(t, noerr)
noerrHandlerCalled = noerr
})

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

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

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

Expand All @@ -257,28 +257,28 @@ func TestHandle_noerrHandler(t *testing.T) {
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
require.That(t, handlerCalled)
}()

defer err2.Handle(&err)
defer err2.Handle(&err)

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

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

defer err2.Handle(&err, func(err error) error {
test.Require(t, false, "no error to handle!")
require.That(t, false, "no error to handle!")
// this should not be called, so lets try to fuckup things...
handlerCalled = false // see first deferred function
return err
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestPanickingCatchAll(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defer func() {
test.Require(t, recover() == nil, "panics should NOT carry on")
require.That(t, recover() == nil, "panics should NOT carry on")
}()
tt.args.f()
})
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestPanickingCarryOn_Handle(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defer func() {
test.Require(t, recover() != nil, "panics should went thru when not our errors")
require.That(t, recover() != nil, "panics should went thru when not our errors")
}()
tt.args.f()
})
Expand Down Expand Up @@ -518,12 +518,12 @@ func TestPanicking_Handle(t *testing.T) {
defer func() {
r := recover()
if tt.wants == nil {
test.Require(t, r != nil, "wants err, then panic")
require.That(t, r != nil, "wants err, then panic")
}
}()
err := tt.args.f()
if err != nil {
test.RequireEqual(t, err.Error(), tt.wants.Error())
require.Equal(t, err.Error(), tt.wants.Error())
}
})
}
Expand Down Expand Up @@ -564,7 +564,7 @@ func TestPanicking_Catch(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defer func() {
test.Require(t, recover() == nil, "panics should NOT carry on")
require.That(t, recover() == nil, "panics should NOT carry on")
}()
tt.args.f()
})
Expand All @@ -583,7 +583,7 @@ func TestCatch_Error(t *testing.T) {
func Test_TryOutError(t *testing.T) {
t.Parallel()
defer err2.Catch(func(err error) error {
test.RequireEqual(t, err.Error(), "fails: test: this is an ERROR",
require.Equal(t, err.Error(), "fails: test: this is an ERROR",
"=> we should catch right error str here")
return err
})
Expand All @@ -593,7 +593,7 @@ func Test_TryOutError(t *testing.T) {
// let's test try.Out1() and it's throw capabilities here, even try.To1()
// is the preferred way.
retVal = try.Out1(noThrow()).Handle().Val1
test.Require(t, retVal == "test", "if no error happens, we get value")
require.Equal(t, retVal, "test", "if no error happens, we get value")

_ = try.Out1(throw()).Handle("fails: %v", retVal).Val1
t.Fail() // If everything works in Handle we are never here.
Expand Down Expand Up @@ -625,11 +625,11 @@ func TestCatch_Panic(t *testing.T) {
func TestSetErrorTracer(t *testing.T) {
t.Parallel()
w := err2.ErrorTracer()
test.Require(t, w == nil, "error tracer should be nil")
require.That(t, w == nil, "error tracer should be nil")
var w1 io.Writer
err2.SetErrorTracer(w1)
w = err2.ErrorTracer()
test.Require(t, w == nil, "error tracer should be nil")
require.That(t, w == nil, "error tracer should be nil")
}

func ExampleCatch_withFmt() {
Expand Down
Loading

0 comments on commit fa9e3e8

Please sign in to comment.