-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxdefer_test.go
79 lines (63 loc) · 1.53 KB
/
xdefer_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package xdefer_test
import (
"fmt"
"runtime"
"testing"
"golang.org/x/xerrors"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/xdefer"
)
func TestErrorf(t *testing.T) {
t.Parallel()
err := func() (err error) {
defer xdefer.Errorf(&err, "second wrap %#v", []int{99, 3})
err = xerrors.New("ola amigo")
if err != nil {
// This is the first line that should be reported on xdefer.
return xerrors.Errorf("first wrap: %w", err)
}
return nil
}()
_, fp, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
exp := fmt.Sprintf(`second wrap []int{99, 3}:
- first wrap:
oss.terrastruct.com/util-go/xdefer_test.TestErrorf.func1
%v:23
- ola amigo:
oss.terrastruct.com/util-go/xdefer_test.TestErrorf.func1
%[1]v:20`,
fp,
)
got := fmt.Sprintf("%+v", err)
assert.String(t, exp, got)
}
func TestEmptyErrorf(t *testing.T) {
t.Parallel()
err := func() (err error) {
defer xdefer.Errorf(&err, "")
err = xerrors.New("ola amigo")
if err != nil {
return err
}
return nil
}()
_, fp, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
exp := fmt.Sprintf(`oss.terrastruct.com/util-go/xdefer_test.TestEmptyErrorf.func1
%v:55
- ola amigo:
oss.terrastruct.com/util-go/xdefer_test.TestEmptyErrorf.func1
%[1]v:53`,
fp,
)
got := fmt.Sprintf("%+v", err)
assert.String(t, exp, got)
exp = err.Error()
got = "oss.terrastruct.com/util-go/xdefer_test.TestEmptyErrorf.func1: ola amigo"
assert.String(t, exp, got)
}