Skip to content

Commit 669c812

Browse files
committed
slog: improve handler test
Write output to a string instead of a map. It's more accurate (e.g. order matters) and closer to a real handler. Change-Id: Ia410ff0dd99bb93db39a6954f7ffd1c5709555cc Reviewed-on: https://go-review.googlesource.com/c/exp/+/432182 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]>
1 parent 46d9e77 commit 669c812

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

slog/handler_test.go

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ package slog
99
import (
1010
"bytes"
1111
"fmt"
12-
"reflect"
1312
"strings"
1413
"testing"
1514
"time"
1615

1716
"golang.org/x/exp/slog/internal/buffer"
1817
)
1918

20-
func TestWith(t *testing.T) {
19+
func TestDefaultWith(t *testing.T) {
2120
d := &defaultHandler{}
2221
if g := len(d.attrs); g != 0 {
2322
t.Errorf("got %d, want 0", g)
@@ -35,25 +34,19 @@ func TestWith(t *testing.T) {
3534
}
3635

3736
func TestCommonHandle(t *testing.T) {
38-
tm := time.Now()
37+
tm := time.Date(2022, 9, 18, 8, 26, 33, 0, time.UTC)
3938
r := NewRecord(tm, InfoLevel, "message", 1)
4039
r.AddAttrs(String("a", "one"), Int("b", 2), Any("", "ignore me"))
4140

4241
for _, test := range []struct {
4342
name string
4443
h *commonHandler
45-
want map[string]any
44+
want string
4645
}{
4746
{
4847
name: "basic",
4948
h: &commonHandler{},
50-
want: map[string]any{
51-
"msg": "message",
52-
"time": tm.Round(0), // strip monotonic
53-
"level": "INFO",
54-
"a": "one",
55-
"b": int64(2),
56-
},
49+
want: "(time=2022-09-18T08:26:33.000Z;level=INFO;msg=message;a=one;b=2)",
5750
},
5851
{
5952
name: "cap keys",
@@ -64,13 +57,7 @@ func TestCommonHandle(t *testing.T) {
6457
},
6558
},
6659
},
67-
want: map[string]any{
68-
"MSG": "message",
69-
"TIME": tm.Round(0), // strip monotonic
70-
"LEVEL": "INFO",
71-
"A": "one",
72-
"B": int64(2),
73-
},
60+
want: "(TIME=2022-09-18T08:26:33.000Z;LEVEL=INFO;MSG=message;A=one;B=2)",
7461
},
7562
{
7663
name: "remove all",
@@ -79,47 +66,59 @@ func TestCommonHandle(t *testing.T) {
7966
ReplaceAttr: func(a Attr) Attr { return Attr{} },
8067
},
8168
},
82-
want: map[string]any{},
69+
// TODO: fix this. The correct result is "()".
70+
want: "(;;)",
8371
},
8472
} {
8573
t.Run(test.name, func(t *testing.T) {
86-
ma := &memAppender{m: map[string]any{}}
87-
test.h.w = &bytes.Buffer{}
88-
test.h.newAppender = func(*buffer.Buffer) appender { return ma }
74+
var buf bytes.Buffer
75+
test.h.w = &buf
76+
test.h.newAppender = func(buf *buffer.Buffer) appender {
77+
return &testAppender{buf}
78+
}
8979
if err := test.h.handle(r); err != nil {
9080
t.Fatal(err)
9181
}
92-
if got := ma.m; !reflect.DeepEqual(got, test.want) {
82+
got := strings.TrimSuffix(buf.String(), "\n")
83+
if got != test.want {
9384
t.Errorf("\ngot %#v\nwant %#v\n", got, test.want)
9485
}
9586
})
9687
}
9788
}
9889

99-
type memAppender struct {
100-
key string
101-
m map[string]any
90+
type testAppender struct {
91+
buf *buffer.Buffer
10292
}
10393

104-
func (a *memAppender) set(v any) { a.m[a.key] = v }
94+
func (a *testAppender) appendStart() { a.buf.WriteByte('(') }
95+
func (a *testAppender) appendSep() { a.buf.WriteByte(';') }
96+
func (a *testAppender) appendEnd() { a.buf.WriteByte(')') }
10597

106-
func (a *memAppender) appendStart() {}
107-
func (a *memAppender) appendSep() {}
108-
func (a *memAppender) appendEnd() {}
109-
func (a *memAppender) appendKey(key string) { a.key = key }
110-
func (a *memAppender) appendString(s string) { a.set(s) }
98+
func (a *testAppender) appendKey(key string) {
99+
a.appendString(key)
100+
a.buf.WriteByte('=')
101+
}
102+
func (a *testAppender) appendString(s string) { a.buf.WriteString(s) }
111103

112-
func (a *memAppender) appendTime(t time.Time) error {
113-
a.set(t)
104+
func (a *testAppender) appendTime(t time.Time) error {
105+
*a.buf = appendTimeRFC3339Millis(*a.buf, t)
114106
return nil
115107
}
116108

117-
func (a *memAppender) appendSource(file string, line int) {
118-
a.set(fmt.Sprintf("%s:%d", file, line))
109+
func (a *testAppender) appendSource(file string, line int) {
110+
a.appendString(fmt.Sprintf("%s:%d", file, line))
119111
}
120112

121-
func (a *memAppender) appendAttrValue(at Attr) error {
122-
a.set(at.Value())
113+
func (a *testAppender) appendAttrValue(at Attr) error {
114+
switch at.Kind() {
115+
case StringKind:
116+
a.appendString(at.str())
117+
case TimeKind:
118+
a.appendTime(at.Time())
119+
default:
120+
*a.buf = at.appendValue(*a.buf)
121+
}
123122
return nil
124123
}
125124

0 commit comments

Comments
 (0)