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

Function TestHooks

zapcore/hook_test.go:34–83  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32)
33
34func TestHooks(t *testing.T) {
35 tests := []struct {
36 entryLevel Level
37 coreLevel Level
38 expectCall bool
39 }{
40 {DebugLevel, InfoLevel, false},
41 {InfoLevel, InfoLevel, true},
42 {WarnLevel, InfoLevel, true},
43 }
44
45 for _, tt := range tests {
46 fac, logs := observer.New(tt.coreLevel)
47
48 // sanity check
49 require.Equal(t, tt.coreLevel, LevelOf(fac), "Original logger has the wrong level")
50
51 intField := makeInt64Field("foo", 42)
52 ent := Entry{Message: "bar", Level: tt.entryLevel}
53
54 var called int
55 f := func(e Entry) error {
56 called++
57 assert.Equal(t, ent, e, "Hook called with unexpected Entry.")
58 return nil
59 }
60
61 h := RegisterHooks(fac, f)
62 if ce := h.With([]Field{intField}).Check(ent, nil); ce != nil {
63 ce.Write()
64 }
65
66 t.Run("LevelOf", func(t *testing.T) {
67 assert.Equal(t, tt.coreLevel, LevelOf(h), "Wrapped logger has the wrong log level")
68 })
69
70 if tt.expectCall {
71 assert.Equal(t, 1, called, "Expected to call hook once.")
72 assert.Equal(
73 t,
74 []observer.LoggedEntry{{Entry: ent, Context: []Field{intField}}},
75 logs.AllUntimed(),
76 "Unexpected logs written out.",
77 )
78 } else {
79 assert.Equal(t, 0, called, "Didn't expect to call hook.")
80 assert.Equal(t, 0, logs.Len(), "Unexpected logs written out.")
81 }
82 }
83}

Callers

nothing calls this directly

Calls 9

NewFunction · 0.92
LevelOfFunction · 0.85
makeInt64FieldFunction · 0.85
RegisterHooksFunction · 0.85
AllUntimedMethod · 0.80
CheckMethod · 0.65
WithMethod · 0.65
WriteMethod · 0.65
LenMethod · 0.45

Tested by

no test coverage detected