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

Function TestWith

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

Source from the content-addressed store, hash-verified

99}
100
101func TestWith(t *testing.T) {
102 out := &bytes.Buffer{}
103 ctx := New(out).With().
104 Str("string", "foo").
105 Stringer("stringer", net.IP{127, 0, 0, 1}).
106 Stringer("stringer_nil", nil).
107 Bytes("bytes", []byte("bar")).
108 Hex("hex", []byte{0x12, 0xef}).
109 RawJSON("json", []byte(`{"some":"json"}`)).
110 AnErr("some_err", nil).
111 Err(errors.New("some error")).
112 Bool("bool", true).
113 Int("int", 1).
114 Int8("int8", 2).
115 Int16("int16", 3).
116 Int32("int32", 4).
117 Int64("int64", 5).
118 Uint("uint", 6).
119 Uint8("uint8", 7).
120 Uint16("uint16", 8).
121 Uint32("uint32", 9).
122 Uint64("uint64", 10).
123 Float32("float32", 11.101).
124 Float64("float64", 12.30303).
125 Time("time", time.Time{}).
126 Dur("dur", 3).
127 Ctx(context.Background()).
128 Any("any", "test").
129 Interface("interface", struct {
130 Pub string
131 Tag string `json:"tag"`
132 }{"a", "b"}).
133 Type("type", math.Phi)
134 _, file, line, _ := runtime.Caller(0)
135 caller := fmt.Sprintf("%s:%d", file, line+3)
136 log := ctx.Caller().Logger()
137 log.Log().Msg("")
138 if got, want := decodeIfBinaryToString(out.Bytes()),
139 `{"string":"foo","stringer":"127.0.0.1","stringer_nil":null,"bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11.101,"float64":12.30303,"time":"0001-01-01T00:00:00Z","dur":0.000003,"any":"test","interface":{"Pub":"a","tag":"b"},"type":"float64","caller":"`+caller+`"}`+"\n"; got != want {
140 t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
141 }
142
143 // Validate CallerWithSkipFrameCount.
144 out.Reset()
145 ctx = New(out).With()
146 _, file, line, _ = runtime.Caller(0)
147 caller = fmt.Sprintf("%s:%d", file, line+5)
148 log = ctx.CallerWithSkipFrameCount(3).Logger()
149 func() {
150 log.Log().Msg("")
151 }()
152 // The above line is a little contrived, but the line above should be the line due
153 // to the extra frame skip.
154 if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`"}`+"\n"; got != want {
155 t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
156 }
157}
158

Callers

nothing calls this directly

Calls 15

NewFunction · 0.85
WithMethod · 0.80
LoggerMethod · 0.80
MsgMethod · 0.80
ResetMethod · 0.80
decodeIfBinaryToStringFunction · 0.70
ErrMethod · 0.65
LogMethod · 0.65
TypeMethod · 0.45
InterfaceMethod · 0.45
AnyMethod · 0.45

Tested by

no test coverage detected