Skip to content

Commit 8203321

Browse files
committed
x 初步确定
1 parent 7674e14 commit 8203321

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

x/runtime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func Caller() string {
1919
}
2020

2121
func Callers() []string {
22-
pcs := make([]uintptr, 16)
23-
n := runtime.Callers(2, pcs)
22+
var pcs [16]uintptr
23+
n := runtime.Callers(2, pcs[:])
2424
frames := runtime.CallersFrames(pcs[:n])
2525

2626
var callers []string

x/runtime_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99
)
1010

11+
// go test -v -run=^$ -bench=^BenchmarkCaller$ -benchtime=1s
1112
// BenchmarkCaller-2 1390318 876.7 ns/op 296 B/op 3 allocs/op
1213
func BenchmarkCaller(b *testing.B) {
1314
b.ReportAllocs()
@@ -18,6 +19,7 @@ func BenchmarkCaller(b *testing.B) {
1819
}
1920
}
2021

22+
// go test -v -run=^$ -bench=^BenchmarkCallers$ -benchtime=1s
2123
// BenchmarkCallers-2 443419 2573 ns/op 732 B/op 12 allocs/op
2224
func BenchmarkCallers(b *testing.B) {
2325
b.ReportAllocs()

x/unwrap.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,24 @@ func CodeMessage(err error, defaultCode int32, defaultMessage string) (int32, st
6060

6161
return code, message
6262
}
63+
64+
// Match unwraps error and check if its code equals to code.
65+
func Match(err error, code int32) bool {
66+
if err == nil {
67+
return code == 0
68+
}
69+
70+
cerr, ok := err.(interface {
71+
Code() int32
72+
})
73+
if ok {
74+
return cerr.Code() == code
75+
}
76+
77+
var xerr *Error
78+
if As(err, &xerr) {
79+
return xerr.Code() == code
80+
}
81+
82+
return false
83+
}

x/unwrap_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
// go test -v -cover -count=1 -test.cpu=1 -run=^TestCode$
1313
func TestCode(t *testing.T) {
14+
testErr := &testError{}
15+
1416
testCases := []struct {
1517
err error
1618
defaultCode int32
@@ -26,6 +28,11 @@ func TestCode(t *testing.T) {
2628
defaultCode: 999,
2729
code: 999,
2830
},
31+
{
32+
err: testErr,
33+
defaultCode: 999,
34+
code: testErr.Code(),
35+
},
2936
{
3037
err: Wrap(1000, "wow"),
3138
defaultCode: 999,
@@ -82,3 +89,52 @@ func TestMessage(t *testing.T) {
8289
}
8390
}
8491
}
92+
93+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatch$
94+
func TestMatch(t *testing.T) {
95+
testErr := &testError{}
96+
97+
testCases := []struct {
98+
err error
99+
code int32
100+
match bool
101+
}{
102+
{
103+
err: nil,
104+
code: 0,
105+
match: true,
106+
},
107+
{
108+
err: nil,
109+
code: 999,
110+
match: false,
111+
},
112+
{
113+
err: io.EOF,
114+
code: 999,
115+
match: false,
116+
},
117+
{
118+
err: testErr,
119+
code: testErr.Code(),
120+
match: true,
121+
},
122+
{
123+
err: Wrap(1000, "wow"),
124+
code: 1000,
125+
match: true,
126+
},
127+
{
128+
err: Wrap(1000, "eof").With(io.EOF),
129+
code: 1000,
130+
match: true,
131+
},
132+
}
133+
134+
for _, testCase := range testCases {
135+
match := Match(testCase.err, testCase.code)
136+
if match != testCase.match {
137+
t.Errorf("match %+v != testCase.match %+v", match, testCase.match)
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)