Skip to content

Commit

Permalink
move warning to dedicated unmarshal test
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
  • Loading branch information
florianl committed May 13, 2024
1 parent 3aefb87 commit 126ea63
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions zapcore/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ func TestLevelText(t *testing.T) {
{"info", InfoLevel},
{"", InfoLevel}, // make the zero value useful
{"warn", WarnLevel},
{"warning", WarnLevel}, // compatibility handling for packages that emit "warning" instead of "warn"
{"error", ErrorLevel},
{"dpanic", DPanicLevel},
{"panic", PanicLevel},
{"fatal", FatalLevel},
}
for _, tt := range tests {
if tt.text != "" && tt.text != "warning" {
if tt.text != "" {
lvl := tt.level
marshaled, err := lvl.MarshalText()
assert.NoError(t, err, "Unexpected error marshaling level %v to text.", &lvl)
Expand All @@ -76,6 +75,17 @@ func TestLevelText(t *testing.T) {
assert.NoError(t, err, `Unexpected error unmarshaling text %q to level.`, tt.text)
assert.Equal(t, tt.level, unmarshaled, `Text %q unmarshaled to an unexpected level.`, tt.text)
}

// Some logging libraries are using "warning" instead of "warn" as level indicator. Handle this case
// for cross compatability.
t.Run("unmarshal warning compatability", func(t *testing.T) {
var unmarshaled Level
input := []byte("warning")
err := unmarshaled.UnmarshalText(input)
assert.NoError(t, err, `Unexpected error unmarshaling text %q to level.`, string(input))
assert.Equal(t, WarnLevel, unmarshaled, `Text %q unmarshaled to an unexpected level.`, string(input))

})
}

func TestParseLevel(t *testing.T) {
Expand Down

0 comments on commit 126ea63

Please sign in to comment.