...
1 package emperror
2
3 import (
4 "errors"
5 "fmt"
6 "testing"
7 )
8
9 func TestPanic(t *testing.T) {
10 defer func() {
11 r := recover()
12 if r == nil {
13 t.Fatal("expected to recover from a panic, but nothing panicked")
14 }
15
16 err, ok := r.(error)
17 if !ok {
18 t.Fatal("expected to recover an error from a panic")
19 }
20
21 if err == nil {
22 t.Fatal("expected to the recovered error to be an error, received nil")
23 }
24
25 st, ok := StackTrace(err)
26 if !ok {
27 t.Fatal("error is expected to carry a stack trace")
28 }
29
30 if got, want := fmt.Sprintf("%n", st[0]), "TestPanic"; got != want {
31 t.Errorf("function name does not match the expected one\nactual: %s\nexpected: %s", got, want)
32 }
33
34 if got, want := fmt.Sprintf("%s", st[0]), "panic_test.go"; got != want {
35 t.Errorf("file name does not match the expected one\nactual: %s\nexpected: %s", got, want)
36 }
37
38 if got, want := fmt.Sprintf("%d", st[0]), "43"; got != want {
39 t.Errorf("line number does not match the expected one\nactual: %s\nexpected: %s", got, want)
40 }
41 }()
42
43 Panic(errors.New("error"))
44 }
45
46 func TestPanic_NoError(t *testing.T) {
47 defer func() {
48 r := recover()
49 if r != nil {
50 t.Fatalf("unexpected panic, received: %v", r)
51 }
52 }()
53
54 Panic(nil)
55 }
56
View as plain text