@@ -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).
212213func 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" ),
0 commit comments