-
Notifications
You must be signed in to change notification settings - Fork 3
/
entry_test.go
55 lines (47 loc) · 2.07 KB
/
entry_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
package logger_test
import (
"github.com/valyala/bytebufferpool"
"testing"
"github.com/stretchr/testify/assert"
"github.com/gol4ng/logger"
)
func TestEntry_String(t *testing.T) {
tests := []struct {
name string
entry logger.Entry
strings []string
}{
{name: "Empty context", entry: logger.Entry{Message: "log message", Level: logger.DebugLevel, Context: logger.NewContext()}, strings: []string{"<debug> log message [ <nil> ]"}},
{name: "Simple entry", entry: logger.Entry{Message: "log message", Level: logger.DebugLevel, Context: logger.Ctx("my_key", "my_value")}, strings: []string{"<debug> log message [ <my_key:my_value> ]"}},
{name: "Simple entry", entry: logger.Entry{Message: "log message", Level: logger.DebugLevel, Context: logger.Ctx("my_key", "my_value").Add("my_int_val", 3)}, strings: []string{"<debug> log message [", "<my_key:my_value>", "<my_int_val:3>"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
str := tt.entry.String()
for _, s := range tt.strings {
assert.Contains(t, str, s)
}
})
}
}
func TestEntryToString(t *testing.T) {
tests := []struct {
name string
entry logger.Entry
strings []string
}{
{name: "Empty context", entry: logger.Entry{Message: "log message", Level: logger.DebugLevel, Context: logger.NewContext()}, strings: []string{"<debug> log message [ <nil> ]"}},
{name: "Simple entry", entry: logger.Entry{Message: "log message", Level: logger.DebugLevel, Context: logger.Ctx("my_key", "my_value")}, strings: []string{"<debug> log message [ <my_key:my_value> ]"}},
{name: "Simple entry", entry: logger.Entry{Message: "log message", Level: logger.DebugLevel, Context: logger.Ctx("my_key", "my_value").Add("my_int_val", 3)}, strings: []string{"<debug> log message [", "<my_key:my_value>", "<my_int_val:3>"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
byteBuffer := bytebufferpool.Get()
defer bytebufferpool.Put(byteBuffer)
logger.EntryToString(tt.entry, byteBuffer)
for _, s := range tt.strings {
assert.Contains(t, byteBuffer.String(), s)
}
})
}
}