Skip to content

Commit

Permalink
add tests for xlog [wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
dmke committed Oct 31, 2024
1 parent 9d10789 commit a383f39
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
4 changes: 2 additions & 2 deletions xlog/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (err ErrorValue) Value() slog.Value {

// LogValue implements [slog.LogValuer].
func (err ErrorValue) LogValue() slog.Value {
return slog.StringValue(err.Error())
return err.Value()

Check warning on line 35 in xlog/attrs.go

View check run for this annotation

Codecov / codecov/patch

xlog/attrs.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}

// Error constructs a first-class error log attribute.
//
// Not to be confused with (xlog.Logger).Error() or (log/slog).Error(),
// which produce an error-level log message.
func Error(err error) slog.Attr {
return slog.Any(ErrorKey, ErrorValue{err})
return Any(ErrorKey, ErrorValue{err})

Check warning on line 43 in xlog/attrs.go

View check run for this annotation

Codecov / codecov/patch

xlog/attrs.go#L42-L43

Added lines #L42 - L43 were not covered by tests
}
7 changes: 7 additions & 0 deletions xlog/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xlog

import (
"errors"
"io"
"log/slog"
"os"
Expand Down Expand Up @@ -47,9 +48,15 @@ func LeveledString(s string) Option {
}
}

var ErrNilWriter = errors.New("invalid writer: nil")

// WriteTo sets the output.
func WriteTo(w io.Writer) Option {
return func(o *options) error {
if w == nil {
return ErrNilWriter
}

o.output = w
return nil
}
Expand Down
114 changes: 114 additions & 0 deletions xlog/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package xlog

import (
"bytes"
"log/slog"
"testing"
"time"

"github.com/stretchr/testify/suite"
)

func TestOptionSuite(t *testing.T) {
suite.Run(t, new(OptionSuite))
}

type OptionSuite struct {
suite.Suite
options
}

func (o *OptionSuite) SetupTest() {
o.level = slog.LevelDebug
}

func (o *OptionSuite) TestLeveled() {
err := Leveled(slog.LevelError)(&o.options)
o.Require().NoError(err)
o.Assert().Equal(slog.LevelError, o.level)

err = Leveled(slog.Level(999))(&o.options)
o.Require().NoError(err)
o.Assert().Equal(slog.Level(999), o.level)
}

func (o *OptionSuite) TestLeveledString_valid() {
for i, tt := range []struct {
input string
expected slog.Level
}{
{"dbg", slog.LevelDebug},
{"debug", slog.LevelDebug},

{"", slog.LevelInfo},
{"info", slog.LevelInfo},
{"INFO", slog.LevelInfo},

{"WARN", slog.LevelWarn},
{"WARNING", slog.LevelWarn},

{"ERR", slog.LevelError},
{"ERROR", slog.LevelError},
{"FaTaL", slog.LevelError},
} {
o.level = slog.Level(100 + i)
err := LeveledString(tt.input)(&o.options)
o.Require().NoError(err)
o.Assert().Equal(tt.expected, o.level)
}
}

func (o *OptionSuite) TestLeveledString_invalid() {
o.level = slog.Level(100)
err := LeveledString("ifno")(&o.options)
o.Require().Equal(slog.Level(100), o.level)
o.Assert().EqualError(err, `unknown log level: "ifno"`)
}

func (o *OptionSuite) TestWriteTo() {
var buf bytes.Buffer
err := WriteTo(&buf)(&o.options)
o.Require().NoError(err)
o.Assert().Equal(&buf, o.output)

err = WriteTo(nil)(&o.options)
o.Assert().EqualError(err, "invalid writer: nil")
}

func (o *OptionSuite) TestMockClock() {
t := time.Now()
err := MockClock(t)(&o.options)
o.Require().NoError(err)
o.Require().NotNil(o.clock)
o.Assert().EqualValues(t, o.clock.Now())
}

func (o *OptionSuite) TestWithSource() {
err := WithSource()(&o.options)
o.Require().NoError(err)
o.Assert().True(o.source)
}

func (o *OptionSuite) TestColor() {
err := Color()(&o.options)
o.Require().NoError(err)
o.Assert().True(o.color)
}

func (o *OptionSuite) TestAsJSON() {
err := AsJSON()(&o.options)
o.Require().NoError(err)
o.Assert().NotNil(o.buildHandler)
}

func (o *OptionSuite) TestAsText() {
err := AsText()(&o.options)
o.Require().NoError(err)
o.Assert().NotNil(o.buildHandler)
}

func (o *OptionSuite) TestDiscard() {
err := Discard()(&o.options)
o.Require().NoError(err)
o.Assert().True(o.discard)
}
4 changes: 2 additions & 2 deletions xlog/xlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func (log *logger) With(a ...slog.Attr) Logger {
// "error" and "fatal". Other input will result in err not being nil.
func ParseLevel(s string) (l slog.Level, err error) {
switch strings.ToLower(s) {
case "debug":
case "dbg", "debug":
l = slog.LevelDebug
case "info", "": // make the zero value useful
l = slog.LevelInfo
case "warn", "warning":
l = slog.LevelWarn
case "error", "fatal":
case "err", "error", "fatal":
l = slog.LevelError
default:
err = fmt.Errorf("unknown log level: %q", s)
Expand Down

0 comments on commit a383f39

Please sign in to comment.