generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers_test.go
145 lines (132 loc) · 2.7 KB
/
helpers_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
139
140
141
142
143
144
145
package plex
import (
"context"
"errors"
"testing"
)
func Test_recoverErr(t *testing.T) {
testdata := map[string]struct {
value interface{}
underlying error
expected error
}{
"nil": {
value: nil,
expected: nil,
},
"string": {
value: "test error",
expected: errors.New("test error"),
},
"error": {
value: errors.New("test error"),
expected: errors.New("test error"),
},
"recover type proxy": {
value: 365,
expected: errors.New("panic: 365"),
},
"nil w/ underlying": {
value: nil,
expected: errors.New("underlying"),
underlying: errors.New("underlying"),
},
"string w/ underlying": {
value: "test error",
expected: errors.New("test error"),
underlying: errors.New("underlying"),
},
"error w/ underlying": {
value: errors.New("test error"),
expected: errors.New("test error"),
underlying: errors.New("underlying"),
},
"recover type proxy w/ underlying": {
value: 365,
expected: errors.New("panic: 365"),
underlying: errors.New("underlying"),
},
}
for name, test := range testdata {
t.Run(name, func(t *testing.T) {
err := recoverErr(test.underlying, test.value)
if err == nil && test.expected == nil {
return
}
if err.Error() != test.expected.Error() {
t.Errorf(
"expected %s, got %s",
test.expected.Error(),
err.Error(),
)
}
under, ok := err.(wrappedError)
if !ok {
if test.underlying != nil && test.value != nil {
t.Fatalf(
"expected %s, got %s",
test.underlying.Error(),
err.Error(),
)
}
return
}
if under.Unwrap() != test.underlying {
t.Fatalf(
"expected %s, got %s",
test.underlying.Error(),
under.Unwrap().Error(),
)
}
})
}
}
func Test_ctx(t *testing.T) {
testdata := map[string]struct {
ctx context.Context
}{
"non nil": {
context.Background(),
},
"nil": {
nil,
},
}
for name, test := range testdata {
t.Run(name, func(t *testing.T) {
ctx, cancel := _ctx(test.ctx)
if ctx == nil {
t.Fatal("expected non nil context")
}
if cancel == nil {
t.Fatal("expected non nil cancel")
}
})
}
}
func Test_merge(t *testing.T) {
testdata := map[string]struct {
parent context.Context
child context.Context
}{
"non-nil": {
parent: context.Background(),
child: context.TODO(),
},
"nil": {
parent: context.Background(),
child: nil,
},
}
for name, test := range testdata {
t.Run(name, func(t *testing.T) {
ctx := merge(test.parent, test.child)
if ctx == nil {
t.Fatal("expected non nil context")
}
if test.child != nil && ctx != test.child {
t.Fatal("expected child context")
}
})
}
}