MCPcopy
hub / github.com/rs/zerolog / TestEvent_Object

Function TestEvent_Object

event_test.go:67–157  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

65}
66
67func TestEvent_Object(t *testing.T) {
68 t.Run("ObjectWithNil", func(t *testing.T) {
69 var buf bytes.Buffer
70 e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, false, nil, nil)
71 e = e.Object("obj", nil)
72 err := e.write()
73 if err != nil {
74 t.Errorf("Event.Object() error: %v", err)
75 }
76
77 want := `{"obj":null}`
78 got := strings.TrimSpace(buf.String())
79 if got != want {
80 t.Errorf("Event.Object()\ngot: %s\nwant: %s", got, want)
81 }
82 })
83
84 t.Run("EmbedObjectWithNil", func(t *testing.T) {
85 var buf bytes.Buffer
86 e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, false, nil, nil)
87 e = e.EmbedObject(nil)
88 err := e.write()
89 if err != nil {
90 t.Errorf("Event.EmbedObject() error: %v", err)
91 }
92
93 want := "{}"
94 got := strings.TrimSpace(buf.String())
95 if got != want {
96 t.Errorf("Event.EmbedObject()\ngot: %s\nwant: %s", got, want)
97 }
98 })
99
100 type contextKeyType struct{}
101 var contextKey = contextKeyType{}
102
103 called := false
104 ctxHook := HookFunc(func(e *Event, level Level, message string) {
105 called = true
106 ctx := e.GetCtx()
107 if ctx == nil {
108 t.Errorf("expected context to be set in Event")
109 }
110 val := ctx.Value(contextKey)
111 if val == nil {
112 t.Errorf("expected context value, got %v", val)
113 }
114 e.Str("ctxValue", val.(string))
115 e.Bool("stackValue", e.stack)
116 })
117
118 t.Run("ObjectWithFullContext", func(t *testing.T) {
119 called = false
120 ctx := context.WithValue(context.Background(), contextKey, "ctx-object")
121
122 var buf bytes.Buffer
123 e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, true, ctx, []Hook{ctxHook})
124 e = e.Object("obj", loggableObject{member: "object-value"})

Callers

nothing calls this directly

Calls 11

newEventFunction · 0.85
HookFuncFuncType · 0.85
GetCtxMethod · 0.80
MsgMethod · 0.80
RunMethod · 0.65
ObjectMethod · 0.45
writeMethod · 0.45
StringMethod · 0.45
EmbedObjectMethod · 0.45
StrMethod · 0.45
BoolMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…