From 3078b5c82ccefbefa12e88065fc13362ae91871f Mon Sep 17 00:00:00 2001 From: hovsep Date: Wed, 18 Sep 2024 01:43:47 +0300 Subject: [PATCH] Tests: refactor tests expecting for panic --- signal/group_test.go | 19 +++++++------------ signal/signal_test.go | 18 +++--------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/signal/group_test.go b/signal/group_test.go index f80a2cf..fc35e01 100644 --- a/signal/group_test.go +++ b/signal/group_test.go @@ -74,18 +74,13 @@ func TestGroup_FirstPayload(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - defer func() { - r := recover() - if tt.wantPanic && r == nil { - t.Errorf("The code did not panic") - } - - if !tt.wantPanic && r != nil { - t.Errorf("The code unexpectedly paniced") - } - }() - - assert.Equal(t, tt.want, tt.group.FirstPayload()) + if tt.wantPanic { + assert.Panics(t, func() { + tt.group.FirstPayload() + }) + } else { + assert.Equal(t, tt.want, tt.group.FirstPayload()) + } }) } } diff --git a/signal/signal_test.go b/signal/signal_test.go index 5438411..a520683 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -42,10 +42,9 @@ func TestNew(t *testing.T) { func TestSignal_Payload(t *testing.T) { tests := []struct { - name string - signal *Signal - want any - wantPanic bool + name string + signal *Signal + want any }{ { name: "nil payload is valid", @@ -60,17 +59,6 @@ func TestSignal_Payload(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - defer func() { - r := recover() - if tt.wantPanic && r == nil { - t.Errorf("The code did not panic") - } - - if !tt.wantPanic && r != nil { - t.Errorf("The code unexpectedly paniced") - } - }() - assert.Equal(t, tt.want, tt.signal.Payload()) }) }