-
Notifications
You must be signed in to change notification settings - Fork 278
/
assertions.go
208 lines (157 loc) · 4.93 KB
/
assertions.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package testhelpers
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
type AssertionManager struct {
testObject *testing.T
}
func NewAssertionManager(testObject *testing.T) AssertionManager {
return AssertionManager{
testObject: testObject,
}
}
func (a AssertionManager) TrimmedEq(actual, expected string) {
a.testObject.Helper()
actualLines := strings.Split(actual, "\n")
expectedLines := strings.Split(expected, "\n")
for lineIdx, line := range actualLines {
actualLines[lineIdx] = strings.TrimRight(line, "\t \n")
}
for lineIdx, line := range expectedLines {
expectedLines[lineIdx] = strings.TrimRight(line, "\t \n")
}
actualTrimmed := strings.Join(actualLines, "\n")
expectedTrimmed := strings.Join(expectedLines, "\n")
a.Equal(actualTrimmed, expectedTrimmed)
}
func (a AssertionManager) AssertTrimmedContains(actual, expected string) {
a.testObject.Helper()
actualLines := strings.Split(actual, "\n")
expectedLines := strings.Split(expected, "\n")
for lineIdx, line := range actualLines {
actualLines[lineIdx] = strings.TrimRight(line, "\t \n")
}
for lineIdx, line := range expectedLines {
expectedLines[lineIdx] = strings.TrimRight(line, "\t \n")
}
actualTrimmed := strings.Join(actualLines, "\n")
expectedTrimmed := strings.Join(expectedLines, "\n")
a.Contains(actualTrimmed, expectedTrimmed)
}
func (a AssertionManager) Equal(actual, expected interface{}) {
a.testObject.Helper()
if diff := cmp.Diff(actual, expected); diff != "" {
a.testObject.Fatalf(diff)
}
}
func (a AssertionManager) NotEqual(actual, expected interface{}) {
a.testObject.Helper()
if diff := cmp.Diff(actual, expected); diff == "" {
a.testObject.Fatalf(diff)
}
}
func (a AssertionManager) Nil(actual interface{}) {
a.testObject.Helper()
if !isNil(actual) {
a.testObject.Fatalf("expected nil: %v", actual)
}
}
func (a AssertionManager) Succeeds(actual interface{}) {
a.testObject.Helper()
a.Nil(actual)
}
func (a AssertionManager) Fails(actual interface{}) {
a.testObject.Helper()
a.NotNil(actual)
}
func (a AssertionManager) NilWithMessage(actual interface{}, message string) {
a.testObject.Helper()
if !isNil(actual) {
a.testObject.Fatalf("expected nil: %s: %s", actual, message)
}
}
func (a AssertionManager) NotNil(actual interface{}) {
a.testObject.Helper()
if isNil(actual) {
a.testObject.Fatal("expect not nil")
}
}
func (a AssertionManager) Contains(actual, expected string) {
a.testObject.Helper()
if !strings.Contains(actual, expected) {
a.testObject.Fatalf(
"Expected '%s' to contain '%s'\n\nDiff:%s",
actual,
expected,
cmp.Diff(expected, actual),
)
}
}
func (a AssertionManager) ContainsF(actual, expected string, formatArgs ...interface{}) {
a.testObject.Helper()
a.Contains(actual, fmt.Sprintf(expected, formatArgs...))
}
// ContainsWithMessage will fail if expected is not contained within actual, messageFormat will be printed as the
// failure message, with actual interpolated in the message
func (a AssertionManager) ContainsWithMessage(actual, expected, messageFormat string) {
a.testObject.Helper()
if !strings.Contains(actual, expected) {
a.testObject.Fatalf(messageFormat, actual)
}
}
func (a AssertionManager) ContainsAll(actual string, expected ...string) {
a.testObject.Helper()
for _, e := range expected {
a.Contains(actual, e)
}
}
func (a AssertionManager) Matches(actual string, pattern *regexp.Regexp) {
a.testObject.Helper()
if !pattern.MatchString(actual) {
a.testObject.Fatalf("Expected '%s' to match regex '%s'", actual, pattern)
}
}
func (a AssertionManager) NoMatches(actual string, pattern *regexp.Regexp) {
a.testObject.Helper()
if pattern.MatchString(actual) {
a.testObject.Fatalf("Expected '%s' not to match regex '%s'", actual, pattern)
}
}
func (a AssertionManager) MatchesAll(actual string, patterns ...*regexp.Regexp) {
a.testObject.Helper()
for _, pattern := range patterns {
a.Matches(actual, pattern)
}
}
func (a AssertionManager) NotContains(actual, expected string) {
a.testObject.Helper()
if strings.Contains(actual, expected) {
a.testObject.Fatalf("Expected '%s' not to be in '%s'", expected, actual)
}
}
// NotContainWithMessage will fail if expected is contained within actual, messageFormat will be printed as the failure
// message, with actual interpolated in the message
func (a AssertionManager) NotContainWithMessage(actual, expected, messageFormat string) {
a.testObject.Helper()
if strings.Contains(actual, expected) {
a.testObject.Fatalf(messageFormat, actual)
}
}
// Error checks that the provided value is an error (non-nil)
func (a AssertionManager) Error(actual error) {
a.testObject.Helper()
if actual == nil {
a.testObject.Fatal("Expected an error but got nil")
}
}
func (a AssertionManager) ErrorContains(actual error, expected string) {
a.testObject.Helper()
if actual == nil {
a.testObject.Fatalf("Expected %q an error but got nil", expected)
}
a.Contains(actual.Error(), expected)
}