-
Notifications
You must be signed in to change notification settings - Fork 588
/
Copy pathevent_test.go
126 lines (112 loc) · 2.58 KB
/
event_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//go:build !binary_log
// +build !binary_log
package zerolog
import (
"bytes"
"errors"
"strings"
"testing"
)
type nilError struct{}
func (nilError) Error() string {
return ""
}
func TestEvent_AnErr(t *testing.T) {
tests := []struct {
name string
err error
want string
}{
{"nil", nil, `{}`},
{"error", errors.New("test"), `{"err":"test"}`},
{"nil interface", func() *nilError { return nil }(), `{}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel)
e.AnErr("err", tt.err)
_ = e.write()
if got, want := strings.TrimSpace(buf.String()), tt.want; got != want {
t.Errorf("Event.AnErr() = %v, want %v", got, want)
}
})
}
}
func TestEvent_ObjectWithNil(t *testing.T) {
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel)
_ = e.Object("obj", nil)
_ = e.write()
want := `{"obj":null}`
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.Object() = %q, want %q", got, want)
}
}
func TestEvent_EmbedObjectWithNil(t *testing.T) {
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel)
_ = e.EmbedObject(nil)
_ = e.write()
want := "{}"
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.EmbedObject() = %q, want %q", got, want)
}
}
func TestEvent_GetFields(t *testing.T) {
type testCase struct {
name string
e *Event
message string
want map[string]interface{}
}
testCases := []testCase{
{
name: "event without message",
e: newEvent(nil, DebugLevel).Str("foo", "bar").Float64("n", 42),
want: map[string]interface{}{
"foo": "bar",
"n": float64(42),
},
},
{
name: "event without message and integer",
e: newEvent(nil, DebugLevel).Str("foo", "bar").Int("n", 42),
want: map[string]interface{}{
"foo": "bar",
"n": float64(42),
},
},
{
name: "event with message",
e: newEvent(nil, DebugLevel).Str("foo", "bar").Float64("n", 42),
message: "test",
want: map[string]interface{}{
"foo": "bar",
"n": float64(42),
"message": "test",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.message != "" {
tc.e.Msg(tc.message)
}
got, err := tc.e.GetMetadata()
if err != nil {
t.Error(err)
}
if len(got) != len(tc.want) {
t.Errorf("Event.GetFields() = %v, want %v", len(got), len(tc.want))
}
for k, v := range tc.want {
if got[k] != v {
t.Errorf("Event.GetFields() = %v, want %v", got[k], v)
}
}
})
}
}