-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_test.go
More file actions
739 lines (608 loc) · 21.6 KB
/
format_test.go
File metadata and controls
739 lines (608 loc) · 21.6 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package errs
import (
"fmt"
"io"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/domonda/go-pretty"
)
// Test types that implement pretty.PrintableWithResult
type SimplePrintable struct {
Value string
}
func (s *SimplePrintable) PrettyPrint(w io.Writer) (int, error) {
n1, _ := io.WriteString(w, "Simple{")
n2, _ := io.WriteString(w, s.Value)
n3, err := io.WriteString(w, "}")
return n1 + n2 + n3, err
}
type NestedPrintable struct {
Value string
}
func (n *NestedPrintable) PrettyPrint(w io.Writer) (int, error) {
n1, _ := io.WriteString(w, "Nested{")
n2, _ := io.WriteString(w, n.Value)
n3, err := io.WriteString(w, "}")
return n1 + n2 + n3, err
}
// Struct with nested pretty.Printable field
type ContainerWithNested struct {
ID string
Nested *NestedPrintable
Count int
}
// Struct with multiple nested pretty.Printable fields
type MultiNestedContainer struct {
First *SimplePrintable
Second *NestedPrintable
Name string
}
// Deeply nested structs
type DeepContainer struct {
Inner *ContainerWithNested
Label string
}
// Struct without pretty.Printable
type RegularStruct struct {
Name string
Value int
}
func TestFormatFunctionCall_TopLevelPrintable(t *testing.T) {
simple := &SimplePrintable{Value: "test"}
result := FormatFunctionCall("testFunc", simple)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "Simple{test}")
}
func TestFormatFunctionCall_NestedPrintable(t *testing.T) {
nested := &NestedPrintable{Value: "inner"}
container := &ContainerWithNested{
ID: "container-1",
Nested: nested,
Count: 42,
}
result := FormatFunctionCall("testFunc", container)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "ContainerWithNested{")
assert.Contains(t, result, "ID:`container-1`")
assert.Contains(t, result, "Nested:Nested{inner}")
assert.Contains(t, result, "Count:42")
}
func TestFormatFunctionCall_MultipleNestedPrintable(t *testing.T) {
container := &MultiNestedContainer{
First: &SimplePrintable{Value: "first"},
Second: &NestedPrintable{Value: "second"},
Name: "multi",
}
result := FormatFunctionCall("testFunc", container)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "MultiNestedContainer{")
assert.Contains(t, result, "First:Simple{first}")
assert.Contains(t, result, "Second:Nested{second}")
assert.Contains(t, result, "Name:`multi`")
}
func TestFormatFunctionCall_DeeplyNestedPrintable(t *testing.T) {
deep := &DeepContainer{
Inner: &ContainerWithNested{
ID: "deep-1",
Nested: &NestedPrintable{Value: "deeply-nested"},
Count: 99,
},
Label: "outer",
}
result := FormatFunctionCall("testFunc", deep)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "DeepContainer{")
assert.Contains(t, result, "ContainerWithNested{")
// The Nested field should use its PrettyPrint method
assert.Contains(t, result, "Nested:Nested{deeply-nested}")
assert.Contains(t, result, "Label:`outer`")
assert.Contains(t, result, "ID:`deep-1`")
}
func TestFormatFunctionCall_MixedParameters(t *testing.T) {
nested := &NestedPrintable{Value: "nested"}
container := &ContainerWithNested{
ID: "mix-1",
Nested: nested,
Count: 10,
}
regular := &RegularStruct{Name: "regular", Value: 123}
result := FormatFunctionCall("testFunc", container, "plain-string", 42, regular)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "ContainerWithNested{")
assert.Contains(t, result, "Nested{nested}")
assert.Contains(t, result, "`plain-string`")
assert.Contains(t, result, "42")
assert.Contains(t, result, "RegularStruct{")
}
func TestFormatFunctionCall_NilNestedField(t *testing.T) {
container := &ContainerWithNested{
ID: "nil-test",
Nested: nil,
Count: 0,
}
result := FormatFunctionCall("testFunc", container)
// Should not panic and should handle nil gracefully
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "ContainerWithNested{")
assert.Contains(t, result, "ID:`nil-test`")
}
func TestFormatFunctionCall_RegularStructWithoutPrintable(t *testing.T) {
regular := &RegularStruct{Name: "test", Value: 456}
result := FormatFunctionCall("testFunc", regular)
// Should use default go-pretty formatting
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "RegularStruct{")
assert.Contains(t, result, "Name:`test`")
assert.Contains(t, result, "Value:456")
}
func TestWrapWithFuncParams_NestedPrintable(t *testing.T) {
testFunc := func(container *ContainerWithNested) (err error) {
defer WrapWithFuncParams(&err, container)
return New("test error")
}
nested := &NestedPrintable{Value: "wrapped"}
container := &ContainerWithNested{
ID: "wrap-1",
Nested: nested,
Count: 7,
}
err := testFunc(container)
require.Error(t, err)
errStr := err.Error()
assert.Contains(t, errStr, "test error")
assert.Contains(t, errStr, "ContainerWithNested{")
assert.Contains(t, errStr, "Nested{wrapped}")
}
// Test Secret handling
type ContainerWithSecret struct {
Username string
Password Secret
APIKey Secret
}
type NestedSecretContainer struct {
ServiceName string
Credentials *ContainerWithSecret
Timeout int
}
func TestFormatFunctionCall_SecretInStruct(t *testing.T) {
container := &ContainerWithSecret{
Username: "admin",
Password: KeepSecret("super-secret-password"),
APIKey: KeepSecret("sk-1234567890abcdef"),
}
result := FormatFunctionCall("testFunc", container)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "ContainerWithSecret{")
assert.Contains(t, result, "Username:`admin`")
assert.Contains(t, result, "Password:***REDACTED***")
assert.Contains(t, result, "APIKey:***REDACTED***")
// Ensure actual secrets are not in output
assert.NotContains(t, result, "super-secret-password")
assert.NotContains(t, result, "sk-1234567890abcdef")
}
func TestFormatFunctionCall_NestedSecret(t *testing.T) {
nested := &NestedSecretContainer{
ServiceName: "api-service",
Credentials: &ContainerWithSecret{
Username: "service-account",
Password: KeepSecret("nested-secret-pass"),
APIKey: KeepSecret("nested-api-key-xyz"),
},
Timeout: 30,
}
result := FormatFunctionCall("testFunc", nested)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "NestedSecretContainer{")
assert.Contains(t, result, "ServiceName:`api-service`")
assert.Contains(t, result, "ContainerWithSecret{")
assert.Contains(t, result, "Username:`service-account`")
assert.Contains(t, result, "Password:***REDACTED***")
assert.Contains(t, result, "APIKey:***REDACTED***")
assert.Contains(t, result, "Timeout:30")
// Ensure actual secrets are not in output
assert.NotContains(t, result, "nested-secret-pass")
assert.NotContains(t, result, "nested-api-key-xyz")
}
func TestFormatFunctionCall_MixedPrintableAndSecret(t *testing.T) {
type MixedContainer struct {
Doc *SimplePrintable
Secret Secret
Metadata string
}
container := &MixedContainer{
Doc: &SimplePrintable{Value: "document-123"},
Secret: KeepSecret("secret-token"),
Metadata: "public-info",
}
result := FormatFunctionCall("testFunc", container)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "MixedContainer{")
assert.Contains(t, result, "Doc:Simple{document-123}")
assert.Contains(t, result, "Secret:***REDACTED***")
assert.Contains(t, result, "Metadata:`public-info`")
// Ensure secret is not in output
assert.NotContains(t, result, "secret-token")
}
func TestWrapWithFuncParams_SecretInNestedStruct(t *testing.T) {
testFunc := func(container *NestedSecretContainer) (err error) {
defer WrapWithFuncParams(&err, container)
return New("authentication failed")
}
nested := &NestedSecretContainer{
ServiceName: "auth-service",
Credentials: &ContainerWithSecret{
Username: "user",
Password: KeepSecret("should-not-appear"),
APIKey: KeepSecret("sk-should-not-appear"),
},
Timeout: 60,
}
err := testFunc(nested)
require.Error(t, err)
errStr := err.Error()
assert.Contains(t, errStr, "authentication failed")
assert.Contains(t, errStr, "NestedSecretContainer{")
assert.Contains(t, errStr, "Username:`user`")
assert.Contains(t, errStr, "Password:***REDACTED***")
assert.Contains(t, errStr, "APIKey:***REDACTED***")
// Most importantly: ensure secrets are NOT in the error message
assert.NotContains(t, errStr, "should-not-appear")
assert.NotContains(t, errStr, "sk-should-not-appear")
}
func TestFormatFunctionCall_TopLevelSecret(t *testing.T) {
// Test that a top-level secret parameter is handled
secret := KeepSecret("top-level-secret")
result := FormatFunctionCall("testFunc", "username", secret)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "`username`")
assert.Contains(t, result, "***REDACTED***")
assert.NotContains(t, result, "top-level-secret")
}
// Test PrintFuncFor customization
type ThirdPartyAPIKey string
type CustomStringer struct {
Name string
}
func (c CustomStringer) String() string {
return "CustomStringer[" + c.Name + "]"
}
type SensitiveData struct {
PublicID string
SecretKey string
}
func TestPrintFuncFor_MaskSensitiveStrings(t *testing.T) {
// Save original printer
originalPrinter := Printer
defer func() { Printer = originalPrinter }()
// Configure printer to mask strings containing sensitive keywords
Printer = Printer.WithPrintFuncFor(func(v reflect.Value) pretty.PrintFunc {
if v.Kind() == reflect.String {
str := v.String()
if strings.Contains(strings.ToLower(str), "password") ||
strings.Contains(strings.ToLower(str), "token") ||
strings.Contains(strings.ToLower(str), "apikey") {
return func(w io.Writer) (int, error) {
return io.WriteString(w, "`***REDACTED***`")
}
}
}
return pretty.PrintFuncForPrintable(v)
})
result := FormatFunctionCall("testFunc", "my-password-123", "safe-string", "Bearer-token-xyz")
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "`***REDACTED***`") // password should be masked
assert.Contains(t, result, "`safe-string`") // safe string should appear
// Second redacted for token
occurrences := strings.Count(result, "`***REDACTED***`")
assert.Equal(t, 2, occurrences, "Should have 2 redacted strings")
assert.NotContains(t, result, "my-password-123")
assert.NotContains(t, result, "Bearer-token-xyz")
}
func TestPrintFuncFor_CustomTypeRedaction(t *testing.T) {
// Save original printer
originalPrinter := Printer
defer func() { Printer = originalPrinter }()
// Configure printer to mask ThirdPartyAPIKey type
Printer = Printer.WithPrintFuncFor(func(v reflect.Value) pretty.PrintFunc {
if v.Type().String() == "errs.ThirdPartyAPIKey" {
return func(w io.Writer) (int, error) {
return io.WriteString(w, "***REDACTED_API_KEY***")
}
}
return pretty.PrintFuncForPrintable(v)
})
apiKey := ThirdPartyAPIKey("sk-1234567890abcdef")
result := FormatFunctionCall("testFunc", "user-123", apiKey)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "`user-123`")
assert.Contains(t, result, "***REDACTED_API_KEY***")
assert.NotContains(t, result, "sk-1234567890abcdef")
}
func TestPrintFuncFor_AdaptFmtStringer(t *testing.T) {
// Save original printer
originalPrinter := Printer
defer func() { Printer = originalPrinter }()
// Configure printer to use String() method from fmt.Stringer types
Printer = Printer.WithPrintFuncFor(func(v reflect.Value) pretty.PrintFunc {
stringer, ok := v.Interface().(fmt.Stringer)
if !ok && v.CanAddr() {
stringer, ok = v.Addr().Interface().(fmt.Stringer)
}
if ok {
return func(w io.Writer) (int, error) {
return io.WriteString(w, stringer.String())
}
}
return pretty.PrintFuncForPrintable(v)
})
custom := CustomStringer{Name: "test"}
result := FormatFunctionCall("testFunc", custom, "other")
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "CustomStringer[test]")
assert.Contains(t, result, "`other`")
}
func TestPrintFuncFor_StructFieldMasking(t *testing.T) {
// Save original printer
originalPrinter := Printer
defer func() { Printer = originalPrinter }()
// Configure printer to mask struct fields containing "secret" in name
Printer = Printer.WithPrintFuncFor(func(v reflect.Value) pretty.PrintFunc {
if v.Kind() == reflect.Struct {
t := v.Type()
hasSecretField := false
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if strings.Contains(strings.ToLower(field.Name), "secret") {
hasSecretField = true
break
}
}
if hasSecretField {
return func(w io.Writer) (int, error) {
n1, _ := io.WriteString(w, t.Name())
n2, err := io.WriteString(w, "{***FIELDS_REDACTED***}")
return n1 + n2, err
}
}
}
return pretty.PrintFuncForPrintable(v)
})
sensitive := SensitiveData{
PublicID: "public-123",
SecretKey: "should-not-appear",
}
result := FormatFunctionCall("testFunc", sensitive)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "SensitiveData{***FIELDS_REDACTED***}")
assert.NotContains(t, result, "public-123")
assert.NotContains(t, result, "should-not-appear")
}
func TestPrintFuncFor_PreservesPrintableInterface(t *testing.T) {
// Save original printer
originalPrinter := Printer
defer func() { Printer = originalPrinter }()
// Configure printer with custom logic that falls back to Printable
Printer = Printer.WithPrintFuncFor(func(v reflect.Value) pretty.PrintFunc {
// Custom logic that doesn't match our types
if v.Kind() == reflect.String && v.String() == "special" {
return func(w io.Writer) (int, error) {
return io.WriteString(w, "`SPECIAL`")
}
}
// Fall back to default Printable interface handling
return pretty.PrintFuncForPrintable(v)
})
simple := &SimplePrintable{Value: "test"}
result := FormatFunctionCall("testFunc", simple, "special", "normal")
assert.Contains(t, result, "testFunc(")
// SimplePrintable should still use its PrettyPrint method
assert.Contains(t, result, "Simple{test}")
// "special" string should use custom logic
assert.Contains(t, result, "`SPECIAL`")
// "normal" string should use default formatting
assert.Contains(t, result, "`normal`")
}
func TestPrintFuncFor_WithWrapWithFuncParams(t *testing.T) {
// Save original printer
originalPrinter := Printer
defer func() { Printer = originalPrinter }()
// Configure printer to mask sensitive data
Printer = Printer.WithPrintFuncFor(func(v reflect.Value) pretty.PrintFunc {
if v.Kind() == reflect.String {
str := v.String()
if strings.Contains(str, "secret-") {
return func(w io.Writer) (int, error) {
return io.WriteString(w, "`***REDACTED***`")
}
}
}
return pretty.PrintFuncForPrintable(v)
})
testFunc := func(userID string, apiKey string) (err error) {
defer WrapWithFuncParams(&err, userID, apiKey)
return New("operation failed")
}
err := testFunc("user-123", "secret-api-key-xyz")
require.Error(t, err)
errStr := err.Error()
assert.Contains(t, errStr, "operation failed")
assert.Contains(t, errStr, "`user-123`")
assert.Contains(t, errStr, "`***REDACTED***`")
assert.NotContains(t, errStr, "secret-api-key-xyz")
}
func TestPrintFuncFor_NestedStructsWithCustomFormatting(t *testing.T) {
// Save original printer
originalPrinter := Printer
defer func() { Printer = originalPrinter }()
type NestedSensitive struct {
Data SensitiveData
Name string
}
// Configure printer to mask SensitiveData type
Printer = Printer.WithPrintFuncFor(func(v reflect.Value) pretty.PrintFunc {
if v.Type().String() == "errs.SensitiveData" {
return func(w io.Writer) (int, error) {
return io.WriteString(w, "SensitiveData{***MASKED***}")
}
}
return pretty.PrintFuncForPrintable(v)
})
nested := NestedSensitive{
Data: SensitiveData{
PublicID: "public",
SecretKey: "secret",
},
Name: "container",
}
result := FormatFunctionCall("testFunc", nested)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "NestedSensitive{")
assert.Contains(t, result, "Data:SensitiveData{***MASKED***}")
assert.Contains(t, result, "Name:`container`")
assert.NotContains(t, result, "public")
assert.NotContains(t, result, "secret")
}
func TestPrintFuncFor_NilReturnUsesDefault(t *testing.T) {
// Save original printer
originalPrinter := Printer
defer func() { Printer = originalPrinter }()
// Configure printer that returns nil for non-matching cases
Printer = Printer.WithPrintFuncFor(func(v reflect.Value) pretty.PrintFunc {
if v.Kind() == reflect.String && v.String() == "intercept" {
return func(w io.Writer) (int, error) {
return io.WriteString(w, "`INTERCEPTED`")
}
}
// Return nil to use default behavior
return nil
})
result := FormatFunctionCall("testFunc", "intercept", "normal", 42)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "`INTERCEPTED`")
assert.Contains(t, result, "`normal`")
assert.Contains(t, result, "42")
}
func TestFormatParamMaxLen_ShortParameter(t *testing.T) {
// Save original value and restore after test
originalMaxLen := FormatParamMaxLen
defer func() { FormatParamMaxLen = originalMaxLen }()
FormatParamMaxLen = 100
shortString := "This is a short string"
result := FormatFunctionCall("testFunc", shortString)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, shortString)
assert.NotContains(t, result, "TRUNCATED")
}
func TestFormatParamMaxLen_LongParameterTruncated(t *testing.T) {
// Save original value and restore after test
originalMaxLen := FormatParamMaxLen
defer func() { FormatParamMaxLen = originalMaxLen }()
FormatParamMaxLen = 50
longString := strings.Repeat("abcdefghij", 20) // 200 characters
result := FormatFunctionCall("testFunc", longString)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "…(TRUNCATED)")
// Result should be shorter than the original string
assert.Less(t, len(result), len(longString)+50)
// Should contain the beginning of the string
assert.Contains(t, result, "abcdefghij")
}
func TestFormatParamMaxLen_ExactlyAtLimit(t *testing.T) {
// Save original value and restore after test
originalMaxLen := FormatParamMaxLen
defer func() { FormatParamMaxLen = originalMaxLen }()
FormatParamMaxLen = 50
// Create string that will be exactly at the limit when formatted (with backticks)
exactString := strings.Repeat("x", 48) // 48 + 2 backticks = 50
result := FormatFunctionCall("testFunc", exactString)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, exactString)
assert.NotContains(t, result, "TRUNCATED")
}
func TestFormatParamMaxLen_UTF8Safety(t *testing.T) {
// Save original value and restore after test
originalMaxLen := FormatParamMaxLen
defer func() { FormatParamMaxLen = originalMaxLen }()
FormatParamMaxLen = 20
// String with multi-byte UTF-8 characters (emoji are 4 bytes each)
utf8String := "Hello 世界 🌍🌎🌏" // Mix of ASCII, Chinese, and emoji
result := FormatFunctionCall("testFunc", utf8String)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "…(TRUNCATED)")
// Result should be valid UTF-8 (no panic when converting to string)
assert.NotPanics(t, func() {
_ = string([]byte(result))
})
// Should contain at least some of the beginning
assert.Contains(t, result, "Hello")
}
func TestFormatParamMaxLen_MultipleParameters(t *testing.T) {
// Save original value and restore after test
originalMaxLen := FormatParamMaxLen
defer func() { FormatParamMaxLen = originalMaxLen }()
FormatParamMaxLen = 30
shortString := "short"
longString := strings.Repeat("verylongtext", 10) // 120 characters
result := FormatFunctionCall("testFunc", shortString, longString, 42)
assert.Contains(t, result, "testFunc(")
// Short string should not be truncated
assert.Contains(t, result, shortString)
// Long string should be truncated
assert.Contains(t, result, "…(TRUNCATED)")
// Number should be present
assert.Contains(t, result, "42")
// Should have proper comma separation
assert.Contains(t, result, ", ")
}
func TestFormatParamMaxLen_LargeStruct(t *testing.T) {
// Save original value and restore after test
originalMaxLen := FormatParamMaxLen
defer func() { FormatParamMaxLen = originalMaxLen }()
FormatParamMaxLen = 50
type LargeStruct struct {
Field1 string
Field2 string
Field3 string
Field4 string
Field5 string
}
largeData := LargeStruct{
Field1: "This is a very long field value that will make the struct large",
Field2: "Another very long field value with lots of text",
Field3: "Yet another long field value",
Field4: "More data here",
Field5: "Even more data",
}
result := FormatFunctionCall("testFunc", largeData)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "…(TRUNCATED)")
assert.Contains(t, result, "LargeStruct")
}
func TestFormatParamMaxLen_ZeroValue(t *testing.T) {
// Save original value and restore after test
originalMaxLen := FormatParamMaxLen
defer func() { FormatParamMaxLen = originalMaxLen }()
// Setting to 0 should effectively truncate everything to empty
FormatParamMaxLen = 0
testString := "any string"
result := FormatFunctionCall("testFunc", testString)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, "…(TRUNCATED)")
// Result should be very short (just the function name and truncation marker)
assert.Less(t, len(result), 30)
}
func TestFormatParamMaxLen_VeryLargeLimit(t *testing.T) {
// Save original value and restore after test
originalMaxLen := FormatParamMaxLen
defer func() { FormatParamMaxLen = originalMaxLen }()
FormatParamMaxLen = 1000000 // 1MB limit
// Use a shorter string that won't trigger go-pretty's internal limit
moderateString := strings.Repeat("test", 20) // 80 characters
result := FormatFunctionCall("testFunc", moderateString)
assert.Contains(t, result, "testFunc(")
assert.Contains(t, result, moderateString)
assert.NotContains(t, result, "TRUNCATED")
}