-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_test.go
More file actions
504 lines (433 loc) · 11.7 KB
/
debug_test.go
File metadata and controls
504 lines (433 loc) · 11.7 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package debugkit
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/fatih/color"
)
func TestMain(m *testing.M) {
color.NoColor = true
os.Exit(m.Run())
}
// captureOutput redirects stdout and captures whatever is printed during fn().
func captureOutput(fn func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
fn()
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
// --- indentStr tests ---
func TestIndentStr(t *testing.T) {
tests := []struct {
level int
want string
}{
{0, ""},
{1, " "},
{2, " "},
{3, " "},
}
for _, tt := range tests {
got := indentStr(tt.level)
if got != tt.want {
t.Errorf("indentStr(%d) = %q, want %q", tt.level, got, tt.want)
}
}
}
// --- Dump tests ---
func TestDumpString(t *testing.T) {
out := captureOutput(func() { Dump("hello") })
want := "\"hello\"\n"
if out != want {
t.Errorf("Dump(string) = %q, want %q", out, want)
}
}
func TestDumpInt(t *testing.T) {
out := captureOutput(func() { Dump(42) })
want := "42\n"
if out != want {
t.Errorf("Dump(int) = %q, want %q", out, want)
}
}
func TestDumpFloat(t *testing.T) {
out := captureOutput(func() { Dump(3.14) })
want := "3.14\n"
if out != want {
t.Errorf("Dump(float) = %q, want %q", out, want)
}
}
func TestDumpBool(t *testing.T) {
out := captureOutput(func() { Dump(true) })
want := "true\n"
if out != want {
t.Errorf("Dump(bool) = %q, want %q", out, want)
}
}
func TestDumpNil(t *testing.T) {
out := captureOutput(func() { Dump(nil) })
want := "nil\n"
if out != want {
t.Errorf("Dump(nil) = %q, want %q", out, want)
}
}
func TestDumpStruct(t *testing.T) {
type Person struct {
Name string
Age int
}
p := Person{Name: "Alice", Age: 30}
out := captureOutput(func() { Dump(p) })
if !strings.Contains(out, "Person {") {
t.Errorf("expected struct header, got %q", out)
}
if !strings.Contains(out, "Name: \"Alice\"") {
t.Errorf("expected Name field, got %q", out)
}
if !strings.Contains(out, "Age: 30") {
t.Errorf("expected Age field, got %q", out)
}
}
func TestDumpNestedStruct(t *testing.T) {
type Address struct {
City string
}
type Person struct {
Name string
Address Address
}
p := Person{Name: "Bob", Address: Address{City: "NYC"}}
out := captureOutput(func() { Dump(p) })
if !strings.Contains(out, "Person {") {
t.Errorf("expected outer struct, got %q", out)
}
if !strings.Contains(out, "Address {") {
t.Errorf("expected nested struct, got %q", out)
}
if !strings.Contains(out, "City: \"NYC\"") {
t.Errorf("expected City field, got %q", out)
}
}
func TestDumpSlice(t *testing.T) {
out := captureOutput(func() { Dump([]int{1, 2, 3}) })
if !strings.Contains(out, "[") {
t.Errorf("expected opening bracket, got %q", out)
}
if !strings.Contains(out, "1,") {
t.Errorf("expected element 1, got %q", out)
}
if !strings.Contains(out, "2,") {
t.Errorf("expected element 2, got %q", out)
}
if !strings.Contains(out, "3,") {
t.Errorf("expected element 3, got %q", out)
}
if !strings.Contains(out, "]") {
t.Errorf("expected closing bracket, got %q", out)
}
}
func TestDumpEmptySlice(t *testing.T) {
out := captureOutput(func() { Dump([]int{}) })
if !strings.Contains(out, "[") || !strings.Contains(out, "]") {
t.Errorf("expected brackets for empty slice, got %q", out)
}
}
func TestDumpArray(t *testing.T) {
out := captureOutput(func() { Dump([2]string{"a", "b"}) })
if !strings.Contains(out, `"a"`) {
t.Errorf("expected element a, got %q", out)
}
if !strings.Contains(out, `"b"`) {
t.Errorf("expected element b, got %q", out)
}
}
func TestDumpMap(t *testing.T) {
// Use a single-entry map to avoid key ordering issues.
out := captureOutput(func() { Dump(map[string]int{"x": 1}) })
if !strings.Contains(out, "{") {
t.Errorf("expected opening brace, got %q", out)
}
if !strings.Contains(out, "x: 1") {
t.Errorf("expected key-value pair, got %q", out)
}
if !strings.Contains(out, "}") {
t.Errorf("expected closing brace, got %q", out)
}
}
func TestDumpEmptyMap(t *testing.T) {
out := captureOutput(func() { Dump(map[string]int{}) })
if !strings.Contains(out, "{") || !strings.Contains(out, "}") {
t.Errorf("expected braces for empty map, got %q", out)
}
}
func TestDumpPointer(t *testing.T) {
s := "hello"
out := captureOutput(func() { Dump(&s) })
want := "\"hello\"\n"
if out != want {
t.Errorf("Dump(&string) = %q, want %q", out, want)
}
}
func TestDumpNilPointer(t *testing.T) {
var p *int
out := captureOutput(func() { Dump(p) })
want := "nil\n"
if out != want {
t.Errorf("Dump(nil pointer) = %q, want %q", out, want)
}
}
func TestDumpCircularReference(t *testing.T) {
type Node struct {
Value int
Next *Node
}
a := &Node{Value: 1}
b := &Node{Value: 2}
a.Next = b
b.Next = a // circular
out := captureOutput(func() { Dump(a) })
if !strings.Contains(out, "<circular>") {
t.Errorf("expected <circular> for circular reference, got %q", out)
}
if !strings.Contains(out, "Value: 1") {
t.Errorf("expected Value: 1, got %q", out)
}
if !strings.Contains(out, "Value: 2") {
t.Errorf("expected Value: 2, got %q", out)
}
}
func TestDumpSliceOfStructs(t *testing.T) {
type Item struct {
ID int
}
items := []Item{{ID: 1}, {ID: 2}}
out := captureOutput(func() { Dump(items) })
if !strings.Contains(out, "Item {") {
t.Errorf("expected struct inside slice, got %q", out)
}
if !strings.Contains(out, "ID: 1") {
t.Errorf("expected ID: 1, got %q", out)
}
if !strings.Contains(out, "ID: 2") {
t.Errorf("expected ID: 2, got %q", out)
}
}
// --- printValue tests for edge cases ---
func TestPrintValueInvalid(t *testing.T) {
out := captureOutput(func() {
visited := make(map[uintptr]bool)
printValue(reflect.Value{}, 0, visited)
})
if out != "nil" {
t.Errorf("printValue(invalid) = %q, want %q", out, "nil")
}
}
func TestHandlePointerVisitedFlag(t *testing.T) {
x := 42
p := &x
out := captureOutput(func() {
visited := make(map[uintptr]bool)
v := reflect.ValueOf(p)
// First call should print the value and mark as visited.
handlePointer(v, 0, visited)
fmt.Print("|")
// Second call should detect circular.
handlePointer(v, 0, visited)
})
parts := strings.Split(out, "|")
if len(parts) != 2 {
t.Fatalf("expected 2 parts, got %d: %q", len(parts), out)
}
if parts[0] != "42" {
t.Errorf("first call = %q, want %q", parts[0], "42")
}
if parts[1] != "<circular>" {
t.Errorf("second call = %q, want %q", parts[1], "<circular>")
}
}
// --- Trace tests ---
func TestTraceInt(t *testing.T) {
out := captureOutput(func() { Trace(99) })
if !strings.Contains(out, "debug_test.go:") {
t.Errorf("expected file:line header, got %q", out)
}
if !strings.Contains(out, "99") {
t.Errorf("expected value 99, got %q", out)
}
}
func TestTraceString(t *testing.T) {
out := captureOutput(func() { Trace("hello") })
if !strings.Contains(out, "debug_test.go:") {
t.Errorf("expected file:line header, got %q", out)
}
if !strings.Contains(out, `"hello"`) {
t.Errorf("expected quoted string, got %q", out)
}
}
func TestTraceNil(t *testing.T) {
out := captureOutput(func() { Trace(nil) })
if !strings.Contains(out, "debug_test.go:") {
t.Errorf("expected file:line header, got %q", out)
}
if !strings.Contains(out, "nil") {
t.Errorf("expected nil, got %q", out)
}
}
func TestTraceStruct(t *testing.T) {
type Item struct {
Name string
}
out := captureOutput(func() { Trace(Item{Name: "x"}) })
if !strings.Contains(out, "debug_test.go:") {
t.Errorf("expected file:line header, got %q", out)
}
if !strings.Contains(out, "Item {") {
t.Errorf("expected struct output, got %q", out)
}
if !strings.Contains(out, `Name: "x"`) {
t.Errorf("expected Name field, got %q", out)
}
}
func TestTraceHeaderFormat(t *testing.T) {
out := captureOutput(func() { Trace(1) })
// Should start with newline, then [file:line]
if !strings.HasPrefix(out, "\n[") {
t.Errorf("expected output to start with newline and bracket, got %q", out)
}
if !strings.Contains(out, "]") {
t.Errorf("expected closing bracket in header, got %q", out)
}
}
// --- TraceAll tests ---
func TestTraceAllSingleValue(t *testing.T) {
out := captureOutput(func() { TraceAll(42) })
if !strings.Contains(out, "debug_test.go:") {
t.Errorf("expected file:line header, got %q", out)
}
if !strings.Contains(out, "arg0 = 42") {
t.Errorf("expected arg0 = 42, got %q", out)
}
}
func TestTraceAllMultipleValues(t *testing.T) {
out := captureOutput(func() { TraceAll("a", 2, true) })
if !strings.Contains(out, "debug_test.go:") {
t.Errorf("expected file:line header, got %q", out)
}
if !strings.Contains(out, `arg0 = "a"`) {
t.Errorf("expected arg0, got %q", out)
}
if !strings.Contains(out, "arg1 = 2") {
t.Errorf("expected arg1, got %q", out)
}
if !strings.Contains(out, "arg2 = true") {
t.Errorf("expected arg2, got %q", out)
}
}
func TestTraceAllNoValues(t *testing.T) {
out := captureOutput(func() { TraceAll() })
// Should still print the header, just no arg lines.
if !strings.Contains(out, "debug_test.go:") {
t.Errorf("expected file:line header, got %q", out)
}
if strings.Contains(out, "arg0") {
t.Errorf("expected no args, got %q", out)
}
}
func TestTraceAllWithNil(t *testing.T) {
out := captureOutput(func() { TraceAll(nil, 5) })
if !strings.Contains(out, "arg0 = nil") {
t.Errorf("expected arg0 = nil, got %q", out)
}
if !strings.Contains(out, "arg1 = 5") {
t.Errorf("expected arg1 = 5, got %q", out)
}
}
func TestTraceAllWithStruct(t *testing.T) {
type Coord struct {
X int
Y int
}
out := captureOutput(func() { TraceAll(Coord{X: 1, Y: 2}, "label") })
if !strings.Contains(out, "Coord {") {
t.Errorf("expected struct in output, got %q", out)
}
if !strings.Contains(out, "X: 1") {
t.Errorf("expected X field, got %q", out)
}
if !strings.Contains(out, `arg1 = "label"`) {
t.Errorf("expected arg1, got %q", out)
}
}
func TestDumpStructWithUnexportedFields(t *testing.T) {
type secret struct {
hidden string
count int
flag bool
ratio float64
Visible string
}
s := secret{
hidden: "private",
count: 42,
flag: true,
ratio: 3.14,
Visible: "public",
}
// Should not panic on unexported fields.
out := captureOutput(func() { Dump(s) })
if !strings.Contains(out, "secret {") {
t.Errorf("expected struct header, got %q", out)
}
if !strings.Contains(out, `Visible: "public"`) {
t.Errorf("expected exported field, got %q", out)
}
if !strings.Contains(out, "hidden:") {
t.Errorf("expected unexported field name, got %q", out)
}
if !strings.Contains(out, "count:") {
t.Errorf("expected unexported int field name, got %q", out)
}
if !strings.Contains(out, "flag:") {
t.Errorf("expected unexported bool field name, got %q", out)
}
if !strings.Contains(out, "ratio:") {
t.Errorf("expected unexported float field name, got %q", out)
}
}
func TestDumpTimeValue(t *testing.T) {
ts := time.Date(2025, 6, 15, 10, 30, 0, 0, time.UTC)
out := captureOutput(func() { Dump(ts) })
if !strings.Contains(out, "2025-06-15") {
t.Errorf("expected human-readable time, got %q", out)
}
// Should NOT show internal fields
if strings.Contains(out, "wall:") || strings.Contains(out, "ext:") {
t.Errorf("should not show internal time fields, got %q", out)
}
}
func TestDumpStructWithTimeField(t *testing.T) {
type Event struct {
Name string
At time.Time
}
e := Event{Name: "launch", At: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)}
out := captureOutput(func() { Dump(e) })
if !strings.Contains(out, "Event {") {
t.Errorf("expected struct header, got %q", out)
}
if !strings.Contains(out, `Name: "launch"`) {
t.Errorf("expected Name field, got %q", out)
}
if !strings.Contains(out, "2025-01-01") {
t.Errorf("expected human-readable time in At field, got %q", out)
}
}