| 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 |
"bytes" |
| 9 |
"flag" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestLevelString(t *testing.T) { |
| 15 |
for _, test := range []struct { |
| 16 |
in Level |
| 17 |
want string |
| 18 |
}{ |
| 19 |
{0, "INFO"}, |
| 20 |
{LevelError, "ERROR"}, |
| 21 |
{LevelError + 2, "ERROR+2"}, |
| 22 |
{LevelError - 2, "WARN+2"}, |
| 23 |
{LevelWarn, "WARN"}, |
| 24 |
{LevelWarn - 1, "INFO+3"}, |
| 25 |
{LevelInfo, "INFO"}, |
| 26 |
{LevelInfo + 1, "INFO+1"}, |
| 27 |
{LevelInfo - 3, "DEBUG+1"}, |
| 28 |
{LevelDebug, "DEBUG"}, |
| 29 |
{LevelDebug - 2, "DEBUG-2"}, |
| 30 |
} { |
| 31 |
got := test.in.String() |
| 32 |
if got != test.want { |
| 33 |
t.Errorf("%d: got %s, want %s", test.in, got, test.want) |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func TestLevelVar(t *testing.T) { |
| 39 |
var al LevelVar |
| 40 |
if got, want := al.Level(), LevelInfo; got != want { |
| 41 |
t.Errorf("got %v, want %v", got, want) |
| 42 |
} |
| 43 |
al.Set(LevelWarn) |
| 44 |
if got, want := al.Level(), LevelWarn; got != want { |
| 45 |
t.Errorf("got %v, want %v", got, want) |
| 46 |
} |
| 47 |
al.Set(LevelInfo) |
| 48 |
if got, want := al.Level(), LevelInfo; got != want { |
| 49 |
t.Errorf("got %v, want %v", got, want) |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
func TestLevelMarshalJSON(t *testing.T) { |
| 55 |
want := LevelWarn - 3 |
| 56 |
wantData := []byte(`"INFO+1"`) |
| 57 |
data, err := want.MarshalJSON() |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
if !bytes.Equal(data, wantData) { |
| 62 |
t.Errorf("got %s, want %s", string(data), string(wantData)) |
| 63 |
} |
| 64 |
var got Level |
| 65 |
if err := got.UnmarshalJSON(data); err != nil { |
| 66 |
t.Fatal(err) |
| 67 |
} |
| 68 |
if got != want { |
| 69 |
t.Errorf("got %s, want %s", got, want) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestLevelMarshalText(t *testing.T) { |
| 74 |
want := LevelWarn - 3 |
| 75 |
wantData := []byte("INFO+1") |
| 76 |
data, err := want.MarshalText() |
| 77 |
if err != nil { |
| 78 |
t.Fatal(err) |
| 79 |
} |
| 80 |
if !bytes.Equal(data, wantData) { |
| 81 |
t.Errorf("got %s, want %s", string(data), string(wantData)) |
| 82 |
} |
| 83 |
var got Level |
| 84 |
if err := got.UnmarshalText(data); err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
if got != want { |
| 88 |
t.Errorf("got %s, want %s", got, want) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestLevelAppendText(t *testing.T) { |
| 93 |
buf := make([]byte, 4, 16) |
| 94 |
want := LevelWarn - 3 |
| 95 |
wantData := []byte("\x00\x00\x00\x00INFO+1") |
| 96 |
data, err := want.AppendText(buf) |
| 97 |
if err != nil { |
| 98 |
t.Fatal(err) |
| 99 |
} |
| 100 |
if !bytes.Equal(data, wantData) { |
| 101 |
t.Errorf("got %s, want %s", string(data), string(wantData)) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestLevelParse(t *testing.T) { |
| 106 |
for _, test := range []struct { |
| 107 |
in string |
| 108 |
want Level |
| 109 |
}{ |
| 110 |
{"DEBUG", LevelDebug}, |
| 111 |
{"INFO", LevelInfo}, |
| 112 |
{"WARN", LevelWarn}, |
| 113 |
{"ERROR", LevelError}, |
| 114 |
{"debug", LevelDebug}, |
| 115 |
{"iNfo", LevelInfo}, |
| 116 |
{"INFO+87", LevelInfo + 87}, |
| 117 |
{"Error-18", LevelError - 18}, |
| 118 |
{"Error-8", LevelInfo}, |
| 119 |
} { |
| 120 |
var got Level |
| 121 |
if err := got.parse(test.in); err != nil { |
| 122 |
t.Fatalf("%q: %v", test.in, err) |
| 123 |
} |
| 124 |
if got != test.want { |
| 125 |
t.Errorf("%q: got %s, want %s", test.in, got, test.want) |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func TestLevelParseError(t *testing.T) { |
| 131 |
for _, test := range []struct { |
| 132 |
in string |
| 133 |
want string // error string should contain this |
| 134 |
}{ |
| 135 |
{"", "unknown name"}, |
| 136 |
{"dbg", "unknown name"}, |
| 137 |
{"INFO+", "invalid syntax"}, |
| 138 |
{"INFO-", "invalid syntax"}, |
| 139 |
{"ERROR+23x", "invalid syntax"}, |
| 140 |
} { |
| 141 |
var l Level |
| 142 |
err := l.parse(test.in) |
| 143 |
if err == nil || !strings.Contains(err.Error(), test.want) { |
| 144 |
t.Errorf("%q: got %v, want string containing %q", test.in, err, test.want) |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestLevelFlag(t *testing.T) { |
| 150 |
fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 151 |
lf := LevelInfo |
| 152 |
fs.TextVar(&lf, "level", lf, "set level") |
| 153 |
err := fs.Parse([]string{"-level", "WARN+3"}) |
| 154 |
if err != nil { |
| 155 |
t.Fatal(err) |
| 156 |
} |
| 157 |
if g, w := lf, LevelWarn+3; g != w { |
| 158 |
t.Errorf("got %v, want %v", g, w) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func TestLevelVarMarshalText(t *testing.T) { |
| 163 |
var v LevelVar |
| 164 |
v.Set(LevelWarn) |
| 165 |
data, err := v.MarshalText() |
| 166 |
if err != nil { |
| 167 |
t.Fatal(err) |
| 168 |
} |
| 169 |
var v2 LevelVar |
| 170 |
if err := v2.UnmarshalText(data); err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
if g, w := v2.Level(), LevelWarn; g != w { |
| 174 |
t.Errorf("got %s, want %s", g, w) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
func TestLevelVarAppendText(t *testing.T) { |
| 179 |
var v LevelVar |
| 180 |
v.Set(LevelWarn) |
| 181 |
buf := make([]byte, 4, 16) |
| 182 |
data, err := v.AppendText(buf) |
| 183 |
if err != nil { |
| 184 |
t.Fatal(err) |
| 185 |
} |
| 186 |
var v2 LevelVar |
| 187 |
if err := v2.UnmarshalText(data[4:]); err != nil { |
| 188 |
t.Fatal(err) |
| 189 |
} |
| 190 |
if g, w := v2.Level(), LevelWarn; g != w { |
| 191 |
t.Errorf("got %s, want %s", g, w) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func TestLevelVarFlag(t *testing.T) { |
| 196 |
fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 197 |
v := &LevelVar{} |
| 198 |
v.Set(LevelWarn + 3) |
| 199 |
fs.TextVar(v, "level", v, "set level") |
| 200 |
err := fs.Parse([]string{"-level", "WARN+3"}) |
| 201 |
if err != nil { |
| 202 |
t.Fatal(err) |
| 203 |
} |
| 204 |
if g, w := v.Level(), LevelWarn+3; g != w { |
| 205 |
t.Errorf("got %v, want %v", g, w) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func TestLevelVarString(t *testing.T) { |
| 210 |
var v LevelVar |
| 211 |
v.Set(LevelError) |
| 212 |
got := v.String() |
| 213 |
want := "LevelVar(ERROR)" |
| 214 |
if got != want { |
| 215 |
t.Errorf("got %q, want %q", got, want) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func BenchmarkLevelString(b *testing.B) { |
| 220 |
levels := []Level{ |
| 221 |
0, |
| 222 |
LevelError, |
| 223 |
LevelError + 2, |
| 224 |
LevelError - 2, |
| 225 |
LevelWarn, |
| 226 |
LevelWarn - 1, |
| 227 |
LevelInfo, |
| 228 |
LevelInfo + 1, |
| 229 |
LevelInfo - 3, |
| 230 |
LevelDebug, |
| 231 |
LevelDebug - 2, |
| 232 |
} |
| 233 |
b.ResetTimer() |
| 234 |
for b.Loop() { |
| 235 |
for _, level := range levels { |
| 236 |
_ = level.String() |
| 237 |
} |
| 238 |
} |
| 239 |
} |