-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathllog_test.go
92 lines (77 loc) · 1.56 KB
/
llog_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
package llog
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"time"
)
func TestInfo(t *testing.T) {
stdo := os.Stdout
r, w, _ := os.Pipe()
expected := new(bytes.Buffer)
os.Stdout = w
_, _ = fmt.Fprintf(
expected,
"%v[%v] %v%v\n",
"\033[36m",
time.Now().Format("2006/01/02-15:04:05"),
"boo",
"\033[0m",
)
Info("boo")
outC := make(chan []byte)
// copy the output in a separate goroutine so printing can't block
// indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.Bytes()
}()
// back to normal state
w.Close()
os.Stdout = stdo // restoring the real stdout
out := <-outC
if !(bytes.Equal(expected.Bytes(), out)) {
t.Errorf("\nexp %+v\n got %+v\n", expected.Bytes(), out)
}
}
func TestLoggerInfo(t *testing.T) {
output := new(bytes.Buffer)
expected := new(bytes.Buffer)
_, _ = fmt.Fprintf(
expected,
"%v[%v] %v%v\n",
"\033[36m",
time.Now().Format("2006/01/02-15:04:05"),
"boo",
"\033[0m",
)
logger := &Logger{
output,
}
logger.Info("boo")
if !(bytes.Equal(expected.Bytes(), output.Bytes())) {
t.Errorf("\nexp %+v\n got %+v\n", expected.Bytes(), output.Bytes())
}
}
func TestLoggerInfof(t *testing.T) {
output := new(bytes.Buffer)
expected := new(bytes.Buffer)
_, _ = fmt.Fprintf(
expected,
"%v[%v] %v%v\n",
"\033[36m",
time.Now().Format("2006/01/02-15:04:05"),
"boo 1 2",
"\033[0m",
)
logger := &Logger{
output,
}
logger.Infof("boo %d %d", 1, 2)
if !(bytes.Equal(expected.Bytes(), output.Bytes())) {
t.Errorf("\nexp %+v\n got %+v\n", expected.String(), output.String())
}
}