| 463 | } |
| 464 | |
| 465 | func TestTruncate(t *testing.T) { |
| 466 | tests := []struct { |
| 467 | name string |
| 468 | input string |
| 469 | maxLen int |
| 470 | want string |
| 471 | }{ |
| 472 | { |
| 473 | name: "short string", |
| 474 | input: "hello", |
| 475 | maxLen: 10, |
| 476 | want: "hello", |
| 477 | }, |
| 478 | { |
| 479 | name: "exact length", |
| 480 | input: "hello", |
| 481 | maxLen: 5, |
| 482 | want: "hello", |
| 483 | }, |
| 484 | { |
| 485 | name: "long string", |
| 486 | input: "hello world this is a long string", |
| 487 | maxLen: 10, |
| 488 | want: "hello worl...", |
| 489 | }, |
| 490 | } |
| 491 | |
| 492 | for _, tt := range tests { |
| 493 | t.Run(tt.name, func(t *testing.T) { |
| 494 | got := truncate(tt.input, tt.maxLen) |
| 495 | if got != tt.want { |
| 496 | t.Errorf("truncate() = %q, want %q", got, tt.want) |
| 497 | } |
| 498 | }) |
| 499 | } |
| 500 | } |