forked from drand/drand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
90 lines (77 loc) · 1.75 KB
/
log_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
package log
import (
"bufio"
"bytes"
"io"
"testing"
"go.uber.org/zap/zapcore"
"github.com/stretchr/testify/require"
)
func TestLoggerKit(t *testing.T) {
type logTest struct {
with []interface{}
level int
allowedLvl int
msg string
out []string
}
w := func(kv ...interface{}) []interface{} {
return kv
}
o := func(outs ...string) []string {
return outs
}
var tests = []logTest{
{nil, LogInfo, LogInfo, "hello", o("hello")},
{nil, LogDebug, LogInfo, "hello", nil},
{nil, LogError, LogDebug, "hello", o("hello")},
{nil, LogWarn, LogError, "hello", nil},
{nil, LogWarn, LogDebug, "hello", o("hello")},
{w("yard", "bird"), LogWarn, LogInfo, "hello", o("yard", "bird", "hello")},
}
for i, test := range tests {
t.Logf(" -- test %d -- ", i)
var b bytes.Buffer
writer := bufio.NewWriter(&b)
syncer := zapcore.AddSync(writer)
var logging func(...interface{})
logger := NewLogger(syncer, test.allowedLvl)
if test.with != nil {
logger = logger.With(test.with...)
}
switch test.level {
case LogInfo:
logging = logger.Info
case LogDebug:
logging = logger.Debug
case LogWarn:
logging = logger.Warn
case LogError:
logging = logger.Error
case LogFatal:
logging = logger.Fatal
case LogPanic:
logging = logger.Panic
default:
t.FailNow()
}
logging("msg=", test.msg)
writer.Flush()
if test.out != nil {
requireContains(t, &b, test.out, true)
} else {
requireContains(t, &b, nil, false)
}
}
}
func requireContains(t *testing.T, r io.Reader, outs []string, present bool) {
out, err := io.ReadAll(r)
require.NoError(t, err)
if !present {
require.Equal(t, string(out), "")
return
}
for _, o := range outs {
require.Contains(t, string(out), o)
}
}