forked from go-co-op/gocron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
112 lines (102 loc) · 2.21 KB
/
logger_test.go
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
package gocron
import (
"bytes"
"log"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNoOpLogger(_ *testing.T) {
noOp := noOpLogger{}
noOp.Debug("debug", "arg1", "arg2")
noOp.Error("error", "arg1", "arg2")
noOp.Info("info", "arg1", "arg2")
noOp.Warn("warn", "arg1", "arg2")
}
func TestNewLogger(t *testing.T) {
tests := []struct {
name string
level LogLevel
}{
{
"debug",
LogLevelDebug,
},
{
"info",
LogLevelInfo,
},
{
"warn",
LogLevelWarn,
},
{
"error",
LogLevelError,
},
{
"Less than error",
-1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var results bytes.Buffer
l := &logger{
level: tt.level,
log: log.New(&results, "", log.LstdFlags),
}
var noArgs []any
oneArg := []any{"arg1"}
twoArgs := []any{"arg1", "arg2"}
var noArgsStr []string
oneArgStr := []string{"arg1"}
twoArgsStr := []string{"arg1", "arg2"}
for _, args := range []struct {
argsAny []any
argsStr []string
}{
{noArgs, noArgsStr},
{oneArg, oneArgStr},
{twoArgs, twoArgsStr},
} {
l.Debug("debug", args.argsAny...)
if tt.level >= LogLevelDebug {
r := results.String()
assert.Contains(t, r, "DEBUG: debug")
assert.Contains(t, r, strings.Join(args.argsStr, "="))
} else {
assert.Empty(t, results.String())
}
results.Reset()
l.Info("info", args.argsAny...)
if tt.level >= LogLevelInfo {
r := results.String()
assert.Contains(t, r, "INFO: info")
assert.Contains(t, r, strings.Join(args.argsStr, "="))
} else {
assert.Empty(t, results.String())
}
results.Reset()
l.Warn("warn", args.argsAny...)
if tt.level >= LogLevelWarn {
r := results.String()
assert.Contains(t, r, "WARN: warn")
assert.Contains(t, r, strings.Join(args.argsStr, "="))
} else {
assert.Empty(t, results.String())
}
results.Reset()
l.Error("error", args.argsAny...)
if tt.level >= LogLevelError {
r := results.String()
assert.Contains(t, r, "ERROR: error")
assert.Contains(t, r, strings.Join(args.argsStr, "="))
} else {
assert.Empty(t, results.String())
}
results.Reset()
}
})
}
}