@@ -461,51 +461,75 @@ func makeKV(args ...interface{}) []interface{} {
461
461
func TestRender (t * testing.T ) {
462
462
testCases := []struct {
463
463
name string
464
- kv []interface {}
464
+ builtins []interface {}
465
+ values []interface {}
466
+ args []interface {}
465
467
expectKV string
466
468
expectJSON string
467
469
}{{
468
470
name : "nil" ,
469
- kv : nil ,
470
471
expectKV : "" ,
471
472
expectJSON : "{}" ,
472
473
}, {
473
474
name : "empty" ,
474
- kv : []interface {}{},
475
+ builtins : []interface {}{},
476
+ values : []interface {}{},
477
+ args : []interface {}{},
475
478
expectKV : "" ,
476
479
expectJSON : "{}" ,
477
480
}, {
478
481
name : "primitives" ,
479
- kv : makeKV ("int" , 1 , "str" , "ABC" , "bool" , true ),
480
- expectKV : `"int"=1 "str"="ABC" "bool"=true` ,
481
- expectJSON : `{"int":1,"str":"ABC","bool":true}` ,
482
+ builtins : makeKV ("int1" , 1 , "int2" , 2 ),
483
+ values : makeKV ("str1" , "ABC" , "str2" , "DEF" ),
484
+ args : makeKV ("bool1" , true , "bool2" , false ),
485
+ expectKV : `"int1"=1 "int2"=2 "str1"="ABC" "str2"="DEF" "bool1"=true "bool2"=false` ,
486
+ expectJSON : `{"int1":1,"int2":2,"str1":"ABC","str2":"DEF","bool1":true,"bool2":false}` ,
482
487
}, {
483
488
name : "missing value" ,
484
- kv : makeKV ("key" ),
485
- expectKV : `"key"="<no-value>"` ,
486
- expectJSON : `{"key":"<no-value>"}` ,
489
+ builtins : makeKV ("builtin" ),
490
+ values : makeKV ("value" ),
491
+ args : makeKV ("arg" ),
492
+ expectKV : `"builtin"="<no-value>" "value"="<no-value>" "arg"="<no-value>"` ,
493
+ expectJSON : `{"builtin":"<no-value>","value":"<no-value>","arg":"<no-value>"}` ,
487
494
}, {
488
- name : "non-string key" ,
489
- kv : makeKV (123 , "val" ),
490
- expectKV : `"<non-string-key>"="val"` ,
491
- expectJSON : `{"<non-string-key>":"val"}` ,
495
+ name : "non-string key int" ,
496
+ args : makeKV (123 , "val" ),
497
+ values : makeKV (456 , "val" ),
498
+ builtins : makeKV (789 , "val" ),
499
+ expectKV : `"<non-string-key: 789>"="val" "<non-string-key: 456>"="val" "<non-string-key: 123>"="val"` ,
500
+ expectJSON : `{"<non-string-key: 789>":"val","<non-string-key: 456>":"val","<non-string-key: 123>":"val"}` ,
501
+ }, {
502
+ name : "non-string key struct" ,
503
+ args : makeKV (struct {
504
+ F1 string
505
+ F2 int
506
+ }{"arg" , 123 }, "val" ),
507
+ values : makeKV (struct {
508
+ F1 string
509
+ F2 int
510
+ }{"value" , 456 }, "val" ),
511
+ builtins : makeKV (struct {
512
+ F1 string
513
+ F2 int
514
+ }{"builtin" , 789 }, "val" ),
515
+ expectKV : `"<non-string-key: {"F1":"builtin",>"="val" "<non-string-key: {"F1":"value","F>"="val" "<non-string-key: {"F1":"arg","F2">"="val"` ,
516
+ expectJSON : `{"<non-string-key: {"F1":"builtin",>":"val","<non-string-key: {"F1":"value","F>":"val","<non-string-key: {"F1":"arg","F2">":"val"}` ,
492
517
}}
493
518
494
- fKV := NewFormatter (Options {})
495
- fJSON := NewFormatterJSON (Options {})
496
519
for _ , tc := range testCases {
497
520
t .Run (tc .name , func (t * testing.T ) {
498
- t .Run ("KV" , func (t * testing.T ) {
499
- r := fKV .render (tc .kv , nil )
500
- if r != tc .expectKV {
501
- t .Errorf ("wrong KV output:\n expected %q\n got %q" , tc .expectKV , r )
521
+ test := func (t * testing.T , formatter Formatter , expect string ) {
522
+ formatter .AddValues (tc .values )
523
+ r := formatter .render (tc .builtins , tc .args )
524
+ if r != expect {
525
+ t .Errorf ("wrong output:\n expected %q\n got %q" , expect , r )
502
526
}
527
+ }
528
+ t .Run ("KV" , func (t * testing.T ) {
529
+ test (t , NewFormatter (Options {}), tc .expectKV )
503
530
})
504
531
t .Run ("JSON" , func (t * testing.T ) {
505
- r := fJSON .render (tc .kv , nil )
506
- if r != tc .expectJSON {
507
- t .Errorf ("wrong JSON output:\n expected %q\n got %q" , tc .expectJSON , r )
508
- }
532
+ test (t , NewFormatterJSON (Options {}), tc .expectJSON )
509
533
})
510
534
})
511
535
}
@@ -805,6 +829,11 @@ func TestInfoWithValues(t *testing.T) {
805
829
values : makeKV ("one" , 1 , "two" , 2 ),
806
830
args : makeKV ("k" , "v" ),
807
831
expect : ` "level"=0 "msg"="msg" "one"=1 "two"=2 "k"="v"` ,
832
+ }, {
833
+ name : "dangling" ,
834
+ values : makeKV ("dangling" ),
835
+ args : makeKV ("k" , "v" ),
836
+ expect : ` "level"=0 "msg"="msg" "dangling"="<no-value>" "k"="v"` ,
808
837
}}
809
838
810
839
for _ , tc := range testCases {
@@ -841,6 +870,11 @@ func TestErrorWithValues(t *testing.T) {
841
870
values : makeKV ("one" , 1 , "two" , 2 ),
842
871
args : makeKV ("k" , "v" ),
843
872
expect : ` "msg"="msg" "error"="err" "one"=1 "two"=2 "k"="v"` ,
873
+ }, {
874
+ name : "dangling" ,
875
+ values : makeKV ("dangling" ),
876
+ args : makeKV ("k" , "v" ),
877
+ expect : ` "msg"="msg" "error"="err" "dangling"="<no-value>" "k"="v"` ,
844
878
}}
845
879
846
880
for _ , tc := range testCases {
0 commit comments