This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package langext
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunPanicSafeNoPanic(t *testing.T) {
|
||||
called := false
|
||||
err := RunPanicSafe(func() {
|
||||
called = true
|
||||
})
|
||||
tst.AssertEqual(t, called, true)
|
||||
if err != nil {
|
||||
t.Errorf("expected nil err, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPanicSafeRecovers(t *testing.T) {
|
||||
err := RunPanicSafe(func() {
|
||||
panic("boom")
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatalf("expected error from panic")
|
||||
}
|
||||
pwe, ok := err.(PanicWrappedErr)
|
||||
if !ok {
|
||||
t.Fatalf("expected PanicWrappedErr, got %T", err)
|
||||
}
|
||||
tst.AssertEqual(t, pwe.RecoveredObj(), "boom")
|
||||
tst.AssertEqual(t, pwe.Error(), "A panic occured")
|
||||
}
|
||||
|
||||
func TestRunPanicSafeR1NoPanic(t *testing.T) {
|
||||
expected := errors.New("expected")
|
||||
err := RunPanicSafeR1(func() error {
|
||||
return expected
|
||||
})
|
||||
if err != expected {
|
||||
t.Errorf("expected original error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPanicSafeR1Panics(t *testing.T) {
|
||||
err := RunPanicSafeR1(func() error {
|
||||
panic("boom")
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatalf("expected wrapped panic")
|
||||
}
|
||||
if _, ok := err.(PanicWrappedErr); !ok {
|
||||
t.Errorf("expected PanicWrappedErr, got %T", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPanicSafeR2NoPanic(t *testing.T) {
|
||||
v, err := RunPanicSafeR2(func() (int, error) {
|
||||
return 42, nil
|
||||
})
|
||||
tst.AssertEqual(t, v, 42)
|
||||
if err != nil {
|
||||
t.Errorf("expected nil err, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPanicSafeR2Panics(t *testing.T) {
|
||||
v, err := RunPanicSafeR2(func() (int, error) {
|
||||
panic("boom")
|
||||
})
|
||||
tst.AssertEqual(t, v, 0) // zero value
|
||||
if err == nil {
|
||||
t.Errorf("expected wrapped panic")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPanicSafeR3NoPanic(t *testing.T) {
|
||||
a, b, err := RunPanicSafeR3(func() (int, string, error) {
|
||||
return 1, "two", nil
|
||||
})
|
||||
tst.AssertEqual(t, a, 1)
|
||||
tst.AssertEqual(t, b, "two")
|
||||
if err != nil {
|
||||
t.Errorf("expected nil err, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPanicSafeR3Panics(t *testing.T) {
|
||||
a, b, err := RunPanicSafeR3(func() (int, string, error) {
|
||||
panic("boom")
|
||||
})
|
||||
tst.AssertEqual(t, a, 0)
|
||||
tst.AssertEqual(t, b, "")
|
||||
if err == nil {
|
||||
t.Errorf("expected wrapped panic")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPanicSafeR4NoPanic(t *testing.T) {
|
||||
a, b, c, err := RunPanicSafeR4(func() (int, string, bool, error) {
|
||||
return 1, "two", true, nil
|
||||
})
|
||||
tst.AssertEqual(t, a, 1)
|
||||
tst.AssertEqual(t, b, "two")
|
||||
tst.AssertEqual(t, c, true)
|
||||
if err != nil {
|
||||
t.Errorf("expected nil err, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPanicSafeR4Panics(t *testing.T) {
|
||||
a, b, c, err := RunPanicSafeR4(func() (int, string, bool, error) {
|
||||
panic("boom")
|
||||
})
|
||||
tst.AssertEqual(t, a, 0)
|
||||
tst.AssertEqual(t, b, "")
|
||||
tst.AssertEqual(t, c, false)
|
||||
if err == nil {
|
||||
t.Errorf("expected wrapped panic")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user