]> Cypherpunks repositories - gostls13.git/commitdiff
log/slog: add test case for level_test.go
authorpgxiaolianzi <gnnu_d13@163.com>
Wed, 7 Feb 2024 07:45:14 +0000 (07:45 +0000)
committerJonathan Amsterdam <jba@google.com>
Thu, 15 Feb 2024 14:41:24 +0000 (14:41 +0000)
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>
src/log/slog/level_test.go

index 0b28e71e4c6e8bd6fece17e63680497b401c91cb..19adcd8466f1ace883dff038c66ed5c8d0f6b0c4 100644 (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)