Skip to content

Commit 6d17803

Browse files
committed
fix/update tests
Signed-off-by: Callum Styan <[email protected]>
1 parent 5816218 commit 6d17803

File tree

2 files changed

+72
-35
lines changed

2 files changed

+72
-35
lines changed

internal/entryhuman/entry_test.go

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ func TestEntry(t *testing.T) {
208208
})
209209
}
210210

211-
// we can remove this if we are okay with removing the existing Fmt function and replacing it with OptimizedFmt (can return an error)
211+
// Verifies that OptimizedFmt returtns the same result as Fmt.
212+
// We can remove this if we are okay with removing the existing Fmt function and replacing it with OptimizedFmt (can return an error).
212213
func TestEntry_Optimized(t *testing.T) {
213214
t.Parallel()
214215

@@ -326,43 +327,80 @@ func TestEntry_Optimized(t *testing.T) {
326327
),
327328
},
328329
},
329-
}
330-
if *updateGoldenFiles {
331-
ents, err := os.ReadDir("testdata")
332-
if err != nil {
333-
t.Fatal(err)
334-
}
335-
for _, ent := range ents {
336-
os.Remove("testdata/" + ent.Name())
337-
}
330+
{
331+
"primitiveTypes",
332+
slog.SinkEntry{
333+
Level: slog.LevelInfo,
334+
Message: "primitives",
335+
Time: kt,
336+
Fields: slog.M(
337+
slog.F("bool_true", true),
338+
slog.F("bool_false", false),
339+
slog.F("int", 42),
340+
slog.F("int8", int8(-8)),
341+
slog.F("int16", int16(-16)),
342+
slog.F("int32", int32(-32)),
343+
slog.F("int64", int64(-64)),
344+
slog.F("uint", uint(42)),
345+
slog.F("uint8", uint8(8)),
346+
slog.F("uint16", uint16(16)),
347+
slog.F("uint32", uint32(32)),
348+
slog.F("uint64", uint64(64)),
349+
slog.F("float32", float32(3.14)),
350+
slog.F("float64", 2.71828),
351+
),
352+
},
353+
},
354+
{
355+
"primitiveEdgeCases",
356+
slog.SinkEntry{
357+
Level: slog.LevelWarn,
358+
Message: "edge cases",
359+
Time: kt,
360+
Fields: slog.M(
361+
slog.F("zero_int", 0),
362+
slog.F("neg_int", -999),
363+
slog.F("max_int64", int64(9223372036854775807)),
364+
slog.F("min_int64", int64(-9223372036854775808)),
365+
slog.F("max_uint64", uint64(18446744073709551615)),
366+
slog.F("zero_float", 0.0),
367+
slog.F("neg_float", -123.456),
368+
),
369+
},
370+
},
371+
{
372+
"mixedPrimitiveAndComplex",
373+
slog.SinkEntry{
374+
Level: slog.LevelDebug,
375+
Message: "mixed types",
376+
Time: kt,
377+
Fields: slog.M(
378+
slog.F("count", 100),
379+
slog.F("name", "test"),
380+
slog.F("enabled", true),
381+
slog.F("ratio", 0.95),
382+
slog.F("data", []byte("bytes")),
383+
slog.F("nil_val", nil),
384+
),
385+
},
386+
},
338387
}
339388

340389
for _, tc := range ents {
341390
tc := tc
342391
t.Run(tc.name, func(t *testing.T) {
343392
t.Parallel()
344-
goldenPath := fmt.Sprintf("testdata/%s.golden", tc.name)
345-
346-
var gotBuf bytes.Buffer
347-
err := entryhuman.OptimizedFmt(&gotBuf, io.Discard, tc.ent)
348-
if err != nil {
349-
t.Fatal(err)
350-
}
351393

352-
if *updateGoldenFiles {
353-
err := os.WriteFile(goldenPath, gotBuf.Bytes(), 0o644)
354-
if err != nil {
355-
t.Fatal(err)
356-
}
357-
return
358-
}
394+
var fmtBuf bytes.Buffer
395+
var optBuf bytes.Buffer
359396

360-
wantByt, err := os.ReadFile(goldenPath)
397+
entryhuman.Fmt(&fmtBuf, io.Discard, tc.ent)
398+
err := entryhuman.OptimizedFmt(&optBuf, io.Discard, tc.ent)
361399
if err != nil {
362400
t.Fatal(err)
363401
}
364402

365-
assert.Equal(t, "entry matches", string(wantByt), gotBuf.String())
403+
assert.Equal(t, "outputs match", fmtBuf.String(), optBuf.String())
366404
})
367405
}
368406

