Skip to content

Commit 4a82f89

Browse files
committed
slog: improve test for commonHandler
Allow Attrs to vary by test case. Test both text and JSON. Change-Id: I291d723efe4f976451f0ab08a8777b9a74b7347a Reviewed-on: https://go-review.googlesource.com/c/exp/+/435895 Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]>
1 parent 4365e8f commit 4a82f89

File tree

1 file changed

+65
-43
lines changed

1 file changed

+65
-43
lines changed

slog/handler_test.go

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,67 +30,89 @@ func TestDefaultWith(t *testing.T) {
3030
}
3131
}
3232

33-
// NOTE TO REVIEWER: Ignore this test. The next CL revamps it.
34-
func TestCommonHandle(t *testing.T) {
35-
tm := time.Date(2022, 9, 18, 8, 26, 33, 0, time.UTC)
36-
r := NewRecord(tm, InfoLevel, "message", 1)
37-
r.AddAttrs(String("a", "one"), Int("b", 2), Any("", "ignore me"))
38-
39-
newHandler := func(replace func(Attr) Attr) *commonHandler {
40-
return &commonHandler{
41-
app: textAppender{},
42-
attrSep: ' ',
43-
opts: HandlerOptions{ReplaceAttr: replace},
44-
}
45-
}
46-
33+
// Verify the common parts of TextHandler and JSONHandler.
34+
func TestJSONAndTextHandlers(t *testing.T) {
4735
removeAttr := func(a Attr) Attr { return Attr{} }
4836

37+
attrs := []Attr{String("a", "one"), Int("b", 2), Any("", "ignore me")}
38+
preAttrs := []Attr{Int("pre", 3), String("x", "y")}
39+
4940
for _, test := range []struct {
50-
name string
51-
h *commonHandler
52-
want string
41+
name string
42+
replace func(Attr) Attr
43+
preAttrs []Attr
44+
attrs []Attr
45+
wantText string
46+
wantJSON string
5347
}{
5448
{
55-
name: "basic",
56-
h: newHandler(nil),
57-
want: "time=2022-09-18T08:26:33.000Z level=INFO msg=message a=one b=2",
49+
name: "basic",
50+
attrs: attrs,
51+
wantText: "time=2000-01-02T03:04:05.000Z level=INFO msg=message a=one b=2",
52+
wantJSON: `{"time":"2000-01-02T03:04:05Z","level":"INFO","msg":"message","a":"one","b":2}`,
5853
},
5954
{
60-
name: "cap keys",
61-
h: newHandler(upperCaseKey),
62-
want: "TIME=2022-09-18T08:26:33.000Z LEVEL=INFO MSG=message A=one B=2",
55+
name: "cap keys",
56+
replace: upperCaseKey,
57+
attrs: attrs,
58+
wantText: "TIME=2000-01-02T03:04:05.000Z LEVEL=INFO MSG=message A=one B=2",
59+
wantJSON: `{"TIME":"2000-01-02T03:04:05Z","LEVEL":"INFO","MSG":"message","A":"one","B":2}`,
6360
},
6461
{
65-
name: "remove all",
66-
h: newHandler(removeAttr),
67-
want: "",
62+
name: "remove all",
63+
replace: removeAttr,
64+
attrs: attrs,
65+
wantText: "",
66+
wantJSON: `{}`,
6867
},
6968
{
70-
name: "preformatted",
71-
h: newHandler(nil).with([]Attr{Int("pre", 3), String("x", "y")}),
72-
want: "time=2022-09-18T08:26:33.000Z level=INFO msg=message pre=3 x=y a=one b=2",
69+
name: "preformatted",
70+
preAttrs: preAttrs,
71+
attrs: attrs,
72+
wantText: "time=2000-01-02T03:04:05.000Z level=INFO msg=message pre=3 x=y a=one b=2",
73+
wantJSON: `{"time":"2000-01-02T03:04:05Z","level":"INFO","msg":"message","pre":3,"x":"y","a":"one","b":2}`,
7374
},
7475
{
75-
name: "preformatted cap keys",
76-
h: newHandler(upperCaseKey).with([]Attr{Int("pre", 3), String("x", "y")}),
77-
want: "TIME=2022-09-18T08:26:33.000Z LEVEL=INFO MSG=message PRE=3 X=y A=one B=2",
76+
name: "preformatted cap keys",
77+
replace: upperCaseKey,
78+
preAttrs: preAttrs,
79+
attrs: attrs,
80+
wantText: "TIME=2000-01-02T03:04:05.000Z LEVEL=INFO MSG=message PRE=3 X=y A=one B=2",
81+
wantJSON: `{"TIME":"2000-01-02T03:04:05Z","LEVEL":"INFO","MSG":"message","PRE":3,"X":"y","A":"one","B":2}`,
7882
},
7983
{
80-
name: "preformatted remove all",
81-
h: newHandler(removeAttr).with([]Attr{Int("pre", 3), String("x", "y")}),
82-
want: "",
84+
name: "preformatted remove all",
85+
replace: removeAttr,
86+
preAttrs: preAttrs,
87+
attrs: attrs,
88+
wantText: "",
89+
wantJSON: "{}",
8390
},
8491
} {
92+
r := NewRecord(testTime, InfoLevel, "message", 1)
93+
r.AddAttrs(test.attrs...)
94+
var buf bytes.Buffer
95+
opts := HandlerOptions{ReplaceAttr: test.replace}
8596
t.Run(test.name, func(t *testing.T) {
86-
var buf bytes.Buffer
87-
test.h.w = &buf
88-
if err := test.h.handle(r); err != nil {
89-
t.Fatal(err)
90-
}
91-
got := strings.TrimSuffix(buf.String(), "\n")
92-
if got != test.want {
93-
t.Errorf("\ngot %#v\nwant %#v\n", got, test.want)
97+
for _, handler := range []struct {
98+
name string
99+
h Handler
100+
want string
101+
}{
102+
{"text", opts.NewTextHandler(&buf), test.wantText},
103+
{"json", opts.NewJSONHandler(&buf), test.wantJSON},
104+
} {
105+
t.Run(handler.name, func(t *testing.T) {
106+
h := handler.h.With(test.preAttrs)
107+
buf.Reset()
108+
if err := h.Handle(r); err != nil {
109+
t.Fatal(err)
110+
}
111+
got := strings.TrimSuffix(buf.String(), "\n")
112+
if got != handler.want {
113+
t.Errorf("\ngot %#v\nwant %#v\n", got, handler.want)
114+
}
115+
})
94116
}
95117
})
96118
}

0 commit comments

Comments
 (0)