MCPcopy Index your code
hub / github.com/coder/slog / TestContextErrorFormatting

Function TestContextErrorFormatting

internal/entryhuman/entry_test.go:289–333  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

287}
288
289func TestContextErrorFormatting(t *testing.T) {
290 t.Parallel()
291
292 tests := []struct {
293 name string
294 err error
295 expected string
296 }{
297 {
298 name: "DeadlineExceeded",
299 err: context.DeadlineExceeded,
300 expected: "context deadline exceeded",
301 },
302 {
303 name: "Canceled",
304 err: context.Canceled,
305 expected: "context canceled",
306 },
307 {
308 name: "WrappedDeadlineExceeded",
309 err: fmt.Errorf("request failed: %w", context.DeadlineExceeded),
310 expected: "request failed: context deadline exceeded",
311 },
312 }
313
314 for _, tc := range tests {
315 tc := tc
316 t.Run(tc.name, func(t *testing.T) {
317 t.Parallel()
318
319 var buf bytes.Buffer
320 entryhuman.Fmt(&buf, io.Discard, slog.SinkEntry{
321 Level: slog.LevelError,
322 Message: "something failed",
323 Fields: slog.M(
324 slog.Error(tc.err),
325 ),
326 })
327
328 got := buf.String()
329 assert.False(t, "error not empty object", strings.Contains(got, "error={}"))
330 assert.True(t, "error contains expected string", strings.Contains(got, tc.expected))
331 })
332 }
333}
334
335func BenchmarkFmt(b *testing.B) {
336 bench := func(b *testing.B, color bool) {

Callers

nothing calls this directly

Calls 6

FmtFunction · 0.92
FalseFunction · 0.92
TrueFunction · 0.92
ErrorfMethod · 0.80
StringMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected