|
4 | 4 | "errors" |
5 | 5 | "io" |
6 | 6 | "os" |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 | ) |
9 | 10 |
|
@@ -53,3 +54,85 @@ func TestHandleCommandOutput_Error(t *testing.T) { |
53 | 54 | t.Errorf("expected 'something failed\\n', got %q", output) |
54 | 55 | } |
55 | 56 | } |
| 57 | + |
| 58 | +func TestHandleFlagError_WithError(t *testing.T) { |
| 59 | + var b strings.Builder |
| 60 | + msg, err := HandleFlagError(&b, errors.New("bad flag")) |
| 61 | + |
| 62 | + if got := b.String(); got != "error parsing flags: bad flag\n" { |
| 63 | + t.Fatalf("builder content mismatch: got %q", got) |
| 64 | + } |
| 65 | + if msg != "" { |
| 66 | + t.Fatalf("expected empty message, got %q", msg) |
| 67 | + } |
| 68 | + if err == nil { |
| 69 | + t.Fatalf("expected non-nil error") |
| 70 | + } |
| 71 | + if err.Error() != "error parsing flags: bad flag" { |
| 72 | + t.Fatalf("unexpected error message: %q", err.Error()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestHandleFlagError_NilError(t *testing.T) { |
| 77 | + var b strings.Builder |
| 78 | + msg, err := HandleFlagError(&b, nil) |
| 79 | + |
| 80 | + if got := b.String(); got != "error parsing flags: <nil>\n" { |
| 81 | + t.Fatalf("builder content mismatch for nil error: got %q", got) |
| 82 | + } |
| 83 | + if msg != "" { |
| 84 | + t.Fatalf("expected empty message, got %q", msg) |
| 85 | + } |
| 86 | + if err == nil { |
| 87 | + t.Fatalf("expected non-nil error when wrapping nil") |
| 88 | + } |
| 89 | + // Document current behavior of fmt.Errorf with %w and nil |
| 90 | + if err.Error() != "error parsing flags: %!w(<nil>)" { |
| 91 | + t.Fatalf("unexpected error message for nil wrap: %q", err.Error()) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestWrapText_EmptyString(t *testing.T) { |
| 96 | + if got := WrapText("", 10); got != "" { |
| 97 | + t.Fatalf("expected empty string, got %q", got) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestWrapText_OnlySpaces(t *testing.T) { |
| 102 | + if got := WrapText(" ", 10); got != " " { |
| 103 | + t.Fatalf("expected to preserve spaces, got %q", got) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestWrapText_NoWrap(t *testing.T) { |
| 108 | + if got := WrapText("hello world", 20); got != "hello world" { |
| 109 | + t.Fatalf("expected 'hello world', got %q", got) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestWrapText_CollapseSpaces(t *testing.T) { |
| 114 | + if got := WrapText("hello world", 20); got != "hello world" { |
| 115 | + t.Fatalf("expected collapsed spaces, got %q", got) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestWrapText_WrapAtWidth(t *testing.T) { |
| 120 | + if got := WrapText("hello world", 10); got != "hello\nworld" { |
| 121 | + t.Fatalf("expected wrap at width, got %q", got) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestWrapText_LongWord(t *testing.T) { |
| 126 | + in := "supercalifragilisticexpialidocious" |
| 127 | + if got := WrapText(in, 10); got != in { |
| 128 | + t.Fatalf("expected long word unchanged, got %q", got) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestWrapText_MultipleLines(t *testing.T) { |
| 133 | + in := "one two three four five" |
| 134 | + expected := "one two\nthree\nfour\nfive" |
| 135 | + if got := WrapText(in, 7); got != expected { |
| 136 | + t.Fatalf("expected %q, got %q", expected, got) |
| 137 | + } |
| 138 | +} |
0 commit comments