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