This repository has been archived by the owner on Nov 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_test.go
95 lines (89 loc) · 2.78 KB
/
buffer_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
93
94
95
package log_test
import (
"errors"
"testing"
"github.com/livebud/log"
"github.com/matryer/is"
)
func TestSampleLog(t *testing.T) {
is := is.New(t)
log := log.Buffer()
log.Info("hello")
log.Info("world")
log.Warnf("hello %s!", "mars")
log.Fields(map[string]interface{}{
"file": "memory_test.go",
"detail": "file not exist",
}).Field("one", "two").Errorf("%d. oh noz", 1)
entries := log.Entries()
is.Equal(len(entries), 4)
is.Equal(entries[0].Level.String(), "info")
is.Equal(entries[0].Message, "hello")
is.Equal(entries[1].Level.String(), "info")
is.Equal(entries[1].Message, "world")
is.Equal(entries[2].Level.String(), "warn")
is.Equal(entries[2].Message, "hello mars!")
is.Equal(entries[3].Level.String(), "error")
is.Equal(entries[3].Message, "1. oh noz")
fields := entries[3].Fields
is.Equal(len(fields), 3)
is.Equal(fields.Keys()[0], "detail")
is.Equal(fields.Get(fields.Keys()[0]), "file not exist")
is.Equal(fields.Keys()[1], "file")
is.Equal(fields.Get(fields.Keys()[1]), "memory_test.go")
is.Equal(fields.Keys()[2], "one")
is.Equal(fields.Get(fields.Keys()[2]), "two")
}
func TestErrLog(t *testing.T) {
is := is.New(t)
log := log.Buffer()
log.Error(errors.New("one"), "two", "three")
entries := log.Entries()
is.Equal(len(entries), 1)
is.Equal(entries[0].Level.String(), "error")
is.Equal(entries[0].Message, "one two three")
fields := entries[0].Fields
is.Equal(len(fields), 0)
}
func TestSampleHandler(t *testing.T) {
is := is.New(t)
handler := log.Buffer()
log := log.New(handler)
log.Info("hello")
log.Info("world")
log.Warnf("hello %s!", "mars")
log.Fields(map[string]interface{}{
"file": "memory_test.go",
"detail": "file not exist",
}).Field("one", "two").Errorf("%d. oh noz", 1)
entries := handler.Entries()
is.Equal(len(entries), 4)
is.Equal(entries[0].Level.String(), "info")
is.Equal(entries[0].Message, "hello")
is.Equal(entries[1].Level.String(), "info")
is.Equal(entries[1].Message, "world")
is.Equal(entries[2].Level.String(), "warn")
is.Equal(entries[2].Message, "hello mars!")
is.Equal(entries[3].Level.String(), "error")
is.Equal(entries[3].Message, "1. oh noz")
fields := entries[3].Fields
is.Equal(len(fields), 3)
is.Equal(fields.Keys()[0], "detail")
is.Equal(fields.Get(fields.Keys()[0]), "file not exist")
is.Equal(fields.Keys()[1], "file")
is.Equal(fields.Get(fields.Keys()[1]), "memory_test.go")
is.Equal(fields.Keys()[2], "one")
is.Equal(fields.Get(fields.Keys()[2]), "two")
}
func TestErrHandler(t *testing.T) {
is := is.New(t)
handler := log.Buffer()
log := log.New(handler)
log.Error(errors.New("one"), "two", "three")
entries := handler.Entries()
is.Equal(len(entries), 1)
is.Equal(entries[0].Level.String(), "error")
is.Equal(entries[0].Message, "one two three")
fields := entries[0].Fields
is.Equal(len(fields), 0)
}