| 287 | } |
| 288 | |
| 289 | func 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 | |
| 335 | func BenchmarkFmt(b *testing.B) { |
| 336 | bench := func(b *testing.B, color bool) { |