From 469e402d3522cb45b26497aa2d2366967c70f3e9 Mon Sep 17 00:00:00 2001 From: Levente Nas Date: Tue, 15 Oct 2024 22:15:10 +0200 Subject: [PATCH] feat(Testing): add assert panic and not panic --- testing/testing.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/testing/testing.go b/testing/testing.go index 4fad544..3eecec4 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -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!") + } +}