| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // --- visibleWidth --- |
| 9 | |
| 10 | func TestVisibleWidthPlain(t *testing.T) { |
| 11 | if got := visibleWidth("hello"); got != 5 { |
| 12 | t.Errorf("visibleWidth(hello) = %d, want 5", got) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | func TestVisibleWidthEmpty(t *testing.T) { |
| 17 | if got := visibleWidth(""); got != 0 { |
| 18 | t.Errorf("visibleWidth(\"\") = %d, want 0", got) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestVisibleWidthANSI(t *testing.T) { |
| 23 | // ANSI SGR codes should not count toward width. |
| 24 | colored := "\x1b[31mhello\x1b[0m" |
| 25 | if got := visibleWidth(colored); got != 5 { |
| 26 | t.Errorf("visibleWidth(colored) = %d, want 5", got) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // --- padRight --- |
| 31 | |
| 32 | func TestPadRightAlreadyWide(t *testing.T) { |
| 33 | got := padRight("hello", 3) |
| 34 | if got != "hello" { |
| 35 | t.Errorf("padRight(hello, 3) = %q, want hello", got) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestPadRightExact(t *testing.T) { |
| 40 | got := padRight("hello", 5) |
| 41 | if got != "hello" { |
| 42 | t.Errorf("padRight(hello, 5) = %q, want hello", got) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestPadRightPads(t *testing.T) { |
| 47 | got := padRight("hi", 5) |
| 48 | if got != "hi " { |
| 49 | t.Errorf("padRight(hi, 5) = %q, want %q", got, "hi ") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestPadRightEmpty(t *testing.T) { |
| 54 | got := padRight("", 3) |
| 55 | if got != " " { |
| 56 | t.Errorf("padRight(\"\", 3) = %q, want %q", got, " ") |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // --- boxed --- |
| 61 | |
| 62 | func TestBoxedSingleLine(t *testing.T) { |
| 63 | got := boxed([]string{"hello"}) |
| 64 | // Should contain the content and the box characters. |
| 65 | if len(got) == 0 { |
| 66 | t.Error("boxed should not be empty") |
| 67 | } |
| 68 | // Should end with newline. |
| 69 | if got[len(got)-1] != '\n' { |
| 70 | t.Error("boxed should end with newline") |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestBoxedMultipleLines(t *testing.T) { |
| 75 | got := boxed([]string{"line1", "longer line", "short"}) |
| 76 | if len(got) == 0 { |
| 77 | t.Error("boxed should not be empty") |
| 78 | } |
| 79 | // All lines should be present. |
| 80 | for _, want := range []string{"line1", "longer line", "short"} { |
| 81 | if !strings.Contains(got, want) { |
| 82 | t.Errorf("boxed missing %q", want) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestBoxedEmpty(t *testing.T) { |
| 88 | got := boxed([]string{}) |
| 89 | if len(got) == 0 { |
| 90 | t.Error("boxed empty should still produce a box") |
| 91 | } |
| 92 | } |
| 93 |