-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer_test.go
75 lines (68 loc) · 1.4 KB
/
writer_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
package stdlogtoapex
import (
"io"
"log"
"testing"
alog "github.com/apex/log"
"github.com/apex/log/handlers/memory"
)
func TestNewWriter(t *testing.T) {
var _ io.Writer = NewWriter()
}
func TestSetOutputToWriter(t *testing.T) {
handler := memory.New()
alog.SetHandler(handler)
writer := NewWriter()
log.SetOutput(writer)
log.Print("Hello!")
if len(handler.Entries) != 1 {
t.Fatal("No log message recorder in handler")
}
entry := handler.Entries[0]
expected := "Hello!\n"
if entry.Message != expected {
t.Errorf("Expected log message %q, got %q", expected, entry.Message)
}
if entry.Level != alog.InfoLevel {
t.Errorf("Expected to log at the default level (%s), but logged at %s.", alog.InfoLevel, entry.Level)
}
}
func TestStripDatePrefix(t *testing.T) {
testCases := []struct {
data []byte
prefixLen int
expected string
}{
{
data: []byte("Hello"),
prefixLen: 0,
expected: "Hello",
},
{
data: []byte("Hello"),
prefixLen: 1,
expected: "ello",
},
{
data: []byte("Hello"),
prefixLen: 5,
expected: "",
},
{
data: []byte("Hello"),
prefixLen: 6,
expected: "Hello",
},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
w := Writer{
prefixLen: tc.prefixLen,
}
s := w.stripDatePrefix(tc.data)
if s != tc.expected {
t.Fatalf("Expected %q, got %q", tc.expected, s)
}
})
}
}