...
1
2
3 package zerolog
4
5 import (
6 "bytes"
7 "errors"
8 "strings"
9 "testing"
10 )
11
12 type nilError struct{}
13
14 func (nilError) Error() string {
15 return ""
16 }
17
18 func TestEvent_AnErr(t *testing.T) {
19 tests := []struct {
20 name string
21 err error
22 want string
23 }{
24 {"nil", nil, `{}`},
25 {"error", errors.New("test"), `{"err":"test"}`},
26 {"nil interface", func() *nilError { return nil }(), `{}`},
27 }
28 for _, tt := range tests {
29 t.Run(tt.name, func(t *testing.T) {
30 var buf bytes.Buffer
31 e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
32 e.AnErr("err", tt.err)
33 _ = e.write()
34 if got, want := strings.TrimSpace(buf.String()), tt.want; got != want {
35 t.Errorf("Event.AnErr() = %v, want %v", got, want)
36 }
37 })
38 }
39 }
40
41 func TestEvent_ObjectWithNil(t *testing.T) {
42 var buf bytes.Buffer
43 e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
44 _ = e.Object("obj", nil)
45 _ = e.write()
46
47 want := `{"obj":null}`
48 got := strings.TrimSpace(buf.String())
49 if got != want {
50 t.Errorf("Event.Object() = %q, want %q", got, want)
51 }
52 }
53
54 func TestEvent_EmbedObjectWithNil(t *testing.T) {
55 var buf bytes.Buffer
56 e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
57 _ = e.EmbedObject(nil)
58 _ = e.write()
59
60 want := "{}"
61 got := strings.TrimSpace(buf.String())
62 if got != want {
63 t.Errorf("Event.EmbedObject() = %q, want %q", got, want)
64 }
65 }
66
View as plain text