-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrap_test.go
90 lines (83 loc) · 2.35 KB
/
wrap_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
package errors
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
type wrapTopTestCase struct {
about string
testCase []interface{}
expectedTopMessage string
}
var nilWrapError error = nil
func getWrapTopTestCases() []wrapTopTestCase {
return []wrapTopTestCase{
{
about: "wrapping all errors",
testCase: []interface{}{
New("500", "This is a very big error! Beware of it!"),
New("", "This is a very big error! Beware of it!"),
New("401", ""),
errors.New("error created from err package"),
fmt.Errorf("%s", "error created from fmt package"),
nil,
},
expectedTopMessage: "incorrect_usage: you should pass either error or message to properly wrap the error! - wrapped with unsupported type, it should be either of type error/string",
},
{
about: "wrapping all messages",
testCase: []interface{}{
"This is a very \"big\" error! Beware of it!",
"This is a very 'big' error! Beware of it!",
"This is a short error!",
"",
},
expectedTopMessage: "",
},
{
about: "wrapping errors and messages",
testCase: []interface{}{
New("500", "This is a very big error! Beware of it!"),
"This is a very \"big\" error! Beware of it!",
New("401", ""),
"This is a very 'big' error! Beware of it!",
"",
nil,
"This is a short error!",
New("", "This is a very big error! Beware of it!"),
},
expectedTopMessage: "This is a very big error! Beware of it!",
},
{
about: "wrapping error with nil error",
testCase: []interface{}{
New("500", "This is a very big error! Beware of it!"),
nil,
errors.New(""),
},
expectedTopMessage: "",
},
{
about: "wrapping error with nil error",
testCase: []interface{}{
New("500", "This is a very big error! Beware of it!"),
nil,
&nilWrapError,
},
expectedTopMessage: "incorrect_usage: you should pass either error or message to properly wrap the error! - wrapped with unsupported type, it should be either of type error/string",
},
}
}
func TestWrap(t *testing.T) {
for _, gtc := range getWrapTopTestCases() {
t.Run(gtc.about, func(t *testing.T) {
var wrappedError error
for _, tc := range gtc.testCase {
wrappedError = Wrap(wrappedError, tc)
}
require.Equal(t, len(gtc.testCase), len(strings.Split(wrappedError.Error(), "\n")))
})
}
}