| 1 |
package tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/fatih/color" |
| 10 |
"github.com/olekukonko/tablewriter" |
| 11 |
"github.com/olekukonko/tablewriter/pkg/twwidth" |
| 12 |
"github.com/olekukonko/tablewriter/renderer" |
| 13 |
"github.com/olekukonko/tablewriter/tw" |
| 14 |
) |
| 15 |
|
| 16 |
type Cleaner string |
| 17 |
|
| 18 |
// Note: Format() overrides String() if both exist. |
| 19 |
func (c Cleaner) Format() string { |
| 20 |
return clean(string(c)) |
| 21 |
} |
| 22 |
|
| 23 |
type Age int |
| 24 |
|
| 25 |
// Age int will be ignore and string will be used |
| 26 |
func (a Age) String() string { |
| 27 |
return fmt.Sprintf("%dyrs", a) |
| 28 |
} |
| 29 |
|
| 30 |
type Person struct { |
| 31 |
Name string |
| 32 |
Age int |
| 33 |
City string |
| 34 |
} |
| 35 |
|
| 36 |
type Profile struct { |
| 37 |
Name Cleaner |
| 38 |
Age Age |
| 39 |
City string |
| 40 |
} |
| 41 |
|
| 42 |
func TestBug252(t *testing.T) { |
| 43 |
var buf bytes.Buffer |
| 44 |
type Person struct { |
| 45 |
Name string |
| 46 |
Age int |
| 47 |
City string |
| 48 |
} |
| 49 |
|
| 50 |
header := []string{"Name", "Age", "City"} |
| 51 |
alice := Person{Name: "Alice", Age: 25, City: "New York"} |
| 52 |
bob := Profile{Name: Cleaner("Bo b"), Age: Age(30), City: "Boston"} |
| 53 |
|
| 54 |
t.Run("Normal", func(t *testing.T) { |
| 55 |
buf.Reset() |
| 56 |
table := tablewriter.NewTable(&buf, tablewriter.WithDebug(true)) |
| 57 |
table.Header(header) |
| 58 |
table.Append("Alice", "25", "New York") |
| 59 |
table.Append("Bob", "30", "Boston") |
| 60 |
table.Render() |
| 61 |
|
| 62 |
expected := ` |
| 63 |
┌───────┬─────┬──────────┐ |
| 64 |
│ NAME │ AGE │ CITY │ |
| 65 |
├───────┼─────┼──────────┤ |
| 66 |
│ Alice │ 25 │ New York │ |
| 67 |
│ Bob │ 30 │ Boston │ |
| 68 |
└───────┴─────┴──────────┘ |
| 69 |
` |
| 70 |
debug := visualCheck(t, "TestBug252-Normal", buf.String(), expected) |
| 71 |
if !debug { |
| 72 |
t.Error(table.Debug()) |
| 73 |
} |
| 74 |
}) |
| 75 |
|
| 76 |
t.Run("Mixed", func(t *testing.T) { |
| 77 |
buf.Reset() |
| 78 |
table := tablewriter.NewTable(&buf, tablewriter.WithDebug(true)) |
| 79 |
table.Header(header) |
| 80 |
table.Append(alice) |
| 81 |
table.Append("Bob", "30", "Boston") |
| 82 |
table.Render() |
| 83 |
|
| 84 |
expected := ` |
| 85 |
┌───────┬─────┬──────────┐ |
| 86 |
│ NAME │ AGE │ CITY │ |
| 87 |
├───────┼─────┼──────────┤ |
| 88 |
│ Alice │ 25 │ New York │ |
| 89 |
│ Bob │ 30 │ Boston │ |
| 90 |
└───────┴─────┴──────────┘ |
| 91 |
` |
| 92 |
debug := visualCheck(t, "TestBug252-Mixed", buf.String(), expected) |
| 93 |
if !debug { |
| 94 |
// t.Error(table.Debug()) |
| 95 |
} |
| 96 |
}) |
| 97 |
|
| 98 |
t.Run("Profile", func(t *testing.T) { |
| 99 |
buf.Reset() |
| 100 |
table := tablewriter.NewTable(&buf, tablewriter.WithDebug(true)) |
| 101 |
table.Header(header) |
| 102 |
table.Append(Cleaner("A lice"), Cleaner("2 5 yrs"), "New York") |
| 103 |
table.Append(bob) |
| 104 |
table.Render() |
| 105 |
|
| 106 |
expected := ` |
| 107 |
┌───────┬───────┬──────────┐ |
| 108 |
│ NAME │ AGE │ CITY │ |
| 109 |
├───────┼───────┼──────────┤ |
| 110 |
│ Alice │ 25yrs │ New York │ |
| 111 |
│ Bob │ 30yrs │ Boston │ |
| 112 |
└───────┴───────┴──────────┘ |
| 113 |
|
| 114 |
` |
| 115 |
debug := visualCheck(t, "TestBasicTableDefault", buf.String(), expected) |
| 116 |
if !debug { |
| 117 |
// t.Error(table.Debug()) |
| 118 |
} |
| 119 |
}) |
| 120 |
|
| 121 |
type Override struct { |
| 122 |
Fish string `json:"name"` |
| 123 |
Name string `json:"-"` |
| 124 |
Age Age |
| 125 |
City string |
| 126 |
} |
| 127 |
t.Run("Override", func(t *testing.T) { |
| 128 |
buf.Reset() |
| 129 |
table := tablewriter.NewTable(&buf, tablewriter.WithDebug(true)) |
| 130 |
table.Header(header) |
| 131 |
table.Append(Cleaner("A lice"), Cleaner("2 5 yrs"), "New York") |
| 132 |
table.Append(Override{ |
| 133 |
Fish: "Bob", |
| 134 |
Name: "Skip", |
| 135 |
Age: Age(25), |
| 136 |
City: "Boston", |
| 137 |
}) |
| 138 |
table.Render() |
| 139 |
|
| 140 |
expected := ` |
| 141 |
┌───────┬───────┬──────────┐ |
| 142 |
│ NAME │ AGE │ CITY │ |
| 143 |
├───────┼───────┼──────────┤ |
| 144 |
│ Alice │ 25yrs │ New York │ |
| 145 |
│ Bob │ 25yrs │ Boston │ |
| 146 |
└───────┴───────┴──────────┘ |
| 147 |
|
| 148 |
` |
| 149 |
debug := visualCheck(t, "TestBug252-Override", buf.String(), expected) |
| 150 |
if !debug { |
| 151 |
// t.Error(table.Debug()) |
| 152 |
} |
| 153 |
}) |
| 154 |
|
| 155 |
t.Run("Override-Streaming", func(t *testing.T) { |
| 156 |
buf.Reset() |
| 157 |
table := tablewriter.NewTable(&buf, tablewriter.WithDebug(true), tablewriter.WithStreaming(tw.StreamConfig{Enable: true})) |
| 158 |
|
| 159 |
err := table.Start() |
| 160 |
if err != nil { |
| 161 |
t.Error(err) |
| 162 |
} |
| 163 |
|
| 164 |
table.Header(header) |
| 165 |
table.Append(Cleaner("A lice"), Cleaner("2 5 yrs"), "New York") |
| 166 |
table.Append(Override{ |
| 167 |
Fish: "Bob", |
| 168 |
Name: "Skip", |
| 169 |
Age: Age(25), |
| 170 |
City: "Boston", |
| 171 |
}) |
| 172 |
expected := ` |
| 173 |
┌────────┬────────┬────────┐ |
| 174 |
│ NAME │ AGE │ CITY │ |
| 175 |
├────────┼────────┼────────┤ |
| 176 |
│ Alice │ 25yrs │ New │ |
| 177 |
│ │ │ York │ |
| 178 |
│ Bob │ 25yrs │ Boston │ |
| 179 |
└────────┴────────┴────────┘ |
| 180 |
|
| 181 |
|
| 182 |
` |
| 183 |
err = table.Close() |
| 184 |
if err != nil { |
| 185 |
t.Error(err) |
| 186 |
} |
| 187 |
debug := visualCheck(t, "TestBug252-Override-Streaming", buf.String(), expected) |
| 188 |
if !debug { |
| 189 |
// t.Error(table.Debug()) |
| 190 |
} |
| 191 |
}) |
| 192 |
} |
| 193 |
|
| 194 |
func TestBug254(t *testing.T) { |
| 195 |
var buf bytes.Buffer |
| 196 |
data := [][]string{ |
| 197 |
{" LEFT", "RIGHT ", " BOTH "}, |
| 198 |
} |
| 199 |
t.Run("Normal", func(t *testing.T) { |
| 200 |
buf.Reset() |
| 201 |
table := tablewriter.NewTable(&buf, |
| 202 |
tablewriter.WithRowMaxWidth(20), |
| 203 |
tablewriter.WithTrimSpace(tw.On), |
| 204 |
tablewriter.WithAlignment(tw.Alignment{tw.AlignCenter, tw.AlignCenter, tw.AlignCenter}), |
| 205 |
) |
| 206 |
table.Bulk(data) |
| 207 |
table.Render() |
| 208 |
|
| 209 |
expected := ` |
| 210 |
┌──────┬───────┬──────┐ |
| 211 |
│ LEFT │ RIGHT │ BOTH │ |
| 212 |
└──────┴───────┴──────┘ |
| 213 |
|
| 214 |
` |
| 215 |
debug := visualCheck(t, "TestBug252-Normal", buf.String(), expected) |
| 216 |
if !debug { |
| 217 |
t.Error(table.Debug()) |
| 218 |
} |
| 219 |
}) |
| 220 |
|
| 221 |
t.Run("Mixed", func(t *testing.T) { |
| 222 |
buf.Reset() |
| 223 |
table := tablewriter.NewTable(&buf, |
| 224 |
tablewriter.WithRowMaxWidth(20), |
| 225 |
tablewriter.WithTrimSpace(tw.Off), |
| 226 |
tablewriter.WithAlignment(tw.Alignment{tw.AlignCenter, tw.AlignCenter, tw.AlignCenter}), |
| 227 |
) |
| 228 |
table.Bulk(data) |
| 229 |
table.Render() |
| 230 |
|
| 231 |
expected := ` |
| 232 |
┌────────┬─────────┬──────────┐ |
| 233 |
│ LEFT │ RIGHT │ BOTH │ |
| 234 |
└────────┴─────────┴──────────┘ |
| 235 |
` |
| 236 |
debug := visualCheck(t, "TestBug252-Mixed", buf.String(), expected) |
| 237 |
if !debug { |
| 238 |
t.Error(table.Debug()) |
| 239 |
} |
| 240 |
}) |
| 241 |
} |
| 242 |
|
| 243 |
func TestBug260(t *testing.T) { |
| 244 |
var buf bytes.Buffer |
| 245 |
|
| 246 |
tableRendition := tw.Rendition{ |
| 247 |
Borders: tw.BorderNone, |
| 248 |
Settings: tw.Settings{ |
| 249 |
Separators: tw.Separators{ |
| 250 |
ShowHeader: tw.Off, |
| 251 |
ShowFooter: tw.Off, |
| 252 |
BetweenRows: tw.Off, |
| 253 |
BetweenColumns: tw.Off, |
| 254 |
}, |
| 255 |
}, |
| 256 |
Symbols: tw.NewSymbols(tw.StyleNone), |
| 257 |
} |
| 258 |
|
| 259 |
t.Run("Normal", func(t *testing.T) { |
| 260 |
buf.Reset() |
| 261 |
tableRenderer := renderer.NewBlueprint(tableRendition) |
| 262 |
table := tablewriter.NewTable( |
| 263 |
&buf, |
| 264 |
tablewriter.WithRenderer(tableRenderer), |
| 265 |
tablewriter.WithTableMax(120), |
| 266 |
tablewriter.WithTrimSpace(tw.Off), |
| 267 |
tablewriter.WithDebug(true), |
| 268 |
tablewriter.WithPadding(tw.PaddingNone), |
| 269 |
) |
| 270 |
|
| 271 |
table.Append([]string{ |
| 272 |
"INFO:", |
| 273 |
"The original machine had a base-plate of prefabulated aluminite, surmounted by a malleable logarithmic casing in such a way that the two main spurving bearings were in a direct line with the pentametric fan.", |
| 274 |
}) |
| 275 |
|
| 276 |
table.Append("INFO:", |
| 277 |
"The original machine had a base-plate of prefabulated aluminite, surmounted by a malleable logarithmic casing in such a way that the two main spurving bearings were in a direct line with the pentametric fan.", |
| 278 |
) |
| 279 |
|
| 280 |
table.Render() |
| 281 |
|
| 282 |
expected := ` |
| 283 |
INFO:The original machine had a base-plate of prefabulated |
| 284 |
aluminite, surmounted by a malleable logarithmic casing in |
| 285 |
such a way that the two main spurving bearings were in a |
| 286 |
direct line with the pentametric fan. |
| 287 |
INFO:The original machine had a base-plate of prefabulated |
| 288 |
aluminite, surmounted by a malleable logarithmic casing in |
| 289 |
such a way that the two main spurving bearings were in a |
| 290 |
direct line with the pentametric fan. |
| 291 |
` |
| 292 |
debug := visualCheck(t, "TestBug252-Mixed", buf.String(), expected) |
| 293 |
if !debug { |
| 294 |
t.Error(table.Debug()) |
| 295 |
} |
| 296 |
}) |
| 297 |
|
| 298 |
t.Run("Mixed", func(t *testing.T) { |
| 299 |
buf.Reset() |
| 300 |
tableRenderer := renderer.NewBlueprint(tableRendition) |
| 301 |
table := tablewriter.NewTable( |
| 302 |
&buf, |
| 303 |
tablewriter.WithRenderer(tableRenderer), |
| 304 |
tablewriter.WithTableMax(120), |
| 305 |
tablewriter.WithTrimSpace(tw.Off), |
| 306 |
tablewriter.WithDebug(true), |
| 307 |
tablewriter.WithPadding(tw.PaddingNone), |
| 308 |
) |
| 309 |
|
| 310 |
table.Append([]string{ |
| 311 |
"INFO:", |
| 312 |
"The original machine had a base-plate of prefabulated aluminite, surmounted by a malleable logarithmic casing in such a way that the two main spurving bearings were in a direct line with the pentametric fan.", |
| 313 |
}) |
| 314 |
|
| 315 |
table.Append("INFO: ", |
| 316 |
"The original machine had a base-plate of prefabulated aluminite, surmounted by a malleable logarithmic casing in such a way that the two main spurving bearings were in a direct line with the pentametric fan.", |
| 317 |
) |
| 318 |
|
| 319 |
table.Render() |
| 320 |
|
| 321 |
expected := ` |
| 322 |
INFO: The original machine had a base-plate of prefabulated |
| 323 |
aluminite, surmounted by a malleable logarithmic casing in |
| 324 |
such a way that the two main spurving bearings were in a |
| 325 |
direct line with the pentametric fan. |
| 326 |
INFO: The original machine had a base-plate of prefabulated |
| 327 |
aluminite, surmounted by a malleable logarithmic casing in |
| 328 |
such a way that the two main spurving bearings were in a |
| 329 |
direct line with the pentametric fan. |
| 330 |
` |
| 331 |
debug := visualCheck(t, "TestBug252-Mixed", buf.String(), expected) |
| 332 |
if !debug { |
| 333 |
t.Error(table.Debug()) |
| 334 |
} |
| 335 |
}) |
| 336 |
} |
| 337 |
|
| 338 |
func TestBug267(t *testing.T) { |
| 339 |
var buf bytes.Buffer |
| 340 |
data := [][]string{ |
| 341 |
{"a", "b", "c"}, |
| 342 |
{"aa", "bb", "cc"}, |
| 343 |
} |
| 344 |
t.Run("WithoutMaxWith", func(t *testing.T) { |
| 345 |
buf.Reset() |
| 346 |
table := tablewriter.NewTable(&buf, |
| 347 |
tablewriter.WithTrimSpace(tw.On), |
| 348 |
tablewriter.WithConfig(tablewriter.Config{Row: tw.CellConfig{Padding: tw.CellPadding{ |
| 349 |
Global: tw.PaddingNone, |
| 350 |
}}}), |
| 351 |
) |
| 352 |
table.Bulk(data) |
| 353 |
table.Render() |
| 354 |
|
| 355 |
expected := ` |
| 356 |
┌──┬──┬──┐ |
| 357 |
│a │b │c │ |
| 358 |
│aa│bb│cc│ |
| 359 |
└──┴──┴──┘ |
| 360 |
|
| 361 |
` |
| 362 |
debug := visualCheck(t, "TestBug252-Normal", buf.String(), expected) |
| 363 |
if !debug { |
| 364 |
t.Error(table.Debug()) |
| 365 |
} |
| 366 |
}) |
| 367 |
|
| 368 |
t.Run("WithMaxWidth", func(t *testing.T) { |
| 369 |
buf.Reset() |
| 370 |
table := tablewriter.NewTable(&buf, |
| 371 |
tablewriter.WithRowMaxWidth(20), |
| 372 |
tablewriter.WithTrimSpace(tw.Off), |
| 373 |
tablewriter.WithAlignment(tw.Alignment{tw.AlignCenter, tw.AlignCenter, tw.AlignCenter}), |
| 374 |
tablewriter.WithDebug(false), |
| 375 |
tablewriter.WithConfig(tablewriter.Config{Row: tw.CellConfig{Padding: tw.CellPadding{ |
| 376 |
Global: tw.PaddingNone, |
| 377 |
}}}), |
| 378 |
) |
| 379 |
table.Bulk(data) |
| 380 |
table.Render() |
| 381 |
|
| 382 |
expected := ` |
| 383 |
┌──┬──┬──┐ |
| 384 |
│a │b │c │ |
| 385 |
│aa│bb│cc│ |
| 386 |
└──┴──┴──┘ |
| 387 |
` |
| 388 |
debug := visualCheck(t, "TestBug252-Mixed", buf.String(), expected) |
| 389 |
if !debug { |
| 390 |
t.Error(table.Debug()) |
| 391 |
} |
| 392 |
}) |
| 393 |
} |
| 394 |
|
| 395 |
func TestBug271(t *testing.T) { |
| 396 |
var buf bytes.Buffer |
| 397 |
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{ |
| 398 |
Header: tw.CellConfig{ |
| 399 |
Merging: tw.CellMerging{ |
| 400 |
Mode: tw.MergeHorizontal, |
| 401 |
}, |
| 402 |
}, |
| 403 |
Footer: tw.CellConfig{ |
| 404 |
Merging: tw.CellMerging{ |
| 405 |
Mode: tw.MergeHorizontal, |
| 406 |
}, |
| 407 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}}, |
| 408 |
}, |
| 409 |
}), |
| 410 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 411 |
Settings: tw.Settings{ |
| 412 |
Separators: tw.Separators{ |
| 413 |
BetweenRows: tw.On, |
| 414 |
}, |
| 415 |
}, |
| 416 |
})), |
| 417 |
) |
| 418 |
table.Header([]string{"Info", "Info", "Info", "Info"}) |
| 419 |
table.Append([]string{"1/1/2014", "Domain name", "Successful", "Successful"}) |
| 420 |
table.Footer([]string{"", "", "TOTAL", "$145.93"}) // Fixed from Append |
| 421 |
table.Render() |
| 422 |
|
| 423 |
expected := ` |
| 424 |
┌──────────────────────────────────────────────────┐ |
| 425 |
│ INFO │ |
| 426 |
├──────────┬─────────────┬────────────┬────────────┤ |
| 427 |
│ 1/1/2014 │ Domain name │ Successful │ Successful │ |
| 428 |
├──────────┴─────────────┴────────────┼────────────┤ |
| 429 |
│ TOTAL │ $145.93 │ |
| 430 |
└─────────────────────────────────────┴────────────┘ |
| 431 |
|
| 432 |
` |
| 433 |
check := visualCheck(t, "HorizontalMergeAlignFooter", buf.String(), expected) |
| 434 |
if !check { |
| 435 |
t.Error(table.Debug()) |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
func TestBug289(t *testing.T) { |
| 440 |
var buf bytes.Buffer |
| 441 |
|
| 442 |
data := [][]string{ |
| 443 |
{"Name", "Version", "Rev", "Tracking", "Publisher", "Notes"}, |
| 444 |
{"amberol", "0.10.3", "30", "latest/stable", "alexmurray✪", "-"}, |
| 445 |
{"android-studio", "2023.1.1", "148", "latest/stable", "snapcrafters✪", "classic"}, |
| 446 |
{"arianna", "23.08.3", "37", "latest/stable", "kde✓", "-"}, |
| 447 |
{"ascii-draw", "0.2.0", "66", "latest/stable", "nokse22", "-"}, |
| 448 |
{"bare", "1.0", "5", "latest/stable", "canonical✓", "base"}, |
| 449 |
{"beekeeper-studio", "4.1.10", "244", "latest/stable", "matthew-rathbone", "-"}, |
| 450 |
{"blender", "4.0.2", "4300", "latest/stable", "blenderfoundati✓", "classic"}, |
| 451 |
} |
| 452 |
|
| 453 |
colorCfg := renderer.ColorizedConfig{ |
| 454 |
Header: renderer.Tint{ |
| 455 |
FG: renderer.Colors{color.Bold}, // Bold headers |
| 456 |
BG: renderer.Colors{}, |
| 457 |
}, |
| 458 |
Column: renderer.Tint{ |
| 459 |
FG: renderer.Colors{color.Reset}, |
| 460 |
BG: renderer.Colors{color.Reset}, |
| 461 |
}, |
| 462 |
Footer: renderer.Tint{ |
| 463 |
FG: renderer.Colors{color.Bold}, |
| 464 |
BG: renderer.Colors{}, |
| 465 |
}, |
| 466 |
Borders: tw.BorderNone, |
| 467 |
Settings: tw.Settings{ |
| 468 |
Separators: tw.Separators{ |
| 469 |
ShowHeader: tw.Off, |
| 470 |
ShowFooter: tw.Off, |
| 471 |
BetweenRows: tw.Off, |
| 472 |
BetweenColumns: tw.Off, |
| 473 |
}, |
| 474 |
Lines: tw.Lines{ |
| 475 |
ShowTop: tw.Off, |
| 476 |
ShowBottom: tw.Off, |
| 477 |
ShowHeaderLine: tw.Off, |
| 478 |
ShowFooterLine: tw.Off, |
| 479 |
}, |
| 480 |
}, |
| 481 |
} |
| 482 |
|
| 483 |
options := []tablewriter.Option{ |
| 484 |
tablewriter.WithRenderer(renderer.NewColorized(colorCfg)), |
| 485 |
tablewriter.WithConfig(tablewriter.Config{ |
| 486 |
MaxWidth: 80, |
| 487 |
Header: tw.CellConfig{ |
| 488 |
Alignment: tw.CellAlignment{ |
| 489 |
Global: tw.AlignLeft, |
| 490 |
PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.Skip, tw.Skip}, |
| 491 |
}, |
| 492 |
Formatting: tw.CellFormatting{ |
| 493 |
AutoWrap: tw.WrapNone, |
| 494 |
AutoFormat: tw.On, |
| 495 |
}, |
| 496 |
Merging: tw.CellMerging{ |
| 497 |
Mode: tw.MergeNone, |
| 498 |
}, |
| 499 |
Padding: tw.CellPadding{ |
| 500 |
Global: tw.Padding{ |
| 501 |
Left: tw.Empty, |
| 502 |
Right: " ", |
| 503 |
Top: tw.Empty, |
| 504 |
Bottom: tw.Empty, |
| 505 |
Overwrite: true, |
| 506 |
}, |
| 507 |
}, |
| 508 |
}, |
| 509 |
Row: tw.CellConfig{ |
| 510 |
Formatting: tw.CellFormatting{AutoWrap: tw.WrapNormal}, // Wrap long content |
| 511 |
Alignment: tw.CellAlignment{ |
| 512 |
Global: tw.AlignLeft, |
| 513 |
PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.Skip, tw.Skip}, |
| 514 |
}, |
| 515 |
Padding: tw.CellPadding{ |
| 516 |
Global: tw.Padding{ |
| 517 |
Left: tw.Empty, |
| 518 |
Right: " ", |
| 519 |
Top: tw.Empty, |
| 520 |
Bottom: tw.Empty, |
| 521 |
Overwrite: true, |
| 522 |
}, |
| 523 |
}, |
| 524 |
}, |
| 525 |
Footer: tw.CellConfig{ |
| 526 |
Alignment: tw.CellAlignment{Global: tw.AlignRight}, |
| 527 |
}, |
| 528 |
}), |
| 529 |
} |
| 530 |
|
| 531 |
table := tablewriter.NewTable(&buf, options...) |
| 532 |
table.Header(data[0]) |
| 533 |
table.Bulk(data[1:]) |
| 534 |
table.Render() |
| 535 |
|
| 536 |
expected := ` |
| 537 |
NAME VERSION REV TRACKING PUBLISHER NOTES |
| 538 |
amberol 0.10.3 30 latest/stable alexmurray✪ - |
| 539 |
android-studio 2023.1.1 148 latest/stable snapcrafters✪ classic |
| 540 |
arianna 23.08.3 37 latest/stable kde✓ - |
| 541 |
ascii-draw 0.2.0 66 latest/stable nokse22 - |
| 542 |
bare 1.0 5 latest/stable canonical✓ base |
| 543 |
beekeeper-studio 4.1.10 244 latest/stable matthew-rathbone - |
| 544 |
blender 4.0.2 4300 latest/stable blenderfoundati✓ classic |
| 545 |
` |
| 546 |
check := visualCheck(t, "HorizontalMergeAlignFooter", buf.String(), expected) |
| 547 |
if !check { |
| 548 |
t.Error(table.Debug()) |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
// TestBugRenditionDebugLeak verifies that Blueprint.Rendition() does not emit debug |
| 553 |
// output when WithDebug(false) is applied via Options(). Previously, when a table was |
| 554 |
// created with WithDebug(true) and then reconfigured via Options() with WithDebug(false) |
| 555 |
// and WithRendition(), the renderer's logger was still enabled during option application, |
| 556 |
// causing unwanted debug messages from Blueprint.Rendition(). |
| 557 |
func TestBugRenditionDebugLeak(t *testing.T) { |
| 558 |
t.Run("OptionsReconfigure", func(t *testing.T) { |
| 559 |
var buf bytes.Buffer |
| 560 |
// Create a table with debug enabled so the renderer gets an enabled logger |
| 561 |
table := tablewriter.NewTable(&buf, tablewriter.WithDebug(true)) |
| 562 |
|
| 563 |
// Reconfigure with debug disabled and a rendition change |
| 564 |
table.Options( |
| 565 |
tablewriter.WithDebug(false), |
| 566 |
tablewriter.WithRendition(tw.Rendition{ |
| 567 |
Borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.Off, Bottom: tw.Off}, |
| 568 |
}), |
| 569 |
) |
| 570 |
|
| 571 |
table.Header([]string{"Name", "Age"}) |
| 572 |
table.Append("Alice", "25") |
| 573 |
table.Render() |
| 574 |
|
| 575 |
debugBuf := table.Debug() |
| 576 |
if strings.Contains(debugBuf.String(), "Blueprint.Rendition updated") { |
| 577 |
t.Errorf("debug output leaked from Blueprint.Rendition despite WithDebug(false):\n%s", debugBuf.String()) |
| 578 |
} |
| 579 |
}) |
| 580 |
|
| 581 |
t.Run("OptionsReconfigureOcean", func(t *testing.T) { |
| 582 |
var buf bytes.Buffer |
| 583 |
table := tablewriter.NewTable(&buf, |
| 584 |
tablewriter.WithDebug(true), |
| 585 |
tablewriter.WithRenderer(renderer.NewOcean()), |
| 586 |
) |
| 587 |
|
| 588 |
table.Options( |
| 589 |
tablewriter.WithDebug(false), |
| 590 |
tablewriter.WithRendition(tw.Rendition{ |
| 591 |
Borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.Off, Bottom: tw.Off}, |
| 592 |
}), |
| 593 |
) |
| 594 |
|
| 595 |
table.Header([]string{"Name", "Age"}) |
| 596 |
table.Append("Alice", "25") |
| 597 |
table.Render() |
| 598 |
|
| 599 |
debugBuf := table.Debug() |
| 600 |
if strings.Contains(debugBuf.String(), "Rendition updated") { |
| 601 |
t.Errorf("debug output leaked from Ocean.Rendition despite WithDebug(false):\n%s", debugBuf.String()) |
| 602 |
} |
| 603 |
}) |
| 604 |
} |
| 605 |
|
| 606 |
// TestBugStreamWidthReduction tests that streaming mode correctly reduces column widths |
| 607 |
// to respect the global width constraint, even when multiple columns are at minimum width |
| 608 |
// and the round-robin distribution needs to skip them to find reducible columns. |
| 609 |
func TestBugStreamWidthReduction(t *testing.T) { |
| 610 |
for _, tt := range []struct { |
| 611 |
name string |
| 612 |
global int |
| 613 |
headers []string |
| 614 |
row []string |
| 615 |
}{ |
| 616 |
{ |
| 617 |
name: "NarrowWith5Cols", |
| 618 |
global: 12, |
| 619 |
headers: []string{"AAAAAA", "BB", "CC", "DD", "EE"}, |
| 620 |
row: []string{"123456", "12", "12", "12", "12"}, |
| 621 |
}, |
| 622 |
{ |
| 623 |
name: "VeryNarrowWith3Cols", |
| 624 |
global: 10, |
| 625 |
headers: []string{"Name", "Value", "X"}, |
| 626 |
row: []string{"LongNameHere", "SomeValue!", "Y"}, |
| 627 |
}, |
| 628 |
{ |
| 629 |
name: "TightWith4Cols", |
| 630 |
global: 10, |
| 631 |
headers: []string{"AA", "BB", "CC", "DD"}, |
| 632 |
row: []string{"11", "22", "33", "44"}, |
| 633 |
}, |
| 634 |
} { |
| 635 |
t.Run(tt.name, func(t *testing.T) { |
| 636 |
var buf bytes.Buffer |
| 637 |
st := createStreamTable(t, &buf, |
| 638 |
tablewriter.WithConfig(tablewriter.Config{ |
| 639 |
Row: tw.CellConfig{ |
| 640 |
Formatting: tw.CellFormatting{AutoWrap: tw.WrapTruncate}, |
| 641 |
}, |
| 642 |
Stream: tw.StreamConfig{Enable: true}, |
| 643 |
Widths: tw.CellWidth{Global: tt.global}, |
| 644 |
}), |
| 645 |
) |
| 646 |
if err := st.Start(); err != nil { |
| 647 |
t.Fatalf("Start failed: %v", err) |
| 648 |
} |
| 649 |
st.Header(tt.headers) |
| 650 |
st.Append(tt.row) |
| 651 |
if err := st.Close(); err != nil { |
| 652 |
t.Fatalf("Close failed: %v", err) |
| 653 |
} |
| 654 |
|
| 655 |
output := buf.String() |
| 656 |
lines := strings.Split(strings.TrimSpace(output), "\n") |
| 657 |
for i, line := range lines { |
| 658 |
// Subtract 2 for left and right border characters |
| 659 |
lineWidth := twwidth.Width(line) - 2 |
| 660 |
if lineWidth > tt.global { |
| 661 |
t.Errorf("line %d exceeds global width %d (got %d): %s", i, tt.global, lineWidth, line) |
| 662 |
} |
| 663 |
} |
| 664 |
}) |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
// TestBugMergedHeaderOverDistribution tests that batch mode correctly handles |
| 669 |
// over-distribution correction when merging headers, terminating early when |
| 670 |
// no eligible columns remain for reduction. |
| 671 |
func TestBugMergedHeaderOverDistribution(t *testing.T) { |
| 672 |
var buf bytes.Buffer |
| 673 |
table := tablewriter.NewTable(&buf, |
| 674 |
tablewriter.WithConfig(tablewriter.Config{ |
| 675 |
Header: tw.CellConfig{ |
| 676 |
Merging: tw.CellMerging{Mode: tw.MergeHorizontal}, |
| 677 |
}, |
| 678 |
}), |
| 679 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 680 |
Settings: tw.Settings{ |
| 681 |
Separators: tw.Separators{BetweenRows: tw.Off}, |
| 682 |
}, |
| 683 |
})), |
| 684 |
) |
| 685 |
// Short merged header spanning many columns with narrow content |
| 686 |
table.Header([]string{"Merged", "Merged", "Merged", "Merged"}) |
| 687 |
table.Append([]string{"A", "B", "C", "D"}) |
| 688 |
table.Append([]string{"1", "2", "3", "4"}) |
| 689 |
table.Render() |
| 690 |
|
| 691 |
output := buf.String() |
| 692 |
lines := strings.Split(strings.TrimSpace(output), "\n") |
| 693 |
|
| 694 |
// All lines should have the same width (consistent table rendering) |
| 695 |
if len(lines) == 0 { |
| 696 |
t.Fatal("no output lines") |
| 697 |
} |
| 698 |
firstWidth := twwidth.Width(lines[0]) |
| 699 |
for i, line := range lines { |
| 700 |
w := twwidth.Width(line) |
| 701 |
if w != firstWidth { |
| 702 |
t.Errorf("line %d has width %d, expected %d: %s", i, w, firstWidth, line) |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
expected := ` |
| 707 |
┌───────────────────────────────────┐ |
| 708 |
│ MERGED │ |
| 709 |
├────────┬────────┬────────┬────────┤ |
| 710 |
│ A │ B │ C │ D │ |
| 711 |
│ 1 │ 2 │ 3 │ 4 │ |
| 712 |
└────────┴────────┴────────┴────────┘ |
| 713 |
` |
| 714 |
if !visualCheck(t, "MergedHeaderOverDistribution", output, expected) { |
| 715 |
t.Error(table.Debug()) |
| 716 |
} |
| 717 |
} |