-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel_test.go
More file actions
107 lines (92 loc) · 2.1 KB
/
Copy pathlevel_test.go
File metadata and controls
107 lines (92 loc) · 2.1 KB
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
package slogx
import (
"errors"
"log/slog"
"testing"
)
func TestLevel_IsValid(t *testing.T) {
t.Parallel()
cases := []struct {
in Level
want bool
}{
{DebugLevel, true},
{InfoLevel, true},
{WarnLevel, true},
{ErrorLevel, true},
{Level(""), false},
{Level("trace"), false},
}
for _, tc := range cases {
if got := tc.in.IsValid(); got != tc.want {
t.Errorf("Level(%q).IsValid() = %v, want %v", tc.in, got, tc.want)
}
}
}
func TestLevel_String(t *testing.T) {
t.Parallel()
cases := map[Level]string{
DebugLevel: "debug",
InfoLevel: "info",
WarnLevel: "warn",
ErrorLevel: "error",
}
for in, want := range cases {
if got := in.String(); got != want {
t.Errorf("Level(%q).String() = %q, want %q", in, got, want)
}
}
}
func TestLevel_Slog(t *testing.T) {
t.Parallel()
cases := map[Level]slog.Level{
DebugLevel: slog.LevelDebug,
InfoLevel: slog.LevelInfo,
WarnLevel: slog.LevelWarn,
ErrorLevel: slog.LevelError,
Level("bogus"): slog.LevelInfo, // fallback branch
}
for in, want := range cases {
if got := in.Slog(); got != want {
t.Errorf("Level(%q).Slog() = %v, want %v", in, got, want)
}
}
}
func TestParseLevel(t *testing.T) {
t.Parallel()
t.Run("valid", func(t *testing.T) {
t.Parallel()
for _, l := range AllLevelValues() {
got, err := ParseLevel(string(l))
if err != nil {
t.Errorf("ParseLevel(%q) unexpected error: %v", l, err)
continue
}
if got != l {
t.Errorf("ParseLevel(%q) = %v, want %v", l, got, l)
}
}
})
t.Run("invalid", func(t *testing.T) {
t.Parallel()
_, err := ParseLevel("trace")
if err == nil {
t.Fatal("ParseLevel(\"trace\") returned nil error")
}
if !errors.Is(err, ErrInvalidLevel) {
t.Errorf("err does not wrap ErrInvalidLevel: %v", err)
}
})
}
func TestAllLevelValues_ReturnsCopy(t *testing.T) {
t.Parallel()
a := AllLevelValues()
if len(a) == 0 {
t.Fatal("AllLevelValues returned an empty slice")
}
a[0] = Level("mutated")
b := AllLevelValues()
if b[0] == Level("mutated") {
t.Errorf("mutating the result of AllLevelValues affected a subsequent call")
}
}