-
Notifications
You must be signed in to change notification settings - Fork 59
/
utils_test.go
76 lines (71 loc) · 1.86 KB
/
utils_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
package main
import (
"fmt"
"testing"
)
// ShouldPanic
func ShouldPanic(t *testing.T, f func(), expectedErrorMessage string, errMsg string) {
t.Helper()
defer func() {
err := recover()
if err.(error).Error() != expectedErrorMessage {
t.Errorf(errMsg)
}
}()
f()
t.Errorf(errMsg)
}
// ShouldNotPanic
func ShouldNotPanic(t *testing.T, f func(), errMsg string) {
t.Helper()
defer func() {
err := recover()
if err != nil {
t.Errorf(errMsg)
}
}()
f()
}
func TestPanicOnErrors(t *testing.T) {
type TestCase struct {
Errors []error
IsPanicExpected bool
PanicErrMsg string
FailMsg string
}
tests := []TestCase{
{
Errors: []error{},
IsPanicExpected: false,
FailMsg: "should not panic when no errors provided",
},
{
Errors: []error{fmt.Errorf("single Error")},
IsPanicExpected: true,
PanicErrMsg: "single Error",
FailMsg: "For a single error, should panic with specific message",
},
{
Errors: []error{nil, fmt.Errorf("single Error")},
IsPanicExpected: true,
PanicErrMsg: "single Error",
FailMsg: "should panic if input contains nil errors",
},
{
Errors: []error{fmt.Errorf("single Error"), fmt.Errorf("one more")},
IsPanicExpected: true,
PanicErrMsg: "Multiple errors occured. Check output for details",
FailMsg: "For multiple errors, should provide generic message",
},
}
fmt.Println("The following output may contain error messages. These are expected")
for _, test := range tests {
if test.IsPanicExpected {
ShouldPanic(t, func() { PanicOnErrors(test.Errors) }, test.PanicErrMsg, test.FailMsg)
} else {
ShouldNotPanic(t, func() { PanicOnErrors(test.Errors) }, test.FailMsg)
}
}
// Separate out expected error messages from rest of testing output
fmt.Printf("End of expected errors\n\n")
}