-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
49 lines (41 loc) · 1.01 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
package client
import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"os"
"testing"
)
func TestDefaultLogger(t *testing.T) {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
msg := "test message"
lgr := NewDefaultLogger(DebugLevel)
lgr.Logf(DebugLevel, msg)
outC := make(chan string)
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
outC <- buf.String()
}()
_ = w.Close()
os.Stdout = old
out := <-outC
assert.Equal(t, msg+"\n", out[len(out)-len(msg)-1:])
}
func TestLoggerFunc_Logf(t *testing.T) {
lgr := LoggerFunc(func(level LogLevel, format string, args ...interface{}) {
assert.Equal(t, format, "format")
assert.Len(t, args, 2)
assert.Equal(t, "arg1", args[0])
assert.Equal(t, "arg2", args[1])
})
lgr.Logf(DebugLevel, "format", "arg1", "arg2")
}
func TestLevel_String(t *testing.T) {
assert.Equal(t, "error", ErrorLevel.String())
assert.Equal(t, "warning", WarningLevel.String())
assert.Equal(t, "info", InfoLevel.String())
assert.Equal(t, "debug", DebugLevel.String())
}