Add tests for Client's SetLogger method

This commit introduces unit tests for the Client's SetLogger method. It verifies the correct logger type is set and ensures setting a nil logger does not override an existing logger.
This commit is contained in:
Winni Neessen 2024-11-11 17:49:07 +01:00
parent 9412f31874
commit 5e3d14f842
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -3169,6 +3169,30 @@ func TestClient_SetDebugLog(t *testing.T) {
}) })
} }
func TestClient_SetLogger(t *testing.T) {
t.Run("set logger to Stdlog logger", func(t *testing.T) {
client := &Client{}
client.SetLogger(log.New(os.Stderr, log.LevelDebug))
if !strings.EqualFold(fmt.Sprintf("%T", client.logger), "*log.Stdlog") {
t.Errorf("expected logger to be of type *log.Stdlog, got: %T", client.logger)
}
})
t.Run("set logger to JSONlog logger", func(t *testing.T) {
client := &Client{}
client.SetLogger(log.NewJSON(os.Stderr, log.LevelDebug))
if !strings.EqualFold(fmt.Sprintf("%T", client.logger), "*log.JSONlog") {
t.Errorf("expected logger to be of type *log.JSONlog, got: %T", client.logger)
}
})
t.Run("nil logger should just return and not set/override", func(t *testing.T) {
client := &Client{logger: log.NewJSON(os.Stderr, log.LevelDebug)}
client.SetLogger(nil)
if !strings.EqualFold(fmt.Sprintf("%T", client.logger), "*log.JSONlog") {
t.Errorf("expected logger to be of type *log.JSONlog, got: %T", client.logger)
}
})
}
// faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes. // faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes.
type faker struct { type faker struct {
io.ReadWriter io.ReadWriter