-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutils.go
137 lines (117 loc) · 3.16 KB
/
testutils.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
package testutils
import (
"bytes"
"io"
"os"
"reflect"
"strings"
"testing"
"github.com/jimsnab/go-simpleutils"
)
var escape = simpleutils.Escape
func ExpectPanic(t *testing.T, fn func()) {
t.Helper()
defer func() {
t.Helper()
r := recover()
if r == nil {
t.Errorf("Did not get expected panic")
}
}()
fn()
}
func ExpectPanicError(t *testing.T, expectedError error, fn func()) {
t.Helper()
defer func() {
t.Helper()
r := recover()
if r == nil {
if expectedError != nil {
t.Errorf("Did not get expected panic. Type expected: %T", expectedError)
}
} else {
if reflect.TypeOf(r) != reflect.TypeOf(expectedError) {
t.Errorf("Got error type \"%s\" but expected \"%s\"", reflect.TypeOf(r), reflect.TypeOf(expectedError))
} else {
testError := r.(error)
if expectedError.Error() != testError.Error() {
t.Errorf("Got %T \"%v\" but expected \"%v\"", testError, testError, expectedError)
}
}
}
}()
fn()
}
func DoMapsMatch(t *testing.T, expectedMap map[string]interface{}, testMap map[string]interface{}) {
t.Helper()
if !reflect.DeepEqual(expectedMap, testMap) {
t.Errorf("Did not get expected %v, got %v", expectedMap, testMap)
}
}
func ExpectError(t *testing.T, expectedError error, testError error) {
t.Helper()
if expectedError == nil {
if testError != nil {
t.Errorf("Got unexpected %T: \"%v\"", testError, testError)
}
} else {
if testError == nil {
t.Errorf("Did not get expected %T: \"%v\"", expectedError, expectedError)
} else if reflect.TypeOf(expectedError) != reflect.TypeOf(testError) {
t.Errorf("Got error type \"%s\" but \"%s\" was expected", reflect.TypeOf(testError), reflect.TypeOf(expectedError))
} else if expectedError.Error() != testError.Error() {
t.Errorf("Got %T\n \"%v\"\nbut\n \"%v\"\nwas expected", testError, testError, expectedError)
}
}
}
func ExpectErrorContainingText(t *testing.T, expectedSubtext string, testError error) {
t.Helper()
if testError == nil {
t.Errorf("Did not get expected error")
} else if !strings.Contains(testError.Error(), expectedSubtext) {
t.Errorf("Got %T\n \"%v\"\nbut\n \"%s\"\nwas expected", testError, testError, expectedSubtext)
}
}
func ExpectBool(t *testing.T, expectedBool bool, testBool bool) {
t.Helper()
if expectedBool != testBool {
t.Errorf("Got %v but %v expected", testBool, expectedBool)
}
}
func ExpectString(t *testing.T, expectedStr string, testStr string) {
t.Helper()
if expectedStr != testStr {
t.Errorf("Got\n \"%s\"\nbut\n \"%s\"\nwas expected", escape(testStr), escape(expectedStr))
}
}
func ExpectValue(t *testing.T, expectedVal interface{}, testVal interface{}) {
t.Helper()
if expectedVal != testVal {
t.Errorf("Got %v but %v expected", testVal, expectedVal)
}
}
func CaptureStdout(t *testing.T, fn func()) string {
orgStdout := os.Stdout
defer func() { os.Stdout = orgStdout }()
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
return ""
}
os.Stdout = w
output := make(chan string)
go func() {
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
if err != nil {
t.Error(err)
output <- ""
} else {
output <- buf.String()
}
}()
fn()
w.Close()
result := <-output
return result
}