-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_test.go
53 lines (47 loc) · 901 Bytes
/
do_test.go
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
package catch_test
import (
"errors"
"fmt"
"os"
"testing"
"github.com/tada/catch"
)
func ExampleDo() {
err := catch.Do(func() {
if err := os.Chmod("nosuchfile.noop", 0600); err != nil {
panic(catch.Error(err))
}
})
fmt.Println(err)
// Output: chmod nosuchfile.noop: no such file or directory
}
func TestCatch_normal(t *testing.T) {
e := errors.New("the error")
err := catch.Do(func() {
panic(catch.Error(e))
})
if err != e {
t.Errorf("expected %v, got %v", e, err)
}
}
func TestCatch_normalByValue(t *testing.T) {
e := errors.New("the error")
err := catch.Do(func() {
panic(catch.Error(e))
})
if err != e {
t.Errorf("expected %v, got %v", e, err)
}
}
func TestCatch_other(t *testing.T) {
e := errors.New("the error")
defer func() {
err := recover()
if err != e {
t.Errorf("expected %v, got %v", e, err)
}
}()
_ = catch.Do(func() {
panic(e)
})
}