-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
82 lines (71 loc) · 1.92 KB
/
util_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
80
81
82
package errors_test
import (
stderr "errors"
"testing"
"github.com/taxio/errors"
)
func TestBaseStackTrace(t *testing.T) {
t.Run("nil", func(t *testing.T) {
got := errors.BaseStackTrace(nil)
if len(got) != 0 {
t.Errorf("expected empty, got %v", got)
}
})
t.Run("other error", func(t *testing.T) {
got := errors.BaseStackTrace(stderr.New("test"))
if len(got) != 0 {
t.Errorf("expected empty, got %v", got)
}
})
t.Run("single error", func(t *testing.T) {
err := errors.New("test")
cErr := mustCast(t, err)
got := errors.BaseStackTrace(err)
if len(cErr.StackTrace()) != len(got) {
t.Fatalf("expected %d, got %d", len(cErr.StackTrace()), len(got))
}
for i, st := range cErr.StackTrace() {
if got[i] != st {
t.Errorf("expected %v, got %v", st, got[i])
}
}
})
t.Run("nested", func(t *testing.T) {
baseErr := errors.New("base")
cBaseErr := mustCast(t, baseErr)
wrapErr1 := errors.Wrap(baseErr)
wrapErr2 := errors.Wrap(wrapErr1)
got := errors.BaseStackTrace(wrapErr2)
if len(cBaseErr.StackTrace()) != len(got) {
t.Fatalf("expected %d, got %d", len(cBaseErr.StackTrace()), len(got))
}
for i, st := range cBaseErr.StackTrace() {
if got[i] != st {
t.Errorf("expected %v, got %v", st, got[i])
}
}
})
t.Run("nested with other base error", func(t *testing.T) {
baseErr := stderr.New("base")
wrapErr1 := errors.Wrap(baseErr)
cWrapErr1 := mustCast(t, wrapErr1)
wrapErr2 := errors.Wrap(wrapErr1)
got := errors.BaseStackTrace(wrapErr2)
if len(cWrapErr1.StackTrace()) != len(got) {
t.Fatalf("expected %d, got %d", len(cWrapErr1.StackTrace()), len(got))
}
for i, st := range cWrapErr1.StackTrace() {
if got[i] != st {
t.Errorf("expected %v, got %v", st, got[i])
}
}
})
}
func mustCast(t *testing.T, err error) *errors.Error {
t.Helper()
var cErr *errors.Error
if !errors.As(err, &cErr) {
t.Fatalf("expected error to be *errors.Error")
}
return cErr
}