forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_test.go
246 lines (204 loc) · 7.84 KB
/
assert_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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"fmt"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
)
func assertNilE(t *testing.T, actual any, descriptions ...string) {
errorOnNonEmpty(t, validateNil(actual, descriptions...))
}
func assertNilF(t *testing.T, actual any, descriptions ...string) {
fatalOnNonEmpty(t, validateNil(actual, descriptions...))
}
func assertNotNilE(t *testing.T, actual any, descriptions ...string) {
errorOnNonEmpty(t, validateNotNil(actual, descriptions...))
}
func assertNotNilF(t *testing.T, actual any, descriptions ...string) {
fatalOnNonEmpty(t, validateNotNil(actual, descriptions...))
}
func assertEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
errorOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
}
func assertEqualF(t *testing.T, actual any, expected any, descriptions ...string) {
fatalOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
}
func assertEqualIgnoringWhitespaceE(t *testing.T, actual string, expected string, descriptions ...string) {
errorOnNonEmpty(t, validateEqualIgnoringWhitespace(actual, expected, descriptions...))
}
func assertDeepEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
errorOnNonEmpty(t, validateDeepEqual(actual, expected, descriptions...))
}
func assertNotEqualF(t *testing.T, actual any, expected any, descriptions ...string) {
fatalOnNonEmpty(t, validateNotEqual(actual, expected, descriptions...))
}
func assertBytesEqualE(t *testing.T, actual []byte, expected []byte, descriptions ...string) {
errorOnNonEmpty(t, validateBytesEqual(actual, expected, descriptions...))
}
func assertTrueF(t *testing.T, actual bool, descriptions ...string) {
fatalOnNonEmpty(t, validateEqual(actual, true, descriptions...))
}
func assertTrueE(t *testing.T, actual bool, descriptions ...string) {
errorOnNonEmpty(t, validateEqual(actual, true, descriptions...))
}
func assertFalseF(t *testing.T, actual bool, descriptions ...string) {
fatalOnNonEmpty(t, validateEqual(actual, false, descriptions...))
}
func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
errorOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
}
func assertStringContainsF(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
fatalOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
}
func assertEmptyStringE(t *testing.T, actual string, descriptions ...string) {
errorOnNonEmpty(t, validateEmptyString(actual, descriptions...))
}
func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) {
errorOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...))
}
func assertBetweenE(t *testing.T, value float64, min float64, max float64, descriptions ...string) {
errorOnNonEmpty(t, validateValueBetween(value, min, max, descriptions...))
}
func assertBetweenInclusiveE(t *testing.T, value float64, min float64, max float64, descriptions ...string) {
errorOnNonEmpty(t, validateValueBetweenInclusive(value, min, max, descriptions...))
}
func assertEmptyE[T any](t *testing.T, actual []T, descriptions ...string) {
errorOnNonEmpty(t, validateEmpty(actual, descriptions...))
}
func fatalOnNonEmpty(t *testing.T, errMsg string) {
if errMsg != "" {
t.Fatal(formatErrorMessage(errMsg))
}
}
func errorOnNonEmpty(t *testing.T, errMsg string) {
if errMsg != "" {
t.Error(formatErrorMessage(errMsg))
}
}
func formatErrorMessage(errMsg string) string {
return fmt.Sprintf("%s. Thrown from %s", errMsg, thrownFrom())
}
func validateNil(actual any, descriptions ...string) string {
if isNil(actual) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be nil but was not. %s", actual, desc)
}
func validateNotNil(actual any, descriptions ...string) string {
if !isNil(actual) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected to be not nil but was not. %s", desc)
}
func validateEqual(actual any, expected any, descriptions ...string) string {
if expected == actual {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc)
}
func removeWhitespaces(s string) string {
pattern, err := regexp.Compile(`\s+`)
if err != nil {
panic(err)
}
return pattern.ReplaceAllString(s, "")
}
func validateEqualIgnoringWhitespace(actual string, expected string, descriptions ...string) string {
if removeWhitespaces(expected) == removeWhitespaces(actual) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc)
}
func validateDeepEqual(actual any, expected any, descriptions ...string) string {
if reflect.DeepEqual(actual, expected) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc)
}
func validateNotEqual(actual any, expected any, descriptions ...string) string {
if expected != actual {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" not to be equal to \"%s\" but they were the same. %s", actual, expected, desc)
}
func validateBytesEqual(actual []byte, expected []byte, descriptions ...string) string {
if bytes.Equal(actual, expected) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc)
}
func validateStringContains(actual string, expectedToContain string, descriptions ...string) string {
if strings.Contains(actual, expectedToContain) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s", actual, expectedToContain, desc)
}
func validateEmptyString(actual string, descriptions ...string) string {
if actual == "" {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be empty, but was not. %s", actual, desc)
}
func validateHasPrefix(actual string, expectedPrefix string, descriptions ...string) string {
if strings.HasPrefix(actual, expectedPrefix) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s", actual, expectedPrefix, desc)
}
func validateValueBetween(value float64, min float64, max float64, descriptions ...string) string {
if value > min && value < max {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%f\" should be between \"%f\" and \"%f\" but did not. %s", value, min, max, desc)
}
func validateValueBetweenInclusive(value float64, min float64, max float64, descriptions ...string) string {
if value >= min && value <= max {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%f\" should be between \"%f\" and \"%f\" inclusively but did not. %s", value, min, max, desc)
}
func validateEmpty[T any](value []T, descriptions ...string) string {
if len(value) == 0 {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%v\" to be empty. %s", value, desc)
}
func joinDescriptions(descriptions ...string) string {
return strings.Join(descriptions, " ")
}
func isNil(value any) bool {
if value == nil {
return true
}
val := reflect.ValueOf(value)
return val.Kind() == reflect.Pointer && val.IsNil()
}
func thrownFrom() string {
buf := make([]byte, 1024)
size := runtime.Stack(buf, false)
stack := string(buf[0:size])
lines := strings.Split(stack, "\n\t")
for i, line := range lines {
if i > 0 && !strings.Contains(line, "assert_test.go") {
return line
}
}
return stack
}