MCPcopy
hub / github.com/uber-go/zap / TestObserver

Function TestObserver

zaptest/observer/observer_test.go:42–74  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

40}
41
42func TestObserver(t *testing.T) {
43 observer, logs := New(zap.InfoLevel)
44 assertEmpty(t, logs)
45
46 t.Run("LevelOf", func(t *testing.T) {
47 assert.Equal(t, zap.InfoLevel, zapcore.LevelOf(observer), "Observer reported the wrong log level.")
48 })
49
50 assert.NoError(t, observer.Sync(), "Unexpected failure in no-op Sync")
51
52 obs := zap.New(observer).With(zap.Int("i", 1))
53 obs.Info("foo")
54 obs.Debug("bar")
55 want := []LoggedEntry{{
56 Entry: zapcore.Entry{Level: zap.InfoLevel, Message: "foo"},
57 Context: []zapcore.Field{zap.Int("i", 1)},
58 }}
59
60 assert.Equal(t, 1, logs.Len(), "Unexpected observed logs Len.")
61 assert.Equal(t, want, logs.AllUntimed(), "Unexpected contents from AllUntimed.")
62
63 all := logs.All()
64 require.Equal(t, 1, len(all), "Unexpected number of LoggedEntries returned from All.")
65 assert.NotEqual(t, time.Time{}, all[0].Time, "Expected non-zero time on LoggedEntry.")
66
67 // copy & zero time for stable assertions
68 untimed := append([]LoggedEntry{}, all...)
69 untimed[0].Time = time.Time{}
70 assert.Equal(t, want, untimed, "Unexpected LoggedEntries from All.")
71
72 assert.Equal(t, all, logs.TakeAll(), "Expected All and TakeAll to return identical results.")
73 assertEmpty(t, logs)
74}
75
76func TestObserverWith(t *testing.T) {
77 sf1, logs := New(zap.InfoLevel)

Callers

nothing calls this directly

Calls 13

LevelOfFunction · 0.92
NewFunction · 0.92
IntFunction · 0.92
assertEmptyFunction · 0.85
AllUntimedMethod · 0.80
AllMethod · 0.80
TakeAllMethod · 0.80
NewFunction · 0.70
SyncMethod · 0.65
WithMethod · 0.65
InfoMethod · 0.45
DebugMethod · 0.45

Tested by

no test coverage detected