Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ var levelStrings = map[string]Level{

// String implementation.
func (l Level) String() string {
if l < DebugLevel || int(l) >= len(levelNames) {
return "invalid"
}
return levelNames[l]
}

Expand Down
27 changes: 27 additions & 0 deletions levels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,30 @@ func TestLevel_UnmarshalJSON(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, InfoLevel, e.Level)
}

func TestLevel_String(t *testing.T) {
cases := []struct {
Level Level
String string
}{
{DebugLevel, "debug"},
{InfoLevel, "info"},
{WarnLevel, "warn"},
{ErrorLevel, "error"},
{FatalLevel, "fatal"},
{InvalidLevel, "invalid"},
{Level(99), "invalid"},
}

for _, c := range cases {
t.Run(c.String, func(t *testing.T) {
assert.Equal(t, c.String, c.Level.String())
})
}
}

func TestLevel_MarshalJSON_Invalid(t *testing.T) {
b, err := json.Marshal(InvalidLevel)
assert.NoError(t, err)
assert.Equal(t, `"invalid"`, string(b))
}