-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_test.go
More file actions
374 lines (293 loc) · 10.4 KB
/
assert_test.go
File metadata and controls
374 lines (293 loc) · 10.4 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package assert
import (
"bytes"
"context"
"testing"
)
func TestAssert(t *testing.T) {
var buffer bytes.Buffer
handler := NewAssertHandler()
handler.ToWriter(&buffer)
handler.SetExitFunc(func(code int) {}) // Prevent exit
handler.Assert(context.TODO(), false, "Test Failure")
if !bytes.Contains(buffer.Bytes(), []byte("Test Failure")) {
t.Fatalf("Expected failure message not found in output")
}
}
func TestCustomExitFunc(t *testing.T) {
var buffer bytes.Buffer
handler := NewAssertHandler()
handler.ToWriter(&buffer)
handler.SetExitFunc(func(code int) {})
called := false
handler.SetExitFunc(func(code int) {
called = true // Track if the custom exit is called
})
handler.Assert(context.TODO(), false, "Test Failure")
if !called {
t.Fatalf("Custom exit function was not called")
}
if !bytes.Contains(buffer.Bytes(), []byte("Test Failure")) {
t.Fatalf("Expected failure message not found in output")
}
}
func TestJSONFormatter(t *testing.T) {
var buffer bytes.Buffer
handler := NewAssertHandler()
handler.ToWriter(&buffer)
handler.SetExitFunc(func(code int) {})
handler.SetFormatter(&JSONFormatter{})
handler.Assert(context.TODO(), false, "Test Failure")
expected := `"msg": "Test Failure"`
if !bytes.Contains(buffer.Bytes(), []byte(expected)) {
t.Fatalf("Expected JSON output not found in output")
}
}
func TestYAMLFormatter(t *testing.T) {
var buffer bytes.Buffer
handler := NewAssertHandler()
handler.ToWriter(&buffer)
handler.SetExitFunc(func(code int) {})
handler.SetFormatter(&YAMLFormatter{})
handler.Assert(context.TODO(), false, "Test Failure")
expected := "msg: Test Failure"
if !bytes.Contains(buffer.Bytes(), []byte(expected)) {
t.Fatalf("Expected YAML output not found in output")
}
}
func TestDeferredAssertions(t *testing.T) {
var buffer bytes.Buffer
handler := NewAssertHandler()
handler.ToWriter(&buffer)
handler.SetExitFunc(func(code int) {})
// Enable deferred assertions
handler.SetDeferAssertions(true)
// These should not be printed immediately
handler.NotNil(context.TODO(), nil, "Test Deferred NotNil") // This will fail since nil is passed
handler.Assert(context.TODO(), false, "Test Deferred Assert")
// Process deferred assertions
handler.ProcessDeferredAssertions(context.TODO())
if !bytes.Contains(buffer.Bytes(), []byte("Test Deferred Assert")) {
t.Fatalf("Expected deferred assertion message not found")
}
if !bytes.Contains(buffer.Bytes(), []byte("Test Deferred NotNil")) {
t.Fatalf("Expected deferred NotNil message not found")
}
}
func TestImmediateAssertions(t *testing.T) {
var buffer bytes.Buffer
handler := NewAssertHandler()
handler.ToWriter(&buffer)
handler.SetExitFunc(func(code int) {})
// Disable deferred assertions (default)
handler.Assert(context.TODO(), false, "Test Immediate Assert")
if !bytes.Contains(buffer.Bytes(), []byte("Test Immediate Assert")) {
t.Fatalf("Expected immediate assertion message not found")
}
}
func TestPackageLevelAssert(t *testing.T) {
var buffer bytes.Buffer
// Test package-level function with custom writer
Assert(context.TODO(), false, "Package level assertion failed",
WithWriter(&buffer),
WithExitFunc(func(code int) {})) // Prevent exit
if !bytes.Contains(buffer.Bytes(), []byte("Package level assertion failed")) {
t.Fatalf("Expected package-level assertion message not found in output")
}
}
func TestNotEmpty(t *testing.T) {
var buffer bytes.Buffer
// Test NotEmpty with empty string (should fail)
NotEmpty(context.TODO(), "", "String should not be empty",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
if !bytes.Contains(buffer.Bytes(), []byte("String should not be empty")) {
t.Fatalf("Expected NotEmpty assertion message not found in output")
}
// Reset buffer
buffer.Reset()
// Test NotEmpty with non-empty string (should pass)
NotEmpty(context.TODO(), "hello", "String should not be empty",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
// Buffer should be empty since assertion passed
if buffer.Len() > 0 {
t.Fatalf("NotEmpty should have passed but got output: %s", buffer.String())
}
}
func TestPackageLevelWithOptions(t *testing.T) {
var buffer bytes.Buffer
// Test with JSON formatter
Assert(context.TODO(), false, "JSON formatted error",
WithFormatter(&JSONFormatter{}),
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
if !bytes.Contains(buffer.Bytes(), []byte(`"msg": "JSON formatted error"`)) {
t.Fatalf("Expected JSON formatted output not found")
}
}
func TestDefaultSafeBehavior(t *testing.T) {
// This test ensures the default behavior doesn't crash
// We can't easily test this without complex process management,
// but we can verify the default exit function is a no-op
// Get a fresh handler to test with
handler := NewAssertHandler()
handler.SetExitFunc(func(code int) {}) // Set no-op exit function
var buffer bytes.Buffer
handler.ToWriter(&buffer)
// This should call the exit function but not crash
handler.Assert(context.TODO(), false, "Test safe behavior")
if !bytes.Contains(buffer.Bytes(), []byte("Test safe behavior")) {
t.Fatalf("Expected safe behavior message not found in output")
}
}
func TestEqual(t *testing.T) {
var buffer bytes.Buffer
// Test Equal with matching values (should pass)
Equal(context.TODO(), 42, 42, "Numbers should be equal",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
// Buffer should be empty since assertion passed
if buffer.Len() > 0 {
t.Fatalf("Equal should have passed but got output: %s", buffer.String())
}
// Reset buffer
buffer.Reset()
// Test Equal with non-matching values (should fail)
Equal(context.TODO(), 42, 24, "Numbers should be equal",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
if !bytes.Contains(buffer.Bytes(), []byte("Numbers should be equal")) {
t.Fatalf("Expected Equal assertion message not found in output")
}
}
func TestNotEqual(t *testing.T) {
var buffer bytes.Buffer
// Test NotEqual with different values (should pass)
NotEqual(context.TODO(), 42, 24, "Numbers should be different",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
// Buffer should be empty since assertion passed
if buffer.Len() > 0 {
t.Fatalf("NotEqual should have passed but got output: %s", buffer.String())
}
// Reset buffer
buffer.Reset()
// Test NotEqual with same values (should fail)
NotEqual(context.TODO(), 42, 42, "Numbers should be different",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
if !bytes.Contains(buffer.Bytes(), []byte("Numbers should be different")) {
t.Fatalf("Expected NotEqual assertion message not found in output")
}
}
func TestContains(t *testing.T) {
var buffer bytes.Buffer
// Test Contains with valid substring (should pass)
Contains(context.TODO(), "hello world", "world", "Should contain substring",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
// Buffer should be empty since assertion passed
if buffer.Len() > 0 {
t.Fatalf("Contains should have passed but got output: %s", buffer.String())
}
// Reset buffer
buffer.Reset()
// Test Contains with invalid substring (should fail)
Contains(context.TODO(), "hello", "xyz", "Should contain substring",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
if !bytes.Contains(buffer.Bytes(), []byte("Should contain substring")) {
t.Fatalf("Expected Contains assertion message not found in output")
}
}
func TestConvenienceOptions(t *testing.T) {
var buffer bytes.Buffer
// Test WithTestingDefaults
Assert(context.TODO(), false, "Testing defaults",
WithTestingDefaults(),
WithWriter(&buffer))
if !bytes.Contains(buffer.Bytes(), []byte("Testing defaults")) {
t.Fatalf("Expected testing defaults assertion message not found in output")
}
// Reset buffer
buffer.Reset()
// Test WithProductionDefaults (should use JSON formatter)
Assert(context.TODO(), false, "Production defaults",
WithProductionDefaults(),
WithWriter(&buffer))
if !bytes.Contains(buffer.Bytes(), []byte(`"msg": "Production defaults"`)) {
t.Fatalf("Expected JSON formatted output from production defaults not found")
}
}
func TestTrueFalse(t *testing.T) {
var buffer bytes.Buffer
// Test True with true value (should pass)
True(context.TODO(), true, "Value should be true",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
// Buffer should be empty since assertion passed
if buffer.Len() > 0 {
t.Fatalf("True should have passed but got output: %s", buffer.String())
}
// Reset buffer
buffer.Reset()
// Test False with false value (should pass)
False(context.TODO(), false, "Value should be false",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
// Buffer should be empty since assertion passed
if buffer.Len() > 0 {
t.Fatalf("False should have passed but got output: %s", buffer.String())
}
}
func TestDebugModes(t *testing.T) {
var buffer bytes.Buffer
// Test default mode (no stack trace)
Assert(context.TODO(), false, "Default mode",
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
output := buffer.String()
if bytes.Contains(buffer.Bytes(), []byte("goroutine")) {
t.Fatalf("Default mode should not include stack trace, but got: %s", output)
}
// Reset buffer
buffer.Reset()
// Test debug mode (with stack trace)
Assert(context.TODO(), false, "Debug mode",
WithDebugMode(),
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
output = buffer.String()
if !bytes.Contains(buffer.Bytes(), []byte("goroutine")) {
t.Fatalf("Debug mode should include stack trace, but got: %s", output)
}
// Reset buffer
buffer.Reset()
// Test verbose mode (with stack trace and args)
Assert(context.TODO(), false, "Verbose mode",
WithVerboseMode(),
WithWriter(&buffer),
WithExitFunc(func(code int) {}))
output = buffer.String()
if !bytes.Contains(buffer.Bytes(), []byte("goroutine")) {
t.Fatalf("Verbose mode should include stack trace, but got: %s", output)
}
if !bytes.Contains(buffer.Bytes(), []byte("ARGS:")) {
t.Fatalf("Verbose mode should include ARGS, but got: %s", output)
}
}
func TestProductionDefaults(t *testing.T) {
var buffer bytes.Buffer
// Test production defaults (should be clean JSON without stack)
Assert(context.TODO(), false, "Production test",
WithProductionDefaults(),
WithWriter(&buffer))
output := buffer.String()
if bytes.Contains(buffer.Bytes(), []byte("goroutine")) {
t.Fatalf("Production defaults should not include stack trace, but got: %s", output)
}
if !bytes.Contains(buffer.Bytes(), []byte(`"msg": "Production test"`)) {
t.Fatalf("Production defaults should use JSON format, but got: %s", output)
}
}