golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/slog/level_test.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slog
     6  
     7  import (
     8  	"flag"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestLevelString(t *testing.T) {
    14  	for _, test := range []struct {
    15  		in   Level
    16  		want string
    17  	}{
    18  		{0, "INFO"},
    19  		{LevelError, "ERROR"},
    20  		{LevelError + 2, "ERROR+2"},
    21  		{LevelError - 2, "WARN+2"},
    22  		{LevelWarn, "WARN"},
    23  		{LevelWarn - 1, "INFO+3"},
    24  		{LevelInfo, "INFO"},
    25  		{LevelInfo + 1, "INFO+1"},
    26  		{LevelInfo - 3, "DEBUG+1"},
    27  		{LevelDebug, "DEBUG"},
    28  		{LevelDebug - 2, "DEBUG-2"},
    29  	} {
    30  		got := test.in.String()
    31  		if got != test.want {
    32  			t.Errorf("%d: got %s, want %s", test.in, got, test.want)
    33  		}
    34  	}
    35  }
    36  
    37  func TestLevelVar(t *testing.T) {
    38  	var al LevelVar
    39  	if got, want := al.Level(), LevelInfo; got != want {
    40  		t.Errorf("got %v, want %v", got, want)
    41  	}
    42  	al.Set(LevelWarn)
    43  	if got, want := al.Level(), LevelWarn; got != want {
    44  		t.Errorf("got %v, want %v", got, want)
    45  	}
    46  	al.Set(LevelInfo)
    47  	if got, want := al.Level(), LevelInfo; got != want {
    48  		t.Errorf("got %v, want %v", got, want)
    49  	}
    50  
    51  }
    52  
    53  func TestMarshalJSON(t *testing.T) {
    54  	want := LevelWarn - 3
    55  	data, err := want.MarshalJSON()
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	var got Level
    60  	if err := got.UnmarshalJSON(data); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	if got != want {
    64  		t.Errorf("got %s, want %s", got, want)
    65  	}
    66  }
    67  
    68  func TestLevelMarshalText(t *testing.T) {
    69  	want := LevelWarn - 3
    70  	data, err := want.MarshalText()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	var got Level
    75  	if err := got.UnmarshalText(data); err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	if got != want {
    79  		t.Errorf("got %s, want %s", got, want)
    80  	}
    81  }
    82  
    83  func TestLevelParse(t *testing.T) {
    84  	for _, test := range []struct {
    85  		in   string
    86  		want Level
    87  	}{
    88  		{"DEBUG", LevelDebug},
    89  		{"INFO", LevelInfo},
    90  		{"WARN", LevelWarn},
    91  		{"ERROR", LevelError},
    92  		{"debug", LevelDebug},
    93  		{"iNfo", LevelInfo},
    94  		{"INFO+87", LevelInfo + 87},
    95  		{"Error-18", LevelError - 18},
    96  		{"Error-8", LevelInfo},
    97  	} {
    98  		var got Level
    99  		if err := got.parse(test.in); err != nil {
   100  			t.Fatalf("%q: %v", test.in, err)
   101  		}
   102  		if got != test.want {
   103  			t.Errorf("%q: got %s, want %s", test.in, got, test.want)
   104  		}
   105  	}
   106  }
   107  
   108  func TestLevelParseError(t *testing.T) {
   109  	for _, test := range []struct {
   110  		in   string
   111  		want string // error string should contain this
   112  	}{
   113  		{"", "unknown name"},
   114  		{"dbg", "unknown name"},
   115  		{"INFO+", "invalid syntax"},
   116  		{"INFO-", "invalid syntax"},
   117  		{"ERROR+23x", "invalid syntax"},
   118  	} {
   119  		var l Level
   120  		err := l.parse(test.in)
   121  		if err == nil || !strings.Contains(err.Error(), test.want) {
   122  			t.Errorf("%q: got %v, want string containing %q", test.in, err, test.want)
   123  		}
   124  	}
   125  }
   126  
   127  func TestLevelFlag(t *testing.T) {
   128  	fs := flag.NewFlagSet("test", flag.ContinueOnError)
   129  	lf := LevelInfo
   130  	fs.TextVar(&lf, "level", lf, "set level")
   131  	err := fs.Parse([]string{"-level", "WARN+3"})
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	if g, w := lf, LevelWarn+3; g != w {
   136  		t.Errorf("got %v, want %v", g, w)
   137  	}
   138  }
   139  
   140  func TestLevelVarMarshalText(t *testing.T) {
   141  	var v LevelVar
   142  	v.Set(LevelWarn)
   143  	data, err := v.MarshalText()
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	var v2 LevelVar
   148  	if err := v2.UnmarshalText(data); err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	if g, w := v2.Level(), LevelWarn; g != w {
   152  		t.Errorf("got %s, want %s", g, w)
   153  	}
   154  }
   155  
   156  func TestLevelVarFlag(t *testing.T) {
   157  	fs := flag.NewFlagSet("test", flag.ContinueOnError)
   158  	v := &LevelVar{}
   159  	v.Set(LevelWarn + 3)
   160  	fs.TextVar(v, "level", v, "set level")
   161  	err := fs.Parse([]string{"-level", "WARN+3"})
   162  	if err != nil {
   163  		t.Fatal(err)
   164  	}
   165  	if g, w := v.Level(), LevelWarn+3; g != w {
   166  		t.Errorf("got %v, want %v", g, w)
   167  	}
   168  }