| 1 |
// File: tests/caption_test.go |
| 2 |
package tests |
| 3 |
|
| 4 |
import ( |
| 5 |
"bytes" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/olekukonko/tablewriter/renderer" |
| 9 |
|
| 10 |
"github.com/olekukonko/tablewriter" |
| 11 |
"github.com/olekukonko/tablewriter/tw" // Assuming your tw types (CaptionPosition, Align) are here |
| 12 |
) |
| 13 |
|
| 14 |
// TestTableCaptions comprehensive tests for caption functionality |
| 15 |
func TestTableCaptions(t *testing.T) { |
| 16 |
data := [][]string{ |
| 17 |
{"Alice", "30", "New York"}, |
| 18 |
{"Bob", "24", "San Francisco"}, |
| 19 |
{"Charlie", "35", "London"}, |
| 20 |
} |
| 21 |
headers := []string{"Name", "Age", "City"} |
| 22 |
shortCaption := "User Data" |
| 23 |
longCaption := "This is a detailed caption for the user data table, intended to demonstrate text wrapping and alignment features." |
| 24 |
|
| 25 |
baseTableSetup := func(buf *bytes.Buffer) *tablewriter.Table { |
| 26 |
table := tablewriter.NewTable(buf, |
| 27 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{Symbols: tw.NewSymbols(tw.StyleASCII)})), |
| 28 |
tablewriter.WithDebug(true), |
| 29 |
) |
| 30 |
table.Header(headers) |
| 31 |
for _, v := range data { |
| 32 |
table.Append(v) |
| 33 |
} |
| 34 |
return table |
| 35 |
} |
| 36 |
|
| 37 |
t.Run("NoCaption", func(t *testing.T) { |
| 38 |
var buf bytes.Buffer |
| 39 |
table := baseTableSetup(&buf) |
| 40 |
table.Render() |
| 41 |
expected := ` |
| 42 |
+---------+-----+---------------+ |
| 43 |
| NAME | AGE | CITY | |
| 44 |
+---------+-----+---------------+ |
| 45 |
| Alice | 30 | New York | |
| 46 |
| Bob | 24 | San Francisco | |
| 47 |
| Charlie | 35 | London | |
| 48 |
+---------+-----+---------------+ |
| 49 |
` |
| 50 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 51 |
t.Log(table.Debug().String()) |
| 52 |
} |
| 53 |
}) |
| 54 |
|
| 55 |
t.Run("LegacySetCaption_BottomCenter", func(t *testing.T) { |
| 56 |
var buf bytes.Buffer |
| 57 |
table := baseTableSetup(&buf) |
| 58 |
table.Caption(tw.Caption{Text: shortCaption}) // Legacy, defaults to BottomCenter, auto width |
| 59 |
table.Render() |
| 60 |
// Width of table: 7+3+15 + 4 borders/separators = 29. "User Data" is 9. |
| 61 |
// (29-9)/2 = 10 padding left. |
| 62 |
expected := ` |
| 63 |
+---------+-----+---------------+ |
| 64 |
| NAME | AGE | CITY | |
| 65 |
+---------+-----+---------------+ |
| 66 |
| Alice | 30 | New York | |
| 67 |
| Bob | 24 | San Francisco | |
| 68 |
| Charlie | 35 | London | |
| 69 |
+---------+-----+---------------+ |
| 70 |
User Data |
| 71 |
` |
| 72 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 73 |
t.Log(table.Debug().String()) |
| 74 |
} |
| 75 |
}) |
| 76 |
|
| 77 |
t.Run("CaptionBottomCenter_AutoWidthBottom", func(t *testing.T) { |
| 78 |
var buf bytes.Buffer |
| 79 |
table := baseTableSetup(&buf) |
| 80 |
table.Caption(tw.Caption{Text: shortCaption, Spot: tw.SpotBottomCenter, Align: tw.AlignCenter}) |
| 81 |
table.Render() |
| 82 |
expected := ` |
| 83 |
+---------+-----+---------------+ |
| 84 |
| NAME | AGE | CITY | |
| 85 |
+---------+-----+---------------+ |
| 86 |
| Alice | 30 | New York | |
| 87 |
| Bob | 24 | San Francisco | |
| 88 |
| Charlie | 35 | London | |
| 89 |
+---------+-----+---------------+ |
| 90 |
User Data |
| 91 |
` |
| 92 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 93 |
t.Log(table.Debug().String()) |
| 94 |
} |
| 95 |
}) |
| 96 |
|
| 97 |
t.Run("CaptionTopCenter_AutoWidthTop", func(t *testing.T) { |
| 98 |
var buf bytes.Buffer |
| 99 |
table := baseTableSetup(&buf) |
| 100 |
table.Caption(tw.Caption{Text: shortCaption, Spot: tw.SpotTopCenter, Align: tw.AlignCenter}) |
| 101 |
table.Render() |
| 102 |
expected := ` |
| 103 |
User Data |
| 104 |
+---------+-----+---------------+ |
| 105 |
| NAME | AGE | CITY | |
| 106 |
+---------+-----+---------------+ |
| 107 |
| Alice | 30 | New York | |
| 108 |
| Bob | 24 | San Francisco | |
| 109 |
| Charlie | 35 | London | |
| 110 |
+---------+-----+---------------+ |
| 111 |
` |
| 112 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 113 |
t.Log(table.Debug().String()) |
| 114 |
} |
| 115 |
}) |
| 116 |
|
| 117 |
t.Run("CaptionBottomLeft_AutoWidth", func(t *testing.T) { |
| 118 |
var buf bytes.Buffer |
| 119 |
table := baseTableSetup(&buf) |
| 120 |
table.Caption(tw.Caption{Text: shortCaption, Spot: tw.SpotBottomLeft, Align: tw.AlignLeft}) |
| 121 |
table.Render() |
| 122 |
expected := ` |
| 123 |
+---------+-----+---------------+ |
| 124 |
| NAME | AGE | CITY | |
| 125 |
+---------+-----+---------------+ |
| 126 |
| Alice | 30 | New York | |
| 127 |
| Bob | 24 | San Francisco | |
| 128 |
| Charlie | 35 | London | |
| 129 |
+---------+-----+---------------+ |
| 130 |
User Data |
| 131 |
` |
| 132 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 133 |
t.Log(table.Debug().String()) |
| 134 |
} |
| 135 |
}) |
| 136 |
|
| 137 |
t.Run("CaptionTopRight_AutoWidth", func(t *testing.T) { |
| 138 |
var buf bytes.Buffer |
| 139 |
table := baseTableSetup(&buf) |
| 140 |
table.Caption(tw.Caption{Text: shortCaption, Spot: tw.SpotTopRight, Align: tw.AlignRight}) |
| 141 |
table.Render() |
| 142 |
expected := ` |
| 143 |
User Data |
| 144 |
+---------+-----+---------------+ |
| 145 |
| NAME | AGE | CITY | |
| 146 |
+---------+-----+---------------+ |
| 147 |
| Alice | 30 | New York | |
| 148 |
| Bob | 24 | San Francisco | |
| 149 |
| Charlie | 35 | London | |
| 150 |
+---------+-----+---------------+ |
| 151 |
` |
| 152 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 153 |
t.Log(table.Debug().String()) |
| 154 |
} |
| 155 |
}) |
| 156 |
|
| 157 |
t.Run("CaptionBottomCenter_LongCaption_AutoWidth", func(t *testing.T) { |
| 158 |
var buf bytes.Buffer |
| 159 |
table := baseTableSetup(&buf) |
| 160 |
table.Caption(tw.Caption{Text: longCaption, Spot: tw.SpotBottomCenter, Align: tw.AlignCenter}) |
| 161 |
table.Render() |
| 162 |
// Table width is 29. Long caption will wrap to this. |
| 163 |
// "This is a detailed caption for" (29) |
| 164 |
// "the user data table, intended" (29) |
| 165 |
// "to demonstrate text wrapping" (28) |
| 166 |
// "and alignment features." (25) |
| 167 |
expected := ` |
| 168 |
+---------+-----+---------------+ |
| 169 |
| NAME | AGE | CITY | |
| 170 |
+---------+-----+---------------+ |
| 171 |
| Alice | 30 | New York | |
| 172 |
| Bob | 24 | San Francisco | |
| 173 |
| Charlie | 35 | London | |
| 174 |
+---------+-----+---------------+ |
| 175 |
This is a detailed caption for |
| 176 |
the user data table, intended |
| 177 |
to demonstrate text wrapping and |
| 178 |
alignment features. |
| 179 |
` |
| 180 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 181 |
t.Log(table.Debug().String()) |
| 182 |
} |
| 183 |
}) |
| 184 |
|
| 185 |
t.Run("CaptionTopLeft_LongCaption_MaxWidth20", func(t *testing.T) { |
| 186 |
var buf bytes.Buffer |
| 187 |
table := baseTableSetup(&buf) |
| 188 |
// captionMaxWidth 20, table width is 29. Caption wraps to 20. Padded to 29 (table width) for alignment. |
| 189 |
table.Caption(tw.Caption{Text: longCaption, Spot: tw.SpotTopLeft, Align: tw.AlignLeft, Width: 20}) |
| 190 |
table.Render() |
| 191 |
|
| 192 |
// The visual check normalizes spaces, so the alignment padding to table width is tricky to test visually for left/right aligned captions. |
| 193 |
// It's more about the wrapping width for the caption text itself. |
| 194 |
// The printTopBottomCaption will align the *block* of wrapped text. |
| 195 |
// Let's adjust the expected output: the lines are padded to actualTableWidth. |
| 196 |
// The caption lines themselves are max 20 wide. |
| 197 |
expectedAdjusted := ` |
| 198 |
This is a detailed |
| 199 |
caption for the user |
| 200 |
data table, intended |
| 201 |
to demonstrate |
| 202 |
text wrapping and |
| 203 |
alignment features. |
| 204 |
+---------+-----+---------------+ |
| 205 |
| NAME | AGE | CITY | |
| 206 |
+---------+-----+---------------+ |
| 207 |
| Alice | 30 | New York | |
| 208 |
| Bob | 24 | San Francisco | |
| 209 |
| Charlie | 35 | London | |
| 210 |
+---------+-----+---------------+ |
| 211 |
` |
| 212 |
if !visualCheckCaption(t, t.Name(), buf.String(), expectedAdjusted) { |
| 213 |
t.Log(table.Debug().String()) |
| 214 |
} |
| 215 |
}) |
| 216 |
|
| 217 |
t.Run("CaptionBottomCenter_EmptyTable", func(t *testing.T) { |
| 218 |
var buf bytes.Buffer |
| 219 |
table := tablewriter.NewWriter(&buf) |
| 220 |
// No header, no data |
| 221 |
table.Caption(tw.Caption{Text: shortCaption, Spot: tw.SpotBottomCenter, Align: tw.AlignCenter}) |
| 222 |
table.Render() |
| 223 |
// Expected: table is empty box, caption centered to its own width or a default. |
| 224 |
// Empty table with default borders prints: |
| 225 |
// +--+ |
| 226 |
// +--+ |
| 227 |
// If actualTableWidth is 0, captionWrapWidth becomes natural width of caption (9 for "User Data") |
| 228 |
// Then paddingTargetWidth also becomes 9. |
| 229 |
expected := ` |
| 230 |
+--+ |
| 231 |
+--+ |
| 232 |
User Data |
| 233 |
` |
| 234 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 235 |
t.Log(table.Debug().String()) |
| 236 |
} |
| 237 |
}) |
| 238 |
|
| 239 |
t.Run("CaptionTopLeft_EmptyTable_MaxWidth10", func(t *testing.T) { |
| 240 |
var buf bytes.Buffer |
| 241 |
table := tablewriter.NewWriter(&buf) |
| 242 |
table.Caption(tw.Caption{Text: "A very long caption text.", Spot: tw.SpotTopLeft, Align: tw.AlignLeft, Width: 10}) |
| 243 |
table.Render() |
| 244 |
// Table is empty, captionMaxWidth is 10. |
| 245 |
// "A very" |
| 246 |
// "long" |
| 247 |
// "caption" |
| 248 |
// "text." |
| 249 |
// Each line left-aligned within width 10. |
| 250 |
expected := ` |
| 251 |
A very |
| 252 |
long |
| 253 |
caption |
| 254 |
text. |
| 255 |
+--+ |
| 256 |
+--+ |
| 257 |
` |
| 258 |
if !visualCheckCaption(t, t.Name(), buf.String(), expected) { |
| 259 |
t.Log(table.Debug().String()) |
| 260 |
} |
| 261 |
}) |
| 262 |
} |