Skip to content

Commit

Permalink
add Disabled to String and ParseLevel; add tests (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilcrest authored Apr 13, 2021
1 parent 98f889f commit 582f0cf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func (l Level) String() string {
return "fatal"
case PanicLevel:
return "panic"
case Disabled:
return "disabled"
case NoLevel:
return ""
}
Expand All @@ -171,6 +173,8 @@ func ParseLevel(levelStr string) (Level, error) {
return FatalLevel, nil
case LevelFieldMarshalFunc(PanicLevel):
return PanicLevel, nil
case LevelFieldMarshalFunc(Disabled):
return Disabled, nil
case LevelFieldMarshalFunc(NoLevel):
return NoLevel, nil
}
Expand Down
59 changes: 59 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,62 @@ func TestUpdateEmptyContext(t *testing.T) {
t.Errorf("invalid log output:\ngot: %q\nwant: %q", got, want)
}
}

func TestLevel_String(t *testing.T) {
tests := []struct {
name string
l Level
want string
}{
{"trace", TraceLevel, "trace"},
{"debug", DebugLevel, "debug"},
{"info", InfoLevel, "info"},
{"warn", WarnLevel, "warn"},
{"error", ErrorLevel, "error"},
{"fatal", FatalLevel, "fatal"},
{"panic", PanicLevel, "panic"},
{"disabled", Disabled, "disabled"},
{"nolevel", NoLevel, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.l.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}

func TestParseLevel(t *testing.T) {
type args struct {
levelStr string
}
tests := []struct {
name string
args args
want Level
wantErr bool
}{
{"trace", args{"trace"}, TraceLevel, false},
{"debug", args{"debug"}, DebugLevel, false},
{"info", args{"info"}, InfoLevel, false},
{"warn", args{"warn"}, WarnLevel, false},
{"error", args{"error"}, ErrorLevel, false},
{"fatal", args{"fatal"}, FatalLevel, false},
{"panic", args{"panic"}, PanicLevel, false},
{"disabled", args{"disabled"}, Disabled, false},
{"nolevel", args{""}, NoLevel, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseLevel(tt.args.levelStr)
if (err != nil) != tt.wantErr {
t.Errorf("ParseLevel() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseLevel() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 582f0cf

Please sign in to comment.