-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassert_test.go
326 lines (266 loc) · 6.99 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package assert
import (
"strings"
"testing"
)
func TestAssert_SuccessCase(t *testing.T) {
t.Parallel()
Assert(true, "this should not panic")
}
func TestAssert_BasicFailure(t *testing.T) {
t.Parallel()
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic but got none")
}
err, ok := r.(AssertionError)
if !ok {
t.Fatalf("expected AssertionError but got %T", r)
}
// Check basic error properties
if err.Message != "basic failure message" {
t.Errorf("expected message 'basic failure message' but got '%s'", err.Message)
}
// Only verify that line number exists and is positive
if err.Line <= 0 {
t.Error("expected positive line number")
}
// Only verify file name format, not specific line
if !strings.HasSuffix(err.File, "_test.go") {
t.Errorf("expected file name to end with _test.go, got %s", err.File)
}
// Verify source context exists and contains the failure line
if !strings.Contains(err.SourceContext, "Assert(false, \"basic failure message\")") {
t.Error("expected source context to contain the failing assertion")
}
}()
Assert(false, "basic failure message")
}
func TestConfig_DisableSourceContext(t *testing.T) {
t.Parallel()
// Save original config and restore after test
originalConfig := activeConfig
defer func() {
SetConfig(originalConfig)
}()
// Disable source context
SetConfig(Config{
IncludeSource: false,
ContextLines: 5,
})
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic but got none")
}
err, ok := r.(AssertionError)
if !ok {
t.Fatalf("expected AssertionError but got %T", r)
}
// Source context should be empty when disabled
if err.SourceContext != "" {
t.Error("expected empty source context when disabled")
}
}()
Assert(false, "failure with disabled source")
}
func TestConfig_CustomContextLines(t *testing.T) {
t.Parallel()
// Save original config and restore after test
originalConfig := activeConfig
defer func() {
SetConfig(originalConfig)
}()
// Set custom context lines
customLines := 2
SetConfig(Config{
IncludeSource: true,
ContextLines: customLines,
})
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic but got none")
}
err, ok := r.(AssertionError)
if !ok {
t.Fatalf("expected AssertionError but got %T", r)
}
// Count the number of lines in source context
lines := strings.Count(err.SourceContext, "\n") + 1
expectedLines := customLines*2 + 1 // context before + current line + context after
if lines != expectedLines {
t.Errorf("expected %d lines of context, got %d", expectedLines, lines)
}
}()
Assert(false, "failure with custom context lines")
}
func TestAssert_WithValues(t *testing.T) {
t.Parallel()
type testStruct struct {
Field string
}
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic but got none")
}
err, ok := r.(AssertionError)
if !ok {
t.Fatalf("expected AssertionError but got %T", r)
}
expectedValues := []string{
"[string_key]: \"string_value\"",
"[int_key]: 42",
"[struct_key]:",
}
for _, expected := range expectedValues {
if !strings.Contains(err.Message, expected) {
t.Errorf("expected message to contain '%s'", expected)
}
}
// Verify source context exists and contains the failure line
if !strings.Contains(err.SourceContext, "Assert(false, \"failure with values\"") {
t.Error("expected source context to contain the failing assertion")
}
}()
Assert(false, "failure with values",
"string_key", "string_value",
"int_key", 42,
"struct_key", testStruct{Field: "value"},
)
}
func TestAssert_OddNumberOfValues(t *testing.T) {
t.Parallel()
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic but got none")
}
err, ok := r.(AssertionError)
if !ok {
t.Fatalf("expected AssertionError but got %T", r)
}
if !strings.Contains(err.Message, "(MISSING)") {
t.Error("expected message to contain (MISSING) for odd number of values")
}
// Verify source context exists
if err.SourceContext == "" {
t.Error("expected non-empty source context")
}
}()
Assert(false, "odd values",
"key1", "value1",
"key2", // Missing value
)
}
func TestAssertionError_Error(t *testing.T) {
t.Parallel()
err := AssertionError{
Message: "test message",
File: "test_file.go",
Line: 42,
SourceContext: " 41 | func TestExample(t *testing.T) {\n" +
"→ 42 | \tAssert(false, \"test message\")\n" +
" 43 | }",
}
errStr := err.Error()
expectedParts := []string{
"test_file.go:42",
"test message",
"Source context:",
"→ 42",
}
for _, part := range expectedParts {
if !strings.Contains(errStr, part) {
t.Errorf("expected error string to contain '%s'", part)
}
}
}
func TestAssert_NilValues(t *testing.T) {
t.Parallel()
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic but got none")
}
err, ok := r.(AssertionError)
if !ok {
t.Fatalf("expected AssertionError but got %T", r)
}
if !strings.Contains(err.Message, "[nil_key]: <nil>") {
t.Error("expected message to handle nil values correctly")
}
// Verify source context exists
if err.SourceContext == "" {
t.Error("expected non-empty source context")
}
}()
Assert(false, "nil value test",
"nil_key", nil,
)
}
func TestAssert_EmptyValues(t *testing.T) {
t.Parallel()
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic but got none")
}
err, ok := r.(AssertionError)
if !ok {
t.Fatalf("expected AssertionError but got %T", r)
}
expectedValues := []string{
"[empty_string]: \"\"",
"[empty_slice]: []string{}",
"[empty_map]: map[string]int{}",
}
for _, expected := range expectedValues {
if !strings.Contains(err.Message, expected) {
t.Errorf("expected message to contain '%s'", expected)
}
}
// Verify source context exists
if err.SourceContext == "" {
t.Error("expected non-empty source context")
}
}()
Assert(false, "empty values test",
"empty_string", "",
"empty_slice", []string{},
"empty_map", map[string]int{},
)
}
func TestAssertCallerFailure(t *testing.T) {
assertMessage := "This should fail to get caller info"
// Capture and verify the panic
defer func() {
r := recover()
if r == nil {
t.Fatal("Expected panic, but none occurred")
}
ae, ok := r.(AssertionError)
if !ok {
t.Fatalf("Expected AssertionError, got %T", r)
}
// Verify we got the simplified error without file/line info
if ae.File != "" {
t.Errorf("Expected empty file, got %q", ae.File)
}
if ae.Line != 0 {
t.Errorf("Expected line to be 0, got %d", ae.Line)
}
if ae.SourceContext != "" {
t.Errorf("Expected empty source context, got %q", ae.SourceContext)
}
// Message should still be included
if ae.Message != assertMessage {
t.Errorf("Expected %q as error message, got %q", assertMessage, ae.Message)
}
}()
// Using the assert function (only accessible internally) instead of Assert
// to pass a specific skipFrames value
assert(false, assertMessage, 1000)
}