feat(Testing): add assert panic and not panic

This commit is contained in:
Levente Nas
2024-10-15 22:15:10 +02:00
committed by Levente Batuska
parent 2e035a6336
commit 65e14cc4db

View File

@@ -73,3 +73,33 @@ func AssertNotNil[T any](t *testing.T, expected *T) {
}
t.Errorf("❌ [%T](%+v) != [%T](%+v)", expected, expected, nil, nil)
}
func internalAssertPanic(f func()) (b bool) {
b = true
defer func() {
_ = recover()
}()
f()
return false
}
func AssertPanic(t *testing.T, f func()) {
t.Helper()
x := internalAssertPanic(f)
if x {
t.Log("✅ Code correctly panicked!")
} else {
t.Error("❌ Code was supposed to panic!")
}
}
func AssertNotPanic(t *testing.T, f func()) {
t.Helper()
x := internalAssertPanic(f)
if !x {
t.Log("✅ Code correctly did not panic!")
} else {
t.Error("❌ Code panicked!")
}
}