-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanic_test.go
More file actions
58 lines (48 loc) · 1.03 KB
/
panic_test.go
File metadata and controls
58 lines (48 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package concurrent
import (
"testing"
)
func TestRecoverAndRethrow(t *testing.T) {
t.Run("captures panic value", func(t *testing.T) {
var captured any
func() {
defer recoverAndRethrow(func(v any) {
captured = v
})
panic("test panic")
}()
if captured == nil {
t.Fatal("expected panic to be captured")
}
if captured != "test panic" {
t.Errorf("captured value = %v, want %q", captured, "test panic")
}
})
t.Run("captures non-string panic value", func(t *testing.T) {
var captured any
func() {
defer recoverAndRethrow(func(v any) {
captured = v
})
panic(42)
}()
if captured == nil {
t.Fatal("expected panic to be captured")
}
if captured != 42 {
t.Errorf("captured value = %v, want 42", captured)
}
})
t.Run("does not call handler when no panic", func(t *testing.T) {
called := false
func() {
defer recoverAndRethrow(func(v any) {
called = true
})
// No panic here
}()
if called {
t.Error("handler should not be called when no panic occurs")
}
})
}