log/slog: add test case for level_test.go

adds a test case for the MarshalJSON and MarshalText method of the Level type in the slog package.

Change-Id: I3f79f0b46c41252ad9d743e03e34503e19998f3e
GitHub-Last-Rev: dab00d4c206ca59fcca7ee8d97ca8cdc9475fdce
GitHub-Pull-Request: golang/go#65525
Reviewed-on: https://go-review.googlesource.com/c/go/+/561315
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
pgxiaolianzi 2024-02-07 07:45:14 +00:00 committed by Jonathan Amsterdam
parent cfe7f21dd5
commit b6ca586181

View File

@ -5,6 +5,7 @@
package slog
import (
"bytes"
"flag"
"strings"
"testing"
@ -50,12 +51,16 @@ func TestLevelVar(t *testing.T) {
}
func TestMarshalJSON(t *testing.T) {
func TestLevelMarshalJSON(t *testing.T) {
want := LevelWarn - 3
wantData := []byte(`"INFO+1"`)
data, err := want.MarshalJSON()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, wantData) {
t.Errorf("got %s, want %s", string(data), string(wantData))
}
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
wantData := []byte("INFO+1")
data, err := want.MarshalText()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, wantData) {
t.Errorf("got %s, want %s", string(data), string(wantData))
}
var got Level
if err := got.UnmarshalText(data); err != nil {
t.Fatal(err)