@@ -378,7 +416,7 @@ func TestEntry_Optimized(t *testing.T) {
378416

379417
done := make(chan struct{}, 2)
380418
go func() {
381-
entryhuman.Fmt(new(bytes.Buffer), f, slog.SinkEntry{
419+
entryhuman.OptimizedFmt(new(bytes.Buffer), f, slog.SinkEntry{
382420
Level: slog.LevelCritical,
383421
Fields: slog.M(
384422
slog.F("hey", "hi"),

sloggers/sloghuman/sloghuman_bench_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func multiline(n int) string {
2727
}
2828

2929
// Benchmarks target the human sink path: humanSink.LogEntry -> entryhuman.Fmt -> bufio.Scanner indent.
30-
3130
func BenchmarkHumanSinkLogEntry_SingleLine(b *testing.B) {
3231
s := sloghuman.Sink(io.Discard)
3332
ent := slog.SinkEntry{
@@ -127,7 +126,7 @@ func BenchmarkFmt_SingleLine(b *testing.B) {
127126
b.ReportAllocs()
128127
b.ResetTimer()
129128
for i := 0; i < b.N; i++ {
130-
entryhuman.Fmt(bytes.NewBuffer(nil), w, ent)
129+
entryhuman.OptimizedFmt(bytes.NewBuffer(nil), w, ent)
131130
}
132131
}
133132

@@ -145,7 +144,7 @@ func BenchmarkFmt_MultilineField_Small(b *testing.B) {
145144
b.ReportAllocs()
146145
b.ResetTimer()
147146
for i := 0; i < b.N; i++ {
148-
entryhuman.Fmt(bytes.NewBuffer(nil), w, ent)
147+
entryhuman.OptimizedFmt(bytes.NewBuffer(nil), w, ent)
149148
}
150149
}
151150

@@ -163,7 +162,7 @@ func BenchmarkFmt_MultilineField_Large(b *testing.B) {
163162
b.ReportAllocs()
164163
b.ResetTimer()
165164
for i := 0; i < b.N; i++ {
166-
entryhuman.Fmt(bytes.NewBuffer(nil), w, ent)
165+
entryhuman.OptimizedFmt(bytes.NewBuffer(nil), w, ent)
167166
}
168167
}
169168

@@ -178,7 +177,7 @@ func BenchmarkFmt_MultilineMessage(b *testing.B) {
178177
b.ReportAllocs()
179178
b.ResetTimer()
180179
for i := 0; i < b.N; i++ {
181-
entryhuman.Fmt(bytes.NewBuffer(nil), w, ent)
180+
entryhuman.OptimizedFmt(bytes.NewBuffer(nil), w, ent)
182181
}
183182
}
184183

@@ -194,7 +193,7 @@ func BenchmarkFmt_WithNames(b *testing.B) {
194193
b.ReportAllocs()
195194
b.ResetTimer()
196195
for i := 0; i < b.N; i++ {
197-
entryhuman.Fmt(bytes.NewBuffer(nil), w, ent)
196+
entryhuman.OptimizedFmt(bytes.NewBuffer(nil), w, ent)
198197
}
199198
}
200199

@@ -209,7 +208,7 @@ func BenchmarkFmt_WithSpan(b *testing.B) {
209208
b.ReportAllocs()
210209
b.ResetTimer()
211210
for i := 0; i < b.N; i++ {
212-
entryhuman.Fmt(bytes.NewBuffer(nil), w, ent)
211+
entryhuman.OptimizedFmt(bytes.NewBuffer(nil), w, ent)
213212
}
214213
}
215214

@@ -233,6 +232,6 @@ func BenchmarkFmt_WithValidSpan(b *testing.B) {
233232
b.ReportAllocs()
234233
b.ResetTimer()
235234
for i := 0; i < b.N; i++ {
236-
entryhuman.Fmt(bytes.NewBuffer(nil), w, ent)
235+
entryhuman.OptimizedFmt(bytes.NewBuffer(nil), w, ent)
237236
}
238237
}

0 commit comments

Comments
 (0)