forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlines_test.go
138 lines (126 loc) · 3.73 KB
/
lines_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package errors
import (
"fmt"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLines(t *testing.T) {
tests := []struct {
lines []string
err error
}{
{[]string{}, nil},
{[]string{"foo"}, fmt.Errorf("foo")},
{[]string{"foo\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:18"}, New("foo")},
{[]string{
"bar\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:22",
"foo\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:22",
}, Wrap(New("foo"), "bar")},
{[]string{
"github.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:26",
"foo\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:26",
}, WithStack(New("foo"))},
{[]string{
"bar 1\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:31",
"github.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:31",
"foo\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:31",
}, Wrapf(WithStack(New("foo")), "bar %d", 1)},
{[]string{
"bar",
"github.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:36",
"foo\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:36",
}, WithMessage(WithStack(New("foo")), "bar")},
{[]string{
"bar 1",
"github.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:41",
"foo\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:41",
}, WithMessagef(WithStack(New("foo")), "bar %d", 1)},
{[]string{
"bar 1",
"github.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:46",
"foo 1\ngithub.com/pkg/errors.TestLines\t.+/github.com/pkg/errors/lines_test.go:46",
}, Wrapf(WithStack(Errorf("foo %d", 1)), "bar %d", 1)},
}
for _, tt := range tests {
got := Lines(tt.err, true)
matchLines(t, tt.lines, got)
}
}
func matchLines(t *testing.T, want []string, got []string) {
assert.Equal(t, len(want), len(got))
for i, w := range want {
match, err := regexp.MatchString(w, got[i])
assert.NoError(t, err, "regexp.MatchString(%q, %q)", w, got[i])
assert.True(t, match, "regexp.MatchString(%q, %q)", w, got[i])
}
}
type codeError struct {
Code int
Msg string
}
func NewCodeError(code int, msg string) *codeError {
return &codeError{Code: code, Msg: msg}
}
func (e *codeError) Error() string {
return fmt.Sprintf("code error: %d, %s", e.Code, e.Msg)
}
func TestLinesNoStack(t *testing.T) {
tests := []struct {
lines []string
err error
}{
{[]string{}, nil},
{[]string{"foo"}, fmt.Errorf("foo")},
{[]string{"foo"}, New("foo")},
{[]string{
"bar",
"foo",
}, Wrap(New("foo"), "bar")},
{[]string{
"foo",
}, WithStack(New("foo"))},
{[]string{
"bar 1",
"foo",
}, Wrapf(WithStack(New("foo")), "bar %d", 1)},
{[]string{
"bar",
"foo",
}, WithMessage(WithStack(New("foo")), "bar")},
{[]string{
"bar 1",
"foo",
}, WithMessagef(WithStack(New("foo")), "bar %d", 1)},
{[]string{
"bar 1",
"foo 1",
}, Wrapf(WithStack(Errorf("foo %d", 1)), "bar %d", 1)},
{[]string{
"code error: 1, foo",
}, NewCodeError(1, "foo")},
{[]string{
"bar",
"code error: 1, foo",
}, Wrap(NewCodeError(1, "foo"), "bar")},
}
for _, tt := range tests {
got := Lines(tt.err, false)
assert.Equal(t, tt.lines, got)
}
}
func TestLinesWithDetails(t *testing.T) {
tests := []struct {
err error
lines []string
}{
{WithDetails(nil), []string{}},
{WithDetails(nil, "whoops"), []string{}},
{WithDetails(New("foo"), "whoops", 1, 2.2), []string{"foo"}},
{WithDetails(Wrap(New("foo"), "bar"), "whoops", 1, 2.2), []string{"bar", "foo"}},
}
for _, tt := range tests {
got := Lines(tt.err, false)
assert.Equal(t, tt.lines, got)
}
}