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