@@ -3,6 +3,7 @@ package console
33import (
44 "bytes"
55 "context"
6+ "errors"
67 "fmt"
78 "log/slog"
89 "os"
@@ -34,6 +35,14 @@ func TestHandler_NoColor(t *testing.T) {
3435 AssertEqual (t , expected , buf .String ())
3536}
3637
38+ type theStringer struct {}
39+
40+ func (t theStringer ) String () string { return "stringer" }
41+
42+ type noStringer struct {
43+ Foo string
44+ }
45+
3746func TestHandler_Attr (t * testing.T ) {
3847 buf := bytes.Buffer {}
3948 h := NewHandler (& buf , & HandlerOptions {NoColor : true })
@@ -48,10 +57,13 @@ func TestHandler_Attr(t *testing.T) {
4857 slog .Time ("time" , now ),
4958 slog .Duration ("dur" , time .Second ),
5059 slog .Group ("group" , slog .String ("foo" , "bar" ), slog .Group ("subgroup" , slog .String ("foo" , "bar" ))),
60+ slog .Any ("err" , errors .New ("the error" )),
61+ slog .Any ("stringer" , theStringer {}),
62+ slog .Any ("nostringer" , noStringer {Foo : "bar" }),
5163 )
5264 AssertNoError (t , h .Handle (context .Background (), rec ))
5365
54- expected := fmt .Sprintf ("%s INF foobar bool=true int=-12 uint=12 float=3.14 foo=bar time=%s dur=1s group.foo=bar group.subgroup.foo=bar\r \n " , now .Format (time .DateTime ), now .Format (time .RFC3339 ))
66+ expected := fmt .Sprintf ("%s INF foobar bool=true int=-12 uint=12 float=3.14 foo=bar time=%s dur=1s group.foo=bar group.subgroup.foo=bar err=the error stringer=stringer nostringer={bar} \r \n " , now .Format (time .DateTime ), now .Format (time .RFC3339 ))
5567 AssertEqual (t , expected , buf .String ())
5668}
5769
@@ -148,3 +160,10 @@ func TestHandler_Source(t *testing.T) {
148160 AssertNoError (t , h2 .Handle (context .Background (), rec ))
149161 AssertEqual (t , fmt .Sprintf ("%s INF foobar\r \n " , now .Format (time .DateTime )), buf .String ())
150162}
163+
164+ func TestHandler_Err (t * testing.T ) {
165+ w := writerFunc (func (b []byte ) (int , error ) { return 0 , errors .New ("nope" ) })
166+ h := NewHandler (w , & HandlerOptions {NoColor : true })
167+ rec := slog .NewRecord (time .Now (), slog .LevelInfo , "foobar" , 0 )
168+ AssertError (t , h .Handle (context .Background (), rec ))
169+ }
0 commit comments