add Disabled to String and ParseLevel; add tests (#307)

This commit is contained in:
Dan Gillis 2021-04-13 01:32:06 -04:00 committed by GitHub
parent 98f889fcde
commit 582f0cf0e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

4
log.go
View File

@ -147,6 +147,8 @@ func (l Level) String() string {
return "fatal" return "fatal"
case PanicLevel: case PanicLevel:
return "panic" return "panic"
case Disabled:
return "disabled"
case NoLevel: case NoLevel:
return "" return ""
} }
@ -171,6 +173,8 @@ func ParseLevel(levelStr string) (Level, error) {
return FatalLevel, nil return FatalLevel, nil
case LevelFieldMarshalFunc(PanicLevel): case LevelFieldMarshalFunc(PanicLevel):
return PanicLevel, nil return PanicLevel, nil
case LevelFieldMarshalFunc(Disabled):
return Disabled, nil
case LevelFieldMarshalFunc(NoLevel): case LevelFieldMarshalFunc(NoLevel):
return NoLevel, nil return NoLevel, nil
} }

View File

@ -788,3 +788,62 @@ func TestUpdateEmptyContext(t *testing.T) {
t.Errorf("invalid log output:\ngot: %q\nwant: %q", got, want) 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)
}
})
}
}