log/slog: add test case for level_test.go

adds a test case for the MarshalJSON method of the Level type in the slog package.
adds a test case for the MarshalText method of the Level type in the slog package.
This commit is contained in:
pgxiaolianzi 2024-02-05 22:10:55 +08:00 committed by GitHub
parent b8ac61e6e6
commit 5d991e1cd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 0 deletions

View File

@ -5,6 +5,7 @@
package slog
import (
"bytes"
"flag"
"strings"
"testing"
@ -52,10 +53,14 @@ func TestLevelVar(t *testing.T) {
func TestMarshalJSON(t *testing.T) {
want := LevelWarn - 3
want_data := []byte{'"', 'I', 'N', 'F', 'O', '+', '1', '"'}
data, err := want.MarshalJSON()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, want_data) {
t.Errorf("got %s, want %s", string(data), string(want_data))
}
var got Level
if err := got.UnmarshalJSON(data); err != nil {
t.Fatal(err)
@ -67,10 +72,14 @@ func TestMarshalJSON(t *testing.T) {
func TestLevelMarshalText(t *testing.T) {
want := LevelWarn - 3
want_data := []byte{'I', 'N', 'F', 'O', '+', '1'}
data, err := want.MarshalText()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, want_data) {
t.Errorf("got %s, want %s", string(data), string(want_data))
}
var got Level
if err := got.UnmarshalText(data); err != nil {
t.Fatal(err)