Skip to content

Commit

Permalink
test(log): add unit test for pkg/log
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamjz committed Nov 16, 2023
1 parent 9eff29c commit 38bb039
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package log

import (
"log/slog"
"testing"

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

func TestParseLevel(t *testing.T) {
for _, tc := range []struct {
name string
lvl string
parsedLevel slog.Level
hasErr bool
}{
{
name: "Empty string",
lvl: "",
parsedLevel: 0,
hasErr: true,
},
{
name: "Uppercase level",
lvl: "DEBUG",
parsedLevel: DebugLevel,
hasErr: false,
},
{
name: "Debug",
lvl: "debug",
parsedLevel: DebugLevel,
hasErr: false,
},
{
name: "Info",
lvl: "info",
parsedLevel: InfoLevel,
hasErr: false,
},
{
name: "Warn",
lvl: "warn",
parsedLevel: WarnLevel,
hasErr: false,
},
{
name: "Error",
lvl: "error",
parsedLevel: ErrorLevel,
hasErr: false,
},
{
name: "Unsupported level",
lvl: "XXX",
parsedLevel: 0,
hasErr: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
l, err := ParseLevel(tc.lvl)

assert.Equal(t, tc.parsedLevel, l)

if tc.hasErr {
assert.NotNil(t, err)
assert.ErrorContains(t, err, "unrecognized level: ")
} else {
assert.Nil(t, err)
}
})
}
}

0 comments on commit 38bb039

Please sign in to comment.