(t *testing.T)
| 65 | } |
| 66 | |
| 67 | func 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"}) |
nothing calls this directly
no test coverage detected
searching dependent graphs…