-
Notifications
You must be signed in to change notification settings - Fork 5
/
log_test.go
88 lines (78 loc) · 1.59 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
package handel
import (
"bytes"
"io"
"io/ioutil"
"testing"
"github.com/go-kit/kit/log"
lvl "github.com/go-kit/kit/log/level"
"github.com/stretchr/testify/require"
)
func TestLoggerKit(t *testing.T) {
const (
Info int = iota
Debug
Warn
Error
)
type logTest struct {
with []interface{}
opts lvl.Option
level int
msg string
out []string
}
w := func(kv ...interface{}) []interface{} {
return kv
}
o := func(outs ...string) []string {
return outs
}
var tests = []logTest{
{nil, nil, Info, "hello", o("hello")},
{nil, lvl.AllowInfo(), Debug, "hello", nil},
{w("yard", "bird"), lvl.AllowWarn(), Warn, "hello", o("yard", "bird", "hello")},
}
for i, test := range tests {
t.Logf(" -- test %d -- ", i)
var b bytes.Buffer
logger := log.NewLogfmtLogger(&b)
if test.opts != nil {
logger = lvl.NewFilter(logger, test.opts)
}
kit := NewKitLoggerFrom(logger)
if test.with != nil {
kit = kit.With(test.with...)
}
var logging func(...interface{})
switch test.level {
case Info:
logging = kit.Info
case Debug:
logging = kit.Debug
case Warn:
logging = kit.Warn
case Error:
logging = kit.Error
default:
t.FailNow()
}
logging("msg", test.msg)
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 := ioutil.ReadAll(r)
require.NoError(t, err)
if !present {
require.Equal(t, string(out), "")
return
}
for _, o := range outs {
require.Contains(t, string(out), o)
}
}