@@ -27,6 +27,7 @@ import (
27
27
"github.com/go-logr/logr"
28
28
)
29
29
30
+ // Will be handled via reflection instead of type assertions.
30
31
type substr string
31
32
32
33
func ptrint (i int ) * int {
@@ -198,7 +199,9 @@ func TestPretty(t *testing.T) {
198
199
exp string // used in cases where JSON can't handle it
199
200
}{
200
201
{val : "strval" },
202
+ {val : "strval\n with\t \" escapes\" " },
201
203
{val : substr ("substrval" )},
204
+ {val : substr ("substrval\n with\t \" escapes\" " )},
202
205
{val : true },
203
206
{val : false },
204
207
{val : int (93 )},
@@ -235,7 +238,11 @@ func TestPretty(t *testing.T) {
235
238
exp : `[]` ,
236
239
},
237
240
{val : []int {9 , 3 , 7 , 6 }},
241
+ {val : []string {"str" , "with\t escape" }},
242
+ {val : []substr {"substr" , "with\t escape" }},
238
243
{val : [4 ]int {9 , 3 , 7 , 6 }},
244
+ {val : [2 ]string {"str" , "with\t escape" }},
245
+ {val : [2 ]substr {"substr" , "with\t escape" }},
239
246
{
240
247
val : struct {
241
248
Int int
@@ -255,11 +262,32 @@ func TestPretty(t *testing.T) {
255
262
"nine" : 3 ,
256
263
},
257
264
},
265
+ {
266
+ val : map [string ]int {
267
+ "with\t escape" : 76 ,
268
+ },
269
+ },
258
270
{
259
271
val : map [substr ]int {
260
272
"nine" : 3 ,
261
273
},
262
274
},
275
+ {
276
+ val : map [substr ]int {
277
+ "with\t escape" : 76 ,
278
+ },
279
+ },
280
+ {
281
+ val : map [int ]int {
282
+ 9 : 3 ,
283
+ },
284
+ },
285
+ {
286
+ val : map [float64 ]int {
287
+ 9.5 : 3 ,
288
+ },
289
+ exp : `{"9.5":3}` ,
290
+ },
263
291
{
264
292
val : struct {
265
293
X int `json:"x"`
@@ -283,6 +311,7 @@ func TestPretty(t *testing.T) {
283
311
val : []struct { X , Y string }{
284
312
{"nine" , "three" },
285
313
{"seven" , "six" },
314
+ {"with\t " , "\t escapes" },
286
315
},
287
316
},
288
317
{
@@ -438,6 +467,24 @@ func TestPretty(t *testing.T) {
438
467
val : PseudoStruct (makeKV ("f1" , 1 , "f2" , true , "f3" , []int {})),
439
468
exp : `{"f1":1,"f2":true,"f3":[]}` ,
440
469
},
470
+ {
471
+ val : map [TjsontagsString ]int {
472
+ {String1 : `"quoted"` , String4 : `unquoted` }: 1 ,
473
+ },
474
+ exp : `{"{\"string1\":\"\\\"quoted\\\"\",\"-\":\"\",\"string4\":\"unquoted\",\"String5\":\"\"}":1}` ,
475
+ },
476
+ {
477
+ val : map [TjsontagsInt ]int {
478
+ {Int1 : 1 , Int2 : 2 }: 3 ,
479
+ },
480
+ exp : `{"{\"int1\":1,\"-\":0,\"Int5\":0}":3}` ,
481
+ },
482
+ {
483
+ val : map [[2 ]struct { S string }]int {
484
+ {{S : `"quoted"` }, {S : "unquoted" }}: 1 ,
485
+ },
486
+ exp : `{"[{\"S\":\"\\\"quoted\\\"\"},{\"S\":\"unquoted\"}]":1}` ,
487
+ },
441
488
}
442
489
443
490
f := NewFormatter (Options {})
@@ -449,7 +496,7 @@ func TestPretty(t *testing.T) {
449
496
} else {
450
497
jb , err := json .Marshal (tc .val )
451
498
if err != nil {
452
- t .Errorf ("[%d]: unexpected error: %v" , i , err )
499
+ t .Fatalf ("[%d]: unexpected error: %v" , i , err )
453
500
}
454
501
want = string (jb )
455
502
}
@@ -496,6 +543,13 @@ func TestRender(t *testing.T) {
496
543
args : makeKV ("bool" , PseudoStruct (makeKV ("boolsub" , true ))),
497
544
expectKV : `"int"={"intsub":1} "str"={"strsub":"2"} "bool"={"boolsub":true}` ,
498
545
expectJSON : `{"int":{"intsub":1},"str":{"strsub":"2"},"bool":{"boolsub":true}}` ,
546
+ }, {
547
+ name : "escapes" ,
548
+ builtins : makeKV ("\" 1\" " , 1 ), // will not be escaped, but should never happen
549
+ values : makeKV ("\t str" , "ABC" ), // escaped
550
+ args : makeKV ("bool\n " , true ), // escaped
551
+ expectKV : `""1""=1 "\tstr"="ABC" "bool\n"=true` ,
552
+ expectJSON : `{""1"":1,"\tstr":"ABC","bool\n":true}` ,
499
553
}, {
500
554
name : "missing value" ,
501
555
builtins : makeKV ("builtin" ),
@@ -505,27 +559,27 @@ func TestRender(t *testing.T) {
505
559
expectJSON : `{"builtin":"<no-value>","value":"<no-value>","arg":"<no-value>"}` ,
506
560
}, {
507
561
name : "non-string key int" ,
508
- args : makeKV (123 , "val" ),
562
+ builtins : makeKV (123 , "val" ), // should never happen
509
563
values : makeKV (456 , "val" ),
510
- builtins : makeKV (789 , "val" ),
511
- expectKV : `"<non-string-key: 789 >"="val" "<non-string-key: 456>"="val" "<non-string-key: 123 >"="val"` ,
512
- expectJSON : `{"<non-string-key: 789 >":"val","<non-string-key: 456>":"val","<non-string-key: 123 >":"val"}` ,
564
+ args : makeKV (789 , "val" ),
565
+ expectKV : `"<non-string-key: 123 >"="val" "<non-string-key: 456>"="val" "<non-string-key: 789 >"="val"` ,
566
+ expectJSON : `{"<non-string-key: 123 >":"val","<non-string-key: 456>":"val","<non-string-key: 789 >":"val"}` ,
513
567
}, {
514
568
name : "non-string key struct" ,
515
- args : makeKV (struct {
569
+ builtins : makeKV (struct { // will not be escaped, but should never happen
516
570
F1 string
517
571
F2 int
518
- }{"arg " , 123 }, "val" ),
572
+ }{"builtin " , 123 }, "val" ),
519
573
values : makeKV (struct {
520
574
F1 string
521
575
F2 int
522
576
}{"value" , 456 }, "val" ),
523
- builtins : makeKV (struct {
577
+ args : makeKV (struct {
524
578
F1 string
525
579
F2 int
526
- }{"builtin " , 789 }, "val" ),
527
- expectKV : `"<non-string-key: {"F1":"builtin",>"="val" "<non-string-key: {"F1": "value", "F>"="val" "<non-string-key: {"F1": "arg", "F2">"="val"` ,
528
- expectJSON : `{"<non-string-key: {"F1":"builtin",>":"val","<non-string-key: {"F1": "value", "F>":"val","<non-string-key: {"F1": "arg", "F2">":"val"}` ,
580
+ }{"arg " , 789 }, "val" ),
581
+ expectKV : `"<non-string-key: {"F1":"builtin",>"="val" "<non-string-key: {\ "F1\":\ "value\",\ "F>"="val" "<non-string-key: {\ "F1\":\ "arg\",\ "F2\ ">"="val"` ,
582
+ expectJSON : `{"<non-string-key: {"F1":"builtin",>":"val","<non-string-key: {\ "F1\":\ "value\",\ "F>":"val","<non-string-key: {\ "F1\":\ "arg\",\ "F2\ ">":"val"}` ,
529
583
}}
530
584
531
585
for _ , tc := range testCases {
@@ -534,7 +588,7 @@ func TestRender(t *testing.T) {
534
588
formatter .AddValues (tc .values )
535
589
r := formatter .render (tc .builtins , tc .args )
536
590
if r != expect {
537
- t .Errorf ("wrong output:\n expected %q \n got %q " , expect , r )
591
+ t .Errorf ("wrong output:\n expected %v \n got %v " , expect , r )
538
592
}
539
593
}
540
594
t .Run ("KV" , func (t * testing.T ) {
0 commit comments