| 1 |
package tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/olekukonko/tablewriter" |
| 9 |
"github.com/olekukonko/tablewriter/renderer" |
| 10 |
"github.com/olekukonko/tablewriter/tw" |
| 11 |
) |
| 12 |
|
| 13 |
func TestBasicTableDefault(t *testing.T) { |
| 14 |
var buf bytes.Buffer |
| 15 |
|
| 16 |
table := tablewriter.NewTable(&buf) |
| 17 |
table.Header([]string{"Name", "Age", "City"}) |
| 18 |
table.Append([]string{"Alice", "25", "New York"}) |
| 19 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 20 |
table.Render() |
| 21 |
|
| 22 |
expected := ` |
| 23 |
┌───────┬─────┬──────────┐ |
| 24 |
│ NAME │ AGE │ CITY │ |
| 25 |
├───────┼─────┼──────────┤ |
| 26 |
│ Alice │ 25 │ New York │ |
| 27 |
│ Bob │ 30 │ Boston │ |
| 28 |
└───────┴─────┴──────────┘ |
| 29 |
` |
| 30 |
debug := visualCheck(t, "TestBasicTableDefault", buf.String(), expected) |
| 31 |
if !debug { |
| 32 |
t.Error(table.Debug()) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func TestBasicTableDefaultBorder(t *testing.T) { |
| 37 |
var buf bytes.Buffer |
| 38 |
|
| 39 |
t.Run("all-off", func(t *testing.T) { |
| 40 |
buf.Reset() |
| 41 |
table := tablewriter.NewTable(&buf, |
| 42 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 43 |
Borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.Off, Bottom: tw.Off}, |
| 44 |
})), |
| 45 |
) |
| 46 |
|
| 47 |
table.Header([]string{"Name", "Age", "City"}) |
| 48 |
table.Append([]string{"Alice", "25", "New York"}) |
| 49 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 50 |
table.Render() |
| 51 |
|
| 52 |
expected := ` |
| 53 |
NAME │ AGE │ CITY |
| 54 |
───────┼─────┼────────── |
| 55 |
Alice │ 25 │ New York |
| 56 |
Bob │ 30 │ Boston |
| 57 |
` |
| 58 |
|
| 59 |
visualCheck(t, "TestBasicTableDefaultBorder-top-off", buf.String(), expected) |
| 60 |
}) |
| 61 |
|
| 62 |
t.Run("top-on", func(t *testing.T) { |
| 63 |
buf.Reset() |
| 64 |
table := tablewriter.NewTable(&buf, |
| 65 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 66 |
Borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.On, Bottom: tw.Off}, |
| 67 |
})), |
| 68 |
) |
| 69 |
|
| 70 |
table.Header([]string{"Name", "Age", "City"}) |
| 71 |
table.Append([]string{"Alice", "25", "New York"}) |
| 72 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 73 |
table.Render() |
| 74 |
|
| 75 |
expected := ` |
| 76 |
───────┬─────┬────────── |
| 77 |
NAME │ AGE │ CITY |
| 78 |
───────┼─────┼────────── |
| 79 |
Alice │ 25 │ New York |
| 80 |
Bob │ 30 │ Boston |
| 81 |
|
| 82 |
` |
| 83 |
|
| 84 |
visualCheck(t, "TestBasicTableDefaultBorder-top-on", buf.String(), expected) |
| 85 |
}) |
| 86 |
|
| 87 |
t.Run("mix", func(t *testing.T) { |
| 88 |
buf.Reset() |
| 89 |
table := tablewriter.NewTable(&buf, |
| 90 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 91 |
Borders: tw.Border{Left: tw.Off, Right: tw.On, Top: tw.On, Bottom: tw.On}, |
| 92 |
})), |
| 93 |
) |
| 94 |
|
| 95 |
table.Header([]string{"Name", "Age", "City"}) |
| 96 |
table.Append([]string{"Alice", "25", "New York"}) |
| 97 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 98 |
table.Render() |
| 99 |
|
| 100 |
expected := ` |
| 101 |
───────┬─────┬──────────┐ |
| 102 |
NAME │ AGE │ CITY │ |
| 103 |
───────┼─────┼──────────┤ |
| 104 |
Alice │ 25 │ New York │ |
| 105 |
Bob │ 30 │ Boston │ |
| 106 |
───────┴─────┴──────────┘ |
| 107 |
|
| 108 |
` |
| 109 |
visualCheck(t, "TestBasicTableDefaultBorder-mix", buf.String(), expected) |
| 110 |
}) |
| 111 |
} |
| 112 |
|
| 113 |
func TestUnicodeWithoutHeader(t *testing.T) { |
| 114 |
data := [][]string{ |
| 115 |
{"Regular", "regular line", "1"}, |
| 116 |
{"Thick", "particularly thick line", "2"}, |
| 117 |
{"Double", "double line", "3"}, |
| 118 |
} |
| 119 |
|
| 120 |
var buf bytes.Buffer |
| 121 |
table := tablewriter.NewTable(&buf, |
| 122 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 123 |
Borders: tw.Border{Left: tw.On, Right: tw.On, Top: tw.Off, Bottom: tw.Off}, |
| 124 |
})), |
| 125 |
) |
| 126 |
table.Header([]string{"Name", "Age", "City"}) |
| 127 |
table.Bulk(data) |
| 128 |
|
| 129 |
table.Render() |
| 130 |
|
| 131 |
expected := ` |
| 132 |
│ NAME │ AGE │ CITY │ |
| 133 |
├─────────┼─────────────────────────┼──────┤ |
| 134 |
│ Regular │ regular line │ 1 │ |
| 135 |
│ Thick │ particularly thick line │ 2 │ |
| 136 |
│ Double │ double line │ 3 │ |
| 137 |
` |
| 138 |
visualCheck(t, "UnicodeWithoutHeader", buf.String(), expected) |
| 139 |
} |
| 140 |
|
| 141 |
func TestBasicTableASCII(t *testing.T) { |
| 142 |
var buf bytes.Buffer |
| 143 |
table := tablewriter.NewTable(&buf, |
| 144 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 145 |
Symbols: tw.NewSymbols(tw.StyleASCII), |
| 146 |
})), |
| 147 |
) |
| 148 |
table.Header([]string{"Name", "Age", "City"}) |
| 149 |
table.Append([]string{"Alice", "25", "New York"}) |
| 150 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 151 |
table.Render() |
| 152 |
|
| 153 |
expected := ` |
| 154 |
+-------+-----+----------+ |
| 155 |
| NAME | AGE | CITY | |
| 156 |
+-------+-----+----------+ |
| 157 |
| Alice | 25 | New York | |
| 158 |
| Bob | 30 | Boston | |
| 159 |
+-------+-----+----------+ |
| 160 |
` |
| 161 |
visualCheck(t, "BasicTableASCII", buf.String(), expected) |
| 162 |
} |
| 163 |
|
| 164 |
func TestBasicTableUnicodeRounded(t *testing.T) { |
| 165 |
var buf bytes.Buffer |
| 166 |
table := tablewriter.NewTable(&buf, |
| 167 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 168 |
Symbols: tw.NewSymbols(tw.StyleRounded), |
| 169 |
})), |
| 170 |
) |
| 171 |
table.Header([]string{"Name", "Age", "City"}) |
| 172 |
table.Append([]string{"Alice", "25", "New York"}) |
| 173 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 174 |
table.Render() |
| 175 |
|
| 176 |
expected := ` |
| 177 |
╭───────┬─────┬──────────╮ |
| 178 |
│ NAME │ AGE │ CITY │ |
| 179 |
├───────┼─────┼──────────┤ |
| 180 |
│ Alice │ 25 │ New York │ |
| 181 |
│ Bob │ 30 │ Boston │ |
| 182 |
╰───────┴─────┴──────────╯ |
| 183 |
` |
| 184 |
visualCheck(t, "BasicTableUnicodeRounded", buf.String(), expected) |
| 185 |
} |
| 186 |
|
| 187 |
func TestBasicTableUnicodeDouble(t *testing.T) { |
| 188 |
var buf bytes.Buffer |
| 189 |
table := tablewriter.NewTable(&buf, |
| 190 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 191 |
Symbols: tw.NewSymbols(tw.StyleDouble), |
| 192 |
})), |
| 193 |
) |
| 194 |
table.Header([]string{"Name", "Age", "City"}) |
| 195 |
table.Append([]string{"Alice", "25", "New York"}) |
| 196 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 197 |
table.Render() |
| 198 |
|
| 199 |
expected := ` |
| 200 |
╔═══════╦═════╦══════════╗ |
| 201 |
║ NAME ║ AGE ║ CITY ║ |
| 202 |
╠═══════╬═════╬══════════╣ |
| 203 |
║ Alice ║ 25 ║ New York ║ |
| 204 |
║ Bob ║ 30 ║ Boston ║ |
| 205 |
╚═══════╩═════╩══════════╝ |
| 206 |
` |
| 207 |
visualCheck(t, "TableUnicodeDouble", buf.String(), expected) |
| 208 |
} |
| 209 |
|
| 210 |
func TestSeparator(t *testing.T) { |
| 211 |
data := [][]string{ |
| 212 |
{"Regular", "regular line", "1"}, |
| 213 |
{"Thick", "particularly thick line", "2"}, |
| 214 |
{"Double", "double line", "3"}, |
| 215 |
} |
| 216 |
|
| 217 |
t.Run("horizontal - enabled", func(t *testing.T) { |
| 218 |
var buf bytes.Buffer |
| 219 |
table := tablewriter.NewTable(&buf, |
| 220 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 221 |
Settings: tw.Settings{ |
| 222 |
Separators: tw.Separators{ |
| 223 |
BetweenColumns: tw.On, |
| 224 |
BetweenRows: tw.On, |
| 225 |
}, |
| 226 |
Lines: tw.Lines{ |
| 227 |
ShowHeaderLine: tw.On, |
| 228 |
}, |
| 229 |
}, |
| 230 |
})), |
| 231 |
) |
| 232 |
table.Header([]string{"Name", "Age", "City"}) |
| 233 |
table.Bulk(data) |
| 234 |
table.Render() |
| 235 |
|
| 236 |
expected := ` |
| 237 |
┌─────────┬─────────────────────────┬──────┐ |
| 238 |
│ NAME │ AGE │ CITY │ |
| 239 |
├─────────┼─────────────────────────┼──────┤ |
| 240 |
│ Regular │ regular line │ 1 │ |
| 241 |
├─────────┼─────────────────────────┼──────┤ |
| 242 |
│ Thick │ particularly thick line │ 2 │ |
| 243 |
├─────────┼─────────────────────────┼──────┤ |
| 244 |
│ Double │ double line │ 3 │ |
| 245 |
└─────────┴─────────────────────────┴──────┘ |
| 246 |
` |
| 247 |
visualCheck(t, "HorizontalEnabled", buf.String(), expected) |
| 248 |
}) |
| 249 |
|
| 250 |
t.Run("horizontal - disabled", func(t *testing.T) { |
| 251 |
var buf bytes.Buffer |
| 252 |
table := tablewriter.NewTable(&buf, |
| 253 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 254 |
Settings: tw.Settings{ |
| 255 |
Separators: tw.Separators{ |
| 256 |
BetweenColumns: tw.On, |
| 257 |
BetweenRows: tw.Off, |
| 258 |
}, |
| 259 |
Lines: tw.Lines{ |
| 260 |
ShowHeaderLine: tw.On, |
| 261 |
}, |
| 262 |
}, |
| 263 |
})), |
| 264 |
) |
| 265 |
table.Header([]string{"Name", "Age", "City"}) |
| 266 |
table.Bulk(data) |
| 267 |
table.Render() |
| 268 |
|
| 269 |
expected := ` |
| 270 |
┌─────────┬─────────────────────────┬──────┐ |
| 271 |
│ NAME │ AGE │ CITY │ |
| 272 |
├─────────┼─────────────────────────┼──────┤ |
| 273 |
│ Regular │ regular line │ 1 │ |
| 274 |
│ Thick │ particularly thick line │ 2 │ |
| 275 |
│ Double │ double line │ 3 │ |
| 276 |
└─────────┴─────────────────────────┴──────┘ |
| 277 |
` |
| 278 |
visualCheck(t, "Separator", buf.String(), expected) |
| 279 |
}) |
| 280 |
|
| 281 |
t.Run("vertical - enabled", func(t *testing.T) { |
| 282 |
var buf bytes.Buffer |
| 283 |
table := tablewriter.NewTable(&buf, |
| 284 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 285 |
Settings: tw.Settings{ |
| 286 |
Separators: tw.Separators{ |
| 287 |
BetweenColumns: tw.On, |
| 288 |
BetweenRows: tw.Off, |
| 289 |
}, |
| 290 |
Lines: tw.Lines{ |
| 291 |
ShowHeaderLine: tw.On, |
| 292 |
}, |
| 293 |
}, |
| 294 |
})), |
| 295 |
) |
| 296 |
table.Header([]string{"Name", "Age", "City"}) |
| 297 |
table.Bulk(data) |
| 298 |
table.Render() |
| 299 |
|
| 300 |
expected := ` |
| 301 |
┌─────────┬─────────────────────────┬──────┐ |
| 302 |
│ NAME │ AGE │ CITY │ |
| 303 |
├─────────┼─────────────────────────┼──────┤ |
| 304 |
│ Regular │ regular line │ 1 │ |
| 305 |
│ Thick │ particularly thick line │ 2 │ |
| 306 |
│ Double │ double line │ 3 │ |
| 307 |
└─────────┴─────────────────────────┴──────┘ |
| 308 |
` |
| 309 |
visualCheck(t, "VerticalEnabled", buf.String(), expected) |
| 310 |
}) |
| 311 |
|
| 312 |
t.Run("vertical - disabled", func(t *testing.T) { |
| 313 |
var buf bytes.Buffer |
| 314 |
table := tablewriter.NewTable(&buf, |
| 315 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 316 |
Settings: tw.Settings{ |
| 317 |
Separators: tw.Separators{ |
| 318 |
BetweenColumns: tw.Off, |
| 319 |
BetweenRows: tw.Off, |
| 320 |
}, |
| 321 |
Lines: tw.Lines{ |
| 322 |
ShowHeaderLine: tw.On, |
| 323 |
}, |
| 324 |
}, |
| 325 |
})), |
| 326 |
) |
| 327 |
table.Header([]string{"Name", "Age", "City"}) |
| 328 |
table.Bulk(data) |
| 329 |
table.Render() |
| 330 |
|
| 331 |
expected := ` |
| 332 |
┌────────────────────────────────────────┐ |
| 333 |
│ NAME AGE CITY │ |
| 334 |
├────────────────────────────────────────┤ |
| 335 |
│ Regular regular line 1 │ |
| 336 |
│ Thick particularly thick line 2 │ |
| 337 |
│ Double double line 3 │ |
| 338 |
└────────────────────────────────────────┘ |
| 339 |
` |
| 340 |
visualCheck(t, "VerticalDisabled", buf.String(), expected) |
| 341 |
}) |
| 342 |
} |
| 343 |
|
| 344 |
func TestLongHeaders(t *testing.T) { |
| 345 |
var buf bytes.Buffer |
| 346 |
|
| 347 |
t.Run("long-headers", func(t *testing.T) { |
| 348 |
c := tablewriter.Config{ |
| 349 |
Header: tw.CellConfig{ |
| 350 |
Formatting: tw.CellFormatting{ |
| 351 |
AutoWrap: tw.WrapTruncate, |
| 352 |
}, |
| 353 |
ColMaxWidths: tw.CellWidth{Global: 30}, |
| 354 |
}, |
| 355 |
} |
| 356 |
buf.Reset() |
| 357 |
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(c), tablewriter.WithDebug(true)) |
| 358 |
table.Header([]string{"Name", "Age", "This is a very long header, let see if this will be properly wrapped"}) |
| 359 |
table.Append([]string{"Alice", "25", "New York"}) |
| 360 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 361 |
table.Render() |
| 362 |
|
| 363 |
expected := ` |
| 364 |
┌───────┬─────┬─────────────────────────────┐ |
| 365 |
│ NAME │ AGE │ THIS IS A VERY LONG HEADER… │ |
| 366 |
├───────┼─────┼─────────────────────────────┤ |
| 367 |
│ Alice │ 25 │ New York │ |
| 368 |
│ Bob │ 30 │ Boston │ |
| 369 |
└───────┴─────┴─────────────────────────────┘ |
| 370 |
` |
| 371 |
if !visualCheck(t, "TestLongHeaders", buf.String(), expected) { |
| 372 |
t.Log(table.Debug()) |
| 373 |
} |
| 374 |
}) |
| 375 |
|
| 376 |
t.Run("long-headers-no-truncate", func(t *testing.T) { |
| 377 |
buf.Reset() |
| 378 |
|
| 379 |
c := tablewriter.Config{ |
| 380 |
Header: tw.CellConfig{ |
| 381 |
Formatting: tw.CellFormatting{ |
| 382 |
AutoWrap: tw.WrapNormal, |
| 383 |
}, |
| 384 |
ColMaxWidths: tw.CellWidth{Global: 30}, |
| 385 |
}, |
| 386 |
} |
| 387 |
|
| 388 |
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(c)) |
| 389 |
table.Header([]string{"Name", "Age", "This is a very long header, let see if this will be properly wrapped"}) |
| 390 |
table.Append([]string{"Alice", "25", "New York"}) |
| 391 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 392 |
table.Render() |
| 393 |
expected := ` |
| 394 |
┌───────┬─────┬────────────────────────────┐ |
| 395 |
│ NAME │ AGE │ THIS IS A VERY LONG HEADER │ |
| 396 |
│ │ │ , LET SEE IF THIS WILL BE │ |
| 397 |
│ │ │ PROPERLY WRAPPED │ |
| 398 |
├───────┼─────┼────────────────────────────┤ |
| 399 |
│ Alice │ 25 │ New York │ |
| 400 |
│ Bob │ 30 │ Boston │ |
| 401 |
└───────┴─────┴────────────────────────────┘ |
| 402 |
` |
| 403 |
if !visualCheck(t, "LongHeaders", buf.String(), expected) { |
| 404 |
t.Log(table.Debug()) |
| 405 |
} |
| 406 |
}) |
| 407 |
} |
| 408 |
|
| 409 |
func TestLongValues(t *testing.T) { |
| 410 |
data := [][]string{ |
| 411 |
{"1", "Learn East has computers with adapted keyboards with enlarged print etc", "Some Data", "Another Data"}, |
| 412 |
{"2", "Instead of lining up the letters all", "the way across, he splits the keyboard in two", "Like most ergonomic keyboards"}, |
| 413 |
{"3", "Nice", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's \n" + |
| 414 |
"standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen bok", "Like most ergonomic keyboards"}, |
| 415 |
} |
| 416 |
|
| 417 |
c := tablewriter.Config{ |
| 418 |
Header: tw.CellConfig{ |
| 419 |
Formatting: tw.CellFormatting{ |
| 420 |
AutoFormat: tw.On, |
| 421 |
}, |
| 422 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 423 |
ColMaxWidths: tw.CellWidth{Global: 30}, |
| 424 |
}, |
| 425 |
Row: tw.CellConfig{ |
| 426 |
Formatting: tw.CellFormatting{ |
| 427 |
AutoWrap: tw.WrapNormal, |
| 428 |
}, |
| 429 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 430 |
ColMaxWidths: tw.CellWidth{Global: 30}, |
| 431 |
}, |
| 432 |
Footer: tw.CellConfig{ |
| 433 |
Alignment: tw.CellAlignment{ |
| 434 |
Global: tw.AlignRight, |
| 435 |
PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.Skip, tw.AlignLeft}, |
| 436 |
}, |
| 437 |
}, |
| 438 |
} |
| 439 |
|
| 440 |
var buf bytes.Buffer |
| 441 |
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(c)) |
| 442 |
table.Header([]string{"No", "Comments", "Another", ""}) |
| 443 |
table.Footer([]string{"", "", "---------->", "<---------"}) |
| 444 |
table.Bulk(data) |
| 445 |
|
| 446 |
table.Render() |
| 447 |
|
| 448 |
expected := ` |
| 449 |
|
| 450 |
┌────┬─────────────────────────────┬──────────────────────────────┬─────────────────────┐ |
| 451 |
│ NO │ COMMENTS │ ANOTHER │ │ |
| 452 |
├────┼─────────────────────────────┼──────────────────────────────┼─────────────────────┤ |
| 453 |
│ 1 │ Learn East has computers │ Some Data │ Another Data │ |
| 454 |
│ │ with adapted keyboards with │ │ │ |
| 455 |
│ │ enlarged print etc │ │ │ |
| 456 |
│ 2 │ Instead of lining up the │ the way across, he splits │ Like most ergonomic │ |
| 457 |
│ │ letters all │ the keyboard in two │ keyboards │ |
| 458 |
│ 3 │ Nice │ Lorem Ipsum is simply │ Like most ergonomic │ |
| 459 |
│ │ │ dummy text of the printing │ keyboards │ |
| 460 |
│ │ │ and typesetting industry. │ │ |
| 461 |
│ │ │ Lorem Ipsum has been the │ │ |
| 462 |
│ │ │ industry's │ │ |
| 463 |
│ │ │ standard dummy text ever │ │ |
| 464 |
│ │ │ since the 1500s, when an │ │ |
| 465 |
│ │ │ unknown printer took a │ │ |
| 466 |
│ │ │ galley of type and scrambled │ │ |
| 467 |
│ │ │ it to make a type specimen │ │ |
| 468 |
│ │ │ bok │ │ |
| 469 |
├────┼─────────────────────────────┼──────────────────────────────┼─────────────────────┤ |
| 470 |
│ │ │ ----------> │ <--------- │ |
| 471 |
└────┴─────────────────────────────┴──────────────────────────────┴─────────────────────┘ |
| 472 |
|
| 473 |
` |
| 474 |
if !visualCheck(t, "LongValues", buf.String(), expected) { |
| 475 |
t.Error(table.Debug()) |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
func TestWrapping(t *testing.T) { |
| 480 |
data := [][]string{ |
| 481 |
{"1", "https://github.com/olekukonko/ruta", "routing websocket"}, |
| 482 |
{"2", "https://github.com/olekukonko/error", "better error"}, |
| 483 |
{"3", "https://github.com/olekukonko/tablewriter", "terminal\ntable"}, |
| 484 |
} |
| 485 |
|
| 486 |
c := tablewriter.Config{ |
| 487 |
Header: tw.CellConfig{ |
| 488 |
Formatting: tw.CellFormatting{ |
| 489 |
AutoFormat: tw.On, |
| 490 |
}, |
| 491 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 492 |
}, |
| 493 |
Row: tw.CellConfig{ |
| 494 |
Formatting: tw.CellFormatting{ |
| 495 |
AutoWrap: tw.WrapBreak, |
| 496 |
}, |
| 497 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 498 |
ColMaxWidths: tw.CellWidth{Global: 33}, |
| 499 |
}, |
| 500 |
Footer: tw.CellConfig{ |
| 501 |
Alignment: tw.CellAlignment{Global: tw.AlignRight}, |
| 502 |
ColMaxWidths: tw.CellWidth{Global: 30}, |
| 503 |
}, |
| 504 |
} |
| 505 |
|
| 506 |
var buf bytes.Buffer |
| 507 |
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(c)) |
| 508 |
table.Header([]string{"No", "Package", "Comments"}) |
| 509 |
table.Bulk(data) |
| 510 |
table.Render() |
| 511 |
|
| 512 |
expected := ` |
| 513 |
┌────┬─────────────────────────────────┬───────────────────┐ |
| 514 |
│ NO │ PACKAGE │ COMMENTS │ |
| 515 |
├────┼─────────────────────────────────┼───────────────────┤ |
| 516 |
│ 1 │ https://github.com/olekukonko/↩ │ routing websocket │ |
| 517 |
│ │ ruta │ │ |
| 518 |
│ 2 │ https://github.com/olekukonko/↩ │ better error │ |
| 519 |
│ │ error │ │ |
| 520 |
│ 3 │ https://github.com/olekukonko/↩ │ terminal │ |
| 521 |
│ │ tablewriter │ table │ |
| 522 |
└────┴─────────────────────────────────┴───────────────────┘ |
| 523 |
` |
| 524 |
visualCheck(t, "Wrapping", buf.String(), expected) |
| 525 |
} |
| 526 |
|
| 527 |
func TestTableWithCustomPadding(t *testing.T) { |
| 528 |
data := [][]string{ |
| 529 |
{"Regular", "regular line", "1"}, |
| 530 |
{"Thick", "particularly thick line", "2"}, |
| 531 |
{"Double", "double line", "3"}, |
| 532 |
} |
| 533 |
|
| 534 |
c := tablewriter.Config{ |
| 535 |
Header: tw.CellConfig{ |
| 536 |
Formatting: tw.CellFormatting{ |
| 537 |
AutoFormat: tw.On, |
| 538 |
}, |
| 539 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 540 |
Padding: tw.CellPadding{ |
| 541 |
Global: tw.Padding{Left: " ", Right: " ", Top: "^", Bottom: "^"}, |
| 542 |
}, |
| 543 |
}, |
| 544 |
Row: tw.CellConfig{ |
| 545 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 546 |
Padding: tw.CellPadding{ |
| 547 |
Global: tw.Padding{Left: "L", Right: "R", Top: "T", Bottom: "B"}, |
| 548 |
}, |
| 549 |
}, |
| 550 |
Footer: tw.CellConfig{ |
| 551 |
Formatting: tw.CellFormatting{ |
| 552 |
AutoFormat: tw.On, |
| 553 |
}, |
| 554 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 555 |
Padding: tw.CellPadding{ |
| 556 |
Global: tw.Padding{Left: "*", Right: "*", Top: "", Bottom: ""}, |
| 557 |
}, |
| 558 |
}, |
| 559 |
} |
| 560 |
|
| 561 |
var buf bytes.Buffer |
| 562 |
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(c)) |
| 563 |
table.Header([]string{"Name", "Age", "City"}) |
| 564 |
table.Bulk(data) |
| 565 |
table.Render() |
| 566 |
|
| 567 |
expected := ` |
| 568 |
┌─────────┬─────────────────────────┬──────┐ |
| 569 |
│ ^^^^^^^ │ ^^^^^^^^^^^^^^^^^^^^^^^ │ ^^^^ │ |
| 570 |
│ NAME │ AGE │ CITY │ |
| 571 |
│ ^^^^^^^ │ ^^^^^^^^^^^^^^^^^^^^^^^ │ ^^^^ │ |
| 572 |
├─────────┼─────────────────────────┼──────┤ |
| 573 |
│LTTTTTTTR│LTTTTTTTTTTTTTTTTTTTTTTTR│LTTTTR│ |
| 574 |
│LRegularR│LLLLLLregular lineRRRRRRR│LL1RRR│ |
| 575 |
│LBBBBBBBR│LBBBBBBBBBBBBBBBBBBBBBBBR│LBBBBR│ |
| 576 |
│LTTTTTTTR│LTTTTTTTTTTTTTTTTTTTTTTTR│LTTTTR│ |
| 577 |
│LLThickRR│Lparticularly thick lineR│LL2RRR│ |
| 578 |
│LBBBBBBBR│LBBBBBBBBBBBBBBBBBBBBBBBR│LBBBBR│ |
| 579 |
│LTTTTTTTR│LTTTTTTTTTTTTTTTTTTTTTTTR│LTTTTR│ |
| 580 |
│LDoubleRR│LLLLLLLdouble lineRRRRRRR│LL3RRR│ |
| 581 |
│LBBBBBBBR│LBBBBBBBBBBBBBBBBBBBBBBBR│LBBBBR│ |
| 582 |
└─────────┴─────────────────────────┴──────┘ |
| 583 |
` |
| 584 |
visualCheck(t, "TableWithCustomPadding", buf.String(), expected) |
| 585 |
} |
| 586 |
|
| 587 |
func TestStreamBorders(t *testing.T) { |
| 588 |
data := [][]string{{"A", "B"}, {"C", "D"}} |
| 589 |
widths := map[int]int{0: 3, 1: 3} // Content (1) + padding (1+1) = 3 |
| 590 |
|
| 591 |
tests := []struct { |
| 592 |
name string |
| 593 |
borders tw.Border |
| 594 |
expected string |
| 595 |
}{ |
| 596 |
{ |
| 597 |
name: "All Off", |
| 598 |
borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.Off, Bottom: tw.Off}, |
| 599 |
expected: ` |
| 600 |
A │ B |
| 601 |
C │ D |
| 602 |
`, |
| 603 |
}, |
| 604 |
{ |
| 605 |
name: "No Left/Right", |
| 606 |
borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.On, Bottom: tw.On}, |
| 607 |
expected: ` |
| 608 |
───┬─── |
| 609 |
A │ B |
| 610 |
C │ D |
| 611 |
───┴─── |
| 612 |
`, |
| 613 |
}, |
| 614 |
{ |
| 615 |
name: "No Top/Bottom", |
| 616 |
borders: tw.Border{Left: tw.On, Right: tw.On, Top: tw.Off, Bottom: tw.Off}, |
| 617 |
expected: ` |
| 618 |
│ A │ B │ |
| 619 |
│ C │ D │ |
| 620 |
`, |
| 621 |
}, |
| 622 |
{ |
| 623 |
name: "Only Left", |
| 624 |
borders: tw.Border{Left: tw.On, Right: tw.Off, Top: tw.Off, Bottom: tw.Off}, |
| 625 |
expected: ` |
| 626 |
│ A │ B |
| 627 |
│ C │ D |
| 628 |
`, |
| 629 |
}, |
| 630 |
{ |
| 631 |
name: "Default", |
| 632 |
borders: tw.Border{Left: tw.On, Right: tw.On, Top: tw.On, Bottom: tw.On}, |
| 633 |
expected: ` |
| 634 |
┌───┬───┐ |
| 635 |
│ A │ B │ |
| 636 |
│ C │ D │ |
| 637 |
└───┴───┘ |
| 638 |
`, |
| 639 |
}, |
| 640 |
} |
| 641 |
|
| 642 |
for _, tt := range tests { |
| 643 |
t.Run(tt.name, func(t *testing.T) { |
| 644 |
var buf bytes.Buffer |
| 645 |
r := renderer.NewBlueprint( |
| 646 |
tw.Rendition{ |
| 647 |
Borders: tt.borders, |
| 648 |
}, |
| 649 |
) |
| 650 |
st := tablewriter.NewTable(&buf, |
| 651 |
tablewriter.WithConfig(tablewriter.Config{ |
| 652 |
Row: tw.CellConfig{ |
| 653 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 654 |
}, |
| 655 |
Widths: tw.CellWidth{PerColumn: widths}, |
| 656 |
}), |
| 657 |
tablewriter.WithRenderer(r), |
| 658 |
tablewriter.WithDebug(false), |
| 659 |
tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), |
| 660 |
) |
| 661 |
err := st.Start() |
| 662 |
if err != nil { |
| 663 |
t.Fatalf("Start failed: %v", err) |
| 664 |
} |
| 665 |
st.Append(data[0]) |
| 666 |
st.Append(data[1]) |
| 667 |
err = st.Close() |
| 668 |
if err != nil { |
| 669 |
t.Fatalf("End failed: %v", err) |
| 670 |
} |
| 671 |
|
| 672 |
if !visualCheck(t, "StreamBorders_"+tt.name, buf.String(), tt.expected) { |
| 673 |
fmt.Printf("--- DEBUG LOG for %s ---\n", tt.name) |
| 674 |
fmt.Println(st.Debug().String()) |
| 675 |
t.Fail() |
| 676 |
} |
| 677 |
}) |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
func TestAlignmentMigration(t *testing.T) { |
| 682 |
// Test new CellAlignment |
| 683 |
buf := &bytes.Buffer{} |
| 684 |
|
| 685 |
t.Run("NewCellAlignment", func(t *testing.T) { |
| 686 |
table := tablewriter.NewTable(buf) |
| 687 |
table.Configure(func(cfg *tablewriter.Config) { |
| 688 |
cfg.Header.Alignment.Global = tw.AlignCenter |
| 689 |
cfg.Row.Alignment.PerColumn = []tw.Align{tw.AlignLeft, tw.AlignRight} |
| 690 |
}) |
| 691 |
table.Header([]string{"Name", "Age Of User"}) |
| 692 |
table.Append([]string{"Alice Samsung", "30"}) |
| 693 |
table.Render() |
| 694 |
|
| 695 |
expected := ` |
| 696 |
┌───────────────┬─────────────┐ |
| 697 |
│ NAME │ AGE OF USER │ |
| 698 |
├───────────────┼─────────────┤ |
| 699 |
│ Alice Samsung │ 30 │ |
| 700 |
└───────────────┴─────────────┘ |
| 701 |
` |
| 702 |
if !visualCheck(t, "New CellAlignment", buf.String(), expected) { |
| 703 |
t.Fatal("New CellAlignment rendering failed") |
| 704 |
} |
| 705 |
}) |
| 706 |
|
| 707 |
t.Run("DeprecatedAlignment", func(t *testing.T) { |
| 708 |
buf.Reset() |
| 709 |
|
| 710 |
table := tablewriter.NewTable(buf) |
| 711 |
table.Configure(func(cfg *tablewriter.Config) { |
| 712 |
cfg.Header.Formatting.Alignment = tw.AlignCenter |
| 713 |
cfg.Row.ColumnAligns = []tw.Align{tw.AlignLeft, tw.AlignRight} |
| 714 |
}) |
| 715 |
table.Header([]string{"Name", "Age Of User"}) |
| 716 |
table.Append([]string{"Alice Samsung", "30"}) |
| 717 |
table.Render() |
| 718 |
|
| 719 |
expected := ` |
| 720 |
┌───────────────┬─────────────┐ |
| 721 |
│ NAME │ AGE OF USER │ |
| 722 |
├───────────────┼─────────────┤ |
| 723 |
│ Alice Samsung │ 30 │ |
| 724 |
└───────────────┴─────────────┘ |
| 725 |
` |
| 726 |
|
| 727 |
if !visualCheck(t, "Deprecated Alignment Fields", buf.String(), expected) { |
| 728 |
t.Fatal("Deprecated ColumnAligns and Formatting.Alignment rendering failed") |
| 729 |
} |
| 730 |
}) |
| 731 |
|
| 732 |
t.Run("TableConfigureBasic", func(t *testing.T) { |
| 733 |
buf.Reset() |
| 734 |
table := tablewriter.NewTable(buf) |
| 735 |
table.Configure(func(cfg *tablewriter.Config) { |
| 736 |
cfg.Header.Formatting.Alignment = tw.AlignLeft |
| 737 |
cfg.Row.Formatting.Alignment = tw.AlignRight |
| 738 |
}) |
| 739 |
table.Header([]string{"NAME", "DEPARTMENT", "SALARY"}) |
| 740 |
table.Append([]string{"Alice", "Engineering", "120000"}) |
| 741 |
table.Append([]string{"Bob", "Marketing", "85000"}) |
| 742 |
table.Render() |
| 743 |
|
| 744 |
expectedConfigure := ` |
| 745 |
┌───────┬─────────────┬────────┐ |
| 746 |
│ NAME │ DEPARTMENT │ SALARY │ |
| 747 |
├───────┼─────────────┼────────┤ |
| 748 |
│ Alice │ Engineering │ 120000 │ |
| 749 |
│ Bob │ Marketing │ 85000 │ |
| 750 |
└───────┴─────────────┴────────┘ |
| 751 |
|
| 752 |
` |
| 753 |
if !visualCheck(t, "Table_Configure_Basic", buf.String(), expectedConfigure) { |
| 754 |
t.Fatal("Table_Configure_Basic rendering failed") |
| 755 |
} |
| 756 |
}) |
| 757 |
|
| 758 |
t.Run("HorizontalMergeEachLineCenter", func(t *testing.T) { |
| 759 |
// Test HorizontalMergeEachLineCenter scenario |
| 760 |
buf.Reset() |
| 761 |
table := tablewriter.NewTable(buf) |
| 762 |
table.Configure(func(cfg *tablewriter.Config) { |
| 763 |
cfg.Row.Alignment.Global = tw.AlignCenter |
| 764 |
cfg.Row.Merging.Mode = tw.MergeHorizontal |
| 765 |
}) |
| 766 |
table.Header([]string{"DATE", "SECTION A", "SECTION B", "SECTION C", "SECTION D", "SECTION E"}) |
| 767 |
table.Append([]string{"1/1/2014", "apple", "boy", "cat", "dog", "elephant"}) |
| 768 |
table.Render() |
| 769 |
|
| 770 |
expectedMerge := ` |
| 771 |
┌──────────┬───────────┬───────────┬───────────┬───────────┬───────────┐ |
| 772 |
│ DATE │ SECTION A │ SECTION B │ SECTION C │ SECTION D │ SECTION E │ |
| 773 |
├──────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ |
| 774 |
│ 1/1/2014 │ apple │ boy │ cat │ dog │ elephant │ |
| 775 |
└──────────┴───────────┴───────────┴───────────┴───────────┴───────────┘ |
| 776 |
` |
| 777 |
if !visualCheck(t, "HorizontalMergeEachLineCenter", buf.String(), expectedMerge) { |
| 778 |
t.Fatal("HorizontalMergeEachLineCenter rendering failed") |
| 779 |
} |
| 780 |
}) |
| 781 |
|
| 782 |
t.Run("StreamBasic", func(t *testing.T) { |
| 783 |
buf.Reset() |
| 784 |
table := tablewriter.NewTable(buf) |
| 785 |
table.Configure(func(cfg *tablewriter.Config) { |
| 786 |
cfg.Footer.Alignment.Global = tw.AlignRight |
| 787 |
cfg.Stream.Enable = true |
| 788 |
}) |
| 789 |
table.Start() |
| 790 |
table.Header([]string{"NAME", "AGE", "CITY"}) |
| 791 |
table.Append([]string{"Alice", "25", "New York"}) |
| 792 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 793 |
table.Footer([]string{"Total", "55", "*"}) |
| 794 |
table.Close() |
| 795 |
|
| 796 |
expectedStream := ` |
| 797 |
┌────────┬────────┬────────┐ |
| 798 |
│ NAME │ AGE │ CITY │ |
| 799 |
├────────┼────────┼────────┤ |
| 800 |
│ Alice │ 25 │ New │ |
| 801 |
│ │ │ York │ |
| 802 |
│ Bob │ 30 │ Boston │ |
| 803 |
├────────┼────────┼────────┤ |
| 804 |
│ Total │ 55 │ * │ |
| 805 |
└────────┴────────┴────────┘ |
| 806 |
` |
| 807 |
if !visualCheck(t, "StreamBasic", buf.String(), expectedStream) { |
| 808 |
t.Fatal("StreamBasic rendering failed") |
| 809 |
} |
| 810 |
}) |
| 811 |
} |