| 1 |
package tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/olekukonko/tablewriter" |
| 8 |
"github.com/olekukonko/tablewriter/renderer" |
| 9 |
"github.com/olekukonko/tablewriter/tw" |
| 10 |
) |
| 11 |
|
| 12 |
func TestMarkdownBasicTable(t *testing.T) { |
| 13 |
var buf bytes.Buffer |
| 14 |
table := tablewriter.NewTable(&buf, |
| 15 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 16 |
) |
| 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 |
| NAME | AGE | CITY | |
| 24 |
|:------|:----|:---------| |
| 25 |
| Alice | 25 | New York | |
| 26 |
| Bob | 30 | Boston | |
| 27 |
` |
| 28 |
if !visualCheck(t, "MarkdownBasicTable", buf.String(), expected) { |
| 29 |
t.Error(table.Debug()) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func TestMarkdownRule1NoExplicitAlignment(t *testing.T) { |
| 34 |
var buf bytes.Buffer |
| 35 |
table := tablewriter.NewTable(&buf, |
| 36 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 37 |
tablewriter.WithConfig(tablewriter.Config{ |
| 38 |
Debug: true, |
| 39 |
}), |
| 40 |
) |
| 41 |
table.Header([]string{"Change", "Resource Type", "Resource Name", "Address"}) |
| 42 |
table.Append([]string{"CREATE", "myresource", "foo", "myresource.foo"}) |
| 43 |
table.Append([]string{"REPLACE", "myresource", "foobar", "myresource.foobar"}) |
| 44 |
table.Render() |
| 45 |
|
| 46 |
expected := ` |
| 47 |
| CHANGE | RESOURCE TYPE | RESOURCE NAME | ADDRESS | |
| 48 |
|:--------|:--------------|:--------------|:------------------| |
| 49 |
| CREATE | myresource | foo | myresource.foo | |
| 50 |
| REPLACE | myresource | foobar | myresource.foobar | |
| 51 |
` |
| 52 |
if !visualCheck(t, "TestMarkdownRule1NoExplicitAlignment", buf.String(), expected) { |
| 53 |
t.Error(table.Debug()) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestMarkdownRule2BodyOnly(t *testing.T) { |
| 58 |
var buf bytes.Buffer |
| 59 |
table := tablewriter.NewTable(&buf, |
| 60 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 61 |
tablewriter.WithConfig(tablewriter.Config{ |
| 62 |
Row: tw.CellConfig{ |
| 63 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 64 |
}, |
| 65 |
Debug: true, |
| 66 |
}), |
| 67 |
) |
| 68 |
table.Header([]string{"Change", "Resource Type", "Resource Name", "Address"}) |
| 69 |
table.Append([]string{"CREATE", "myresource", "foo", "myresource.foo"}) |
| 70 |
table.Append([]string{"REPLACE", "myresource", "foobar", "myresource.foobar"}) |
| 71 |
table.Render() |
| 72 |
|
| 73 |
expected := ` |
| 74 |
| CHANGE | RESOURCE TYPE | RESOURCE NAME | ADDRESS | |
| 75 |
|:--------|:--------------|:--------------|:------------------| |
| 76 |
| CREATE | myresource | foo | myresource.foo | |
| 77 |
| REPLACE | myresource | foobar | myresource.foobar | |
| 78 |
` |
| 79 |
if !visualCheck(t, "TestMarkdownRule2BodyOnly", buf.String(), expected) { |
| 80 |
t.Error(table.Debug()) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestMarkdownRule3BothSet(t *testing.T) { |
| 85 |
var buf bytes.Buffer |
| 86 |
table := tablewriter.NewTable(&buf, |
| 87 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 88 |
tablewriter.WithConfig(tablewriter.Config{ |
| 89 |
Header: tw.CellConfig{ |
| 90 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 91 |
}, |
| 92 |
Row: tw.CellConfig{ |
| 93 |
Alignment: tw.CellAlignment{Global: tw.AlignRight}, |
| 94 |
}, |
| 95 |
Debug: true, |
| 96 |
}), |
| 97 |
) |
| 98 |
table.Header([]string{"Name", "Age", "City"}) |
| 99 |
table.Append([]string{"Alice", "25", "New York"}) |
| 100 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 101 |
table.Render() |
| 102 |
|
| 103 |
expected := ` |
| 104 |
| NAME | AGE | CITY | |
| 105 |
|------:|----:|---------:| |
| 106 |
| Alice | 25 | New York | |
| 107 |
| Bob | 30 | Boston | |
| 108 |
` |
| 109 |
if !visualCheck(t, "TestMarkdownRule3BothSet", buf.String(), expected) { |
| 110 |
t.Error(table.Debug()) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestMarkdownRule4HeaderOnly(t *testing.T) { |
| 115 |
var buf bytes.Buffer |
| 116 |
table := tablewriter.NewTable(&buf, |
| 117 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 118 |
tablewriter.WithConfig(tablewriter.Config{ |
| 119 |
Header: tw.CellConfig{ |
| 120 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 121 |
}, |
| 122 |
Debug: true, |
| 123 |
}), |
| 124 |
) |
| 125 |
table.Header([]string{"Name", "Age", "City"}) |
| 126 |
table.Append([]string{"Alice", "25", "New York"}) |
| 127 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 128 |
table.Render() |
| 129 |
|
| 130 |
expected := ` |
| 131 |
| NAME | AGE | CITY | |
| 132 |
|:------|:----|:---------| |
| 133 |
| Alice | 25 | New York | |
| 134 |
| Bob | 30 | Boston | |
| 135 |
` |
| 136 |
if !visualCheck(t, "TestMarkdownRule4HeaderOnly", buf.String(), expected) { |
| 137 |
t.Error(table.Debug()) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func TestMarkdownPerColumnAlignment(t *testing.T) { |
| 142 |
var buf bytes.Buffer |
| 143 |
table := tablewriter.NewTable(&buf, |
| 144 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 145 |
tablewriter.WithConfig(tablewriter.Config{ |
| 146 |
Header: tw.CellConfig{ |
| 147 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignLeft, tw.AlignCenter, tw.AlignRight}}, |
| 148 |
}, |
| 149 |
Row: tw.CellConfig{ |
| 150 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignRight, tw.AlignLeft, tw.AlignCenter}}, |
| 151 |
}, |
| 152 |
Debug: true, |
| 153 |
}), |
| 154 |
) |
| 155 |
table.Header([]string{"Name", "Age", "City"}) |
| 156 |
table.Append([]string{"Alice", "25", "New York"}) |
| 157 |
table.Render() |
| 158 |
|
| 159 |
expected := ` |
| 160 |
| NAME | AGE | CITY | |
| 161 |
|------:|:----|:--------:| |
| 162 |
| Alice | 25 | New York | |
| 163 |
` |
| 164 |
if !visualCheck(t, "TestMarkdownPerColumnAlignment", buf.String(), expected) { |
| 165 |
t.Error(table.Debug()) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func TestMarkdownAlignment(t *testing.T) { |
| 170 |
var buf bytes.Buffer |
| 171 |
table := tablewriter.NewTable(&buf, |
| 172 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 173 |
tablewriter.WithConfig(tablewriter.Config{ |
| 174 |
Header: tw.CellConfig{ |
| 175 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignLeft, tw.AlignRight, tw.AlignRight, tw.AlignCenter}}, |
| 176 |
}, |
| 177 |
}), |
| 178 |
) |
| 179 |
table.Header([]string{"Name", "Age", "City", "Status"}) |
| 180 |
table.Append([]string{"Alice", "25", "New York", "OK"}) |
| 181 |
table.Append([]string{"Bob", "30", "Boston", "ERROR"}) |
| 182 |
table.Render() |
| 183 |
|
| 184 |
expected := ` |
| 185 |
| NAME | AGE | CITY | STATUS | |
| 186 |
|:------|:----|:---------|:-------| |
| 187 |
| Alice | 25 | New York | OK | |
| 188 |
| Bob | 30 | Boston | ERROR | |
| 189 |
` |
| 190 |
if !visualCheck(t, "MarkdownAlignment", buf.String(), expected) { |
| 191 |
t.Error(table.Debug()) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func TestMarkdownNoBorders(t *testing.T) { |
| 196 |
var buf bytes.Buffer |
| 197 |
table := tablewriter.NewTable(&buf, |
| 198 |
tablewriter.WithRenderer(renderer.NewMarkdown(tw.Rendition{ |
| 199 |
Borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.Off, Bottom: tw.Off}, |
| 200 |
})), |
| 201 |
tablewriter.WithConfig(tablewriter.Config{ |
| 202 |
Header: tw.CellConfig{ |
| 203 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignLeft}}, |
| 204 |
}, |
| 205 |
}), |
| 206 |
) |
| 207 |
|
| 208 |
table.Header([]string{"Name", "Age", "City"}) |
| 209 |
table.Append([]string{"Alice", "25", "New York"}) |
| 210 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 211 |
table.Render() |
| 212 |
|
| 213 |
expected := ` |
| 214 |
NAME | AGE | CITY |
| 215 |
:------|:----|:--------- |
| 216 |
Alice | 25 | New York |
| 217 |
Bob | 30 | Boston |
| 218 |
` |
| 219 |
if !visualCheck(t, "MarkdownNoBorders", buf.String(), expected) { |
| 220 |
t.Error(table.Debug()) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
func TestMarkdownUnicode(t *testing.T) { |
| 225 |
var buf bytes.Buffer |
| 226 |
table := tablewriter.NewTable(&buf, |
| 227 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 228 |
) |
| 229 |
table.Header([]string{"Name", "Age", "City"}) |
| 230 |
table.Append([]string{"Bøb", "30", "Tōkyō"}) |
| 231 |
table.Append([]string{"José", "28", "México"}) |
| 232 |
table.Append([]string{"张三", "35", "北京"}) |
| 233 |
table.Render() |
| 234 |
|
| 235 |
expected := ` |
| 236 |
| NAME | AGE | CITY | |
| 237 |
|:-----|:----|:-------| |
| 238 |
| Bøb | 30 | Tōkyō | |
| 239 |
| José | 28 | México | |
| 240 |
| 张三 | 35 | 北京 | |
| 241 |
` |
| 242 |
if !visualCheck(t, "MarkdownUnicode", buf.String(), expected) { |
| 243 |
t.Error(table.Debug()) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func TestMarkdownLongHeaders(t *testing.T) { |
| 248 |
var buf bytes.Buffer |
| 249 |
c := tablewriter.Config{ |
| 250 |
Header: tw.CellConfig{ |
| 251 |
Formatting: tw.CellFormatting{ |
| 252 |
AutoWrap: tw.WrapTruncate, |
| 253 |
}, |
| 254 |
ColMaxWidths: tw.CellWidth{Global: 20}, |
| 255 |
}, |
| 256 |
} |
| 257 |
table := tablewriter.NewTable(&buf, |
| 258 |
tablewriter.WithConfig(c), |
| 259 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 260 |
) |
| 261 |
table.Header([]string{"Name", "Age", "Very Long Header That Needs Truncation"}) |
| 262 |
table.Append([]string{"Alice", "25", "New York"}) |
| 263 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 264 |
table.Render() |
| 265 |
|
| 266 |
expected := ` |
| 267 |
| NAME | AGE | VERY LONG HEADER… | |
| 268 |
|:------|:----|:------------------| |
| 269 |
| Alice | 25 | New York | |
| 270 |
| Bob | 30 | Boston | |
| 271 |
` |
| 272 |
if !visualCheck(t, "MarkdownLongHeaders", buf.String(), expected) { |
| 273 |
t.Error(table.Debug()) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func TestMarkdownLongValues(t *testing.T) { |
| 278 |
var buf bytes.Buffer |
| 279 |
c := tablewriter.Config{ |
| 280 |
Row: tw.CellConfig{ |
| 281 |
Formatting: tw.CellFormatting{ |
| 282 |
AutoWrap: tw.WrapNormal, |
| 283 |
}, |
| 284 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 285 |
ColMaxWidths: tw.CellWidth{Global: 20}, |
| 286 |
}, |
| 287 |
} |
| 288 |
table := tablewriter.NewTable(&buf, |
| 289 |
tablewriter.WithConfig(c), |
| 290 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 291 |
) |
| 292 |
table.Header([]string{"No", "Description", "Note"}) |
| 293 |
table.Append([]string{"1", "This is a very long description that should wrap", "Short"}) |
| 294 |
table.Append([]string{"2", "Short desc", "Another note"}) |
| 295 |
table.Render() |
| 296 |
|
| 297 |
expected := ` |
| 298 |
| NO | DESCRIPTION | NOTE | |
| 299 |
|:---|:-----------------|:-------------| |
| 300 |
| 1 | This is a very | Short | |
| 301 |
| | long description | | |
| 302 |
| | that should wrap | | |
| 303 |
| 2 | Short desc | Another note | |
| 304 |
` |
| 305 |
if !visualCheck(t, "MarkdownLongValues", buf.String(), expected) { |
| 306 |
t.Error(table.Debug()) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func TestMarkdownCustomPadding(t *testing.T) { |
| 311 |
var buf bytes.Buffer |
| 312 |
c := tablewriter.Config{ |
| 313 |
Header: tw.CellConfig{ |
| 314 |
Padding: tw.CellPadding{ |
| 315 |
Global: tw.Padding{Left: "*", Right: "*", Top: "", Bottom: ""}, |
| 316 |
}, |
| 317 |
}, |
| 318 |
Row: tw.CellConfig{ |
| 319 |
Padding: tw.CellPadding{ |
| 320 |
Global: tw.Padding{Left: ">", Right: "<", Top: "", Bottom: ""}, |
| 321 |
}, |
| 322 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 323 |
}, |
| 324 |
} |
| 325 |
table := tablewriter.NewTable(&buf, |
| 326 |
tablewriter.WithConfig(c), |
| 327 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 328 |
) |
| 329 |
table.Header([]string{"Name", "Age", "City"}) |
| 330 |
table.Append([]string{"Alice", "25", "New York"}) |
| 331 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 332 |
table.Render() |
| 333 |
|
| 334 |
expected := ` |
| 335 |
|*NAME**|*AGE*|***CITY***| |
| 336 |
|:------|:----|:---------| |
| 337 |
|>Alice<|>25<<|>New York<| |
| 338 |
|>Bob<<<|>30<<|>Boston<<<| |
| 339 |
` |
| 340 |
if !visualCheck(t, "MarkdownCustomPadding", buf.String(), expected) { |
| 341 |
t.Error(table.Debug()) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
func TestMarkdownHorizontalMerge(t *testing.T) { |
| 346 |
var buf bytes.Buffer |
| 347 |
c := tablewriter.Config{ |
| 348 |
Header: tw.CellConfig{ |
| 349 |
Merging: tw.CellMerging{ |
| 350 |
Mode: tw.MergeHorizontal, |
| 351 |
}, |
| 352 |
}, |
| 353 |
Row: tw.CellConfig{ |
| 354 |
Merging: tw.CellMerging{ |
| 355 |
Mode: tw.MergeHorizontal, |
| 356 |
}, |
| 357 |
}, |
| 358 |
} |
| 359 |
table := tablewriter.NewTable(&buf, |
| 360 |
tablewriter.WithConfig(c), |
| 361 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 362 |
) |
| 363 |
table.Header([]string{"Merged", "Merged", "Normal"}) |
| 364 |
table.Append([]string{"Same", "Same", "Unique"}) |
| 365 |
table.Render() |
| 366 |
|
| 367 |
expected := ` |
| 368 |
| MERGED | NORMAL | |
| 369 |
|:----------------|:-------| |
| 370 |
| Same | Unique | |
| 371 |
` |
| 372 |
if !visualCheck(t, "MarkdownHorizontalMerge", buf.String(), expected) { |
| 373 |
t.Error(table.Debug()) |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
func TestMarkdownEmptyTable(t *testing.T) { |
| 378 |
var buf bytes.Buffer |
| 379 |
table := tablewriter.NewTable(&buf, |
| 380 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 381 |
) |
| 382 |
table.Render() |
| 383 |
|
| 384 |
expected := "" |
| 385 |
if !visualCheck(t, "MarkdownEmptyTable", buf.String(), expected) { |
| 386 |
t.Error(table.Debug()) |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
func TestMarkdownWithFooter(t *testing.T) { |
| 391 |
var buf bytes.Buffer |
| 392 |
c := tablewriter.Config{ |
| 393 |
Footer: tw.CellConfig{ |
| 394 |
Alignment: tw.CellAlignment{Global: tw.AlignRight}, |
| 395 |
}, |
| 396 |
} |
| 397 |
table := tablewriter.NewTable(&buf, |
| 398 |
tablewriter.WithConfig(c), |
| 399 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 400 |
) |
| 401 |
table.Header([]string{"Name", "Age", "City"}) |
| 402 |
table.Append([]string{"Alice", "25", "New York"}) |
| 403 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 404 |
table.Footer([]string{"Total", "2", ""}) |
| 405 |
table.Render() |
| 406 |
|
| 407 |
expected := ` |
| 408 |
| NAME | AGE | CITY | |
| 409 |
|:------|:----|:---------| |
| 410 |
| Alice | 25 | New York | |
| 411 |
| Bob | 30 | Boston | |
| 412 |
| Total | 2 | | |
| 413 |
` |
| 414 |
if !visualCheck(t, "MarkdownWithFooter", buf.String(), expected) { |
| 415 |
t.Error(table.Debug()) |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
func TestMarkdownAlignmentNone(t *testing.T) { |
| 420 |
t.Run("AlignNone", func(t *testing.T) { |
| 421 |
var buf bytes.Buffer |
| 422 |
table := tablewriter.NewTable(&buf, tablewriter.WithRenderer(renderer.NewMarkdown())) |
| 423 |
table.Configure(func(cfg *tablewriter.Config) { |
| 424 |
cfg.Header.Alignment.PerColumn = []tw.Align{tw.AlignNone} |
| 425 |
cfg.Row.Alignment.PerColumn = []tw.Align{tw.AlignNone} |
| 426 |
cfg.Debug = true |
| 427 |
}) |
| 428 |
table.Header([]string{"Header"}) |
| 429 |
table.Append([]string{"Data"}) |
| 430 |
table.Render() |
| 431 |
|
| 432 |
expected := ` |
| 433 |
| HEADER | |
| 434 |
|:------:| |
| 435 |
| Data | |
| 436 |
` |
| 437 |
if !visualCheck(t, "AlignNone", buf.String(), expected) { |
| 438 |
t.Fatal(table.Debug()) |
| 439 |
} |
| 440 |
}) |
| 441 |
} |
| 442 |
|
| 443 |
func TestMarkdownMixedAlignments(t *testing.T) { |
| 444 |
var buf bytes.Buffer |
| 445 |
table := tablewriter.NewTable(&buf, |
| 446 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 447 |
tablewriter.WithConfig(tablewriter.Config{ |
| 448 |
Header: tw.CellConfig{ |
| 449 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignLeft, tw.AlignNone, tw.AlignRight}}, |
| 450 |
}, |
| 451 |
Row: tw.CellConfig{ |
| 452 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignNone, tw.AlignCenter, tw.AlignNone}}, |
| 453 |
}, |
| 454 |
}), |
| 455 |
) |
| 456 |
table.Header([]string{"Left", "Default", "Right"}) |
| 457 |
table.Append([]string{"A", "B", "C"}) |
| 458 |
table.Render() |
| 459 |
|
| 460 |
expected := ` |
| 461 |
| LEFT | DEFAULT | RIGHT | |
| 462 |
|:-----|:-------:|------:| |
| 463 |
| A | B | C | |
| 464 |
` |
| 465 |
if !visualCheck(t, "TestMarkdownMixedAlignments", buf.String(), expected) { |
| 466 |
t.Error(table.Debug()) |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
func TestMarkdownCenterAlignment(t *testing.T) { |
| 471 |
var buf bytes.Buffer |
| 472 |
table := tablewriter.NewTable(&buf, |
| 473 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 474 |
tablewriter.WithConfig(tablewriter.Config{ |
| 475 |
Header: tw.CellConfig{ |
| 476 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 477 |
}, |
| 478 |
Row: tw.CellConfig{ |
| 479 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 480 |
}, |
| 481 |
}), |
| 482 |
) |
| 483 |
table.Header([]string{"Name", "Age"}) |
| 484 |
table.Append([]string{"Alice", "25"}) |
| 485 |
table.Render() |
| 486 |
|
| 487 |
expected := ` |
| 488 |
| NAME | AGE | |
| 489 |
|:-----:|:---:| |
| 490 |
| Alice | 25 | |
| 491 |
` |
| 492 |
if !visualCheck(t, "TestMarkdownCenterAlignment", buf.String(), expected) { |
| 493 |
t.Error(table.Debug()) |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
func TestMarkdownHeaderOnlyTable(t *testing.T) { |
| 498 |
var buf bytes.Buffer |
| 499 |
table := tablewriter.NewTable(&buf, |
| 500 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 501 |
) |
| 502 |
table.Header([]string{"Name", "Age", "City"}) |
| 503 |
// No Append, no Footer — just headers |
| 504 |
table.Render() |
| 505 |
|
| 506 |
expected := ` |
| 507 |
| NAME | AGE | CITY | |
| 508 |
|:----:|:---:|:----:| |
| 509 |
` |
| 510 |
if !visualCheck(t, "MarkdownHeaderOnlyTable", buf.String(), expected) { |
| 511 |
t.Error(table.Debug()) |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
func TestMarkdownRendererCloseFlushesSeparator(t *testing.T) { |
| 516 |
var buf bytes.Buffer |
| 517 |
md := renderer.NewMarkdown() |
| 518 |
md.Start(&buf) |
| 519 |
|
| 520 |
// Simulate header call with correct widths and uppercased headers |
| 521 |
ctx := tw.Formatting{ |
| 522 |
Row: tw.RowContext{ |
| 523 |
Current: map[int]tw.CellContext{ |
| 524 |
0: {Align: tw.AlignLeft, Width: 6}, |
| 525 |
1: {Align: tw.AlignCenter, Width: 5}, |
| 526 |
}, |
| 527 |
// Only initialize keys 0 and 1 so it strictly limits to 2 columns. |
| 528 |
// Because no explicit widths are given, they default to 0, |
| 529 |
// triggering the minimum 3-character separator. |
| 530 |
Widths: tw.NewMapperWithKeys[int, int](0, 1), |
| 531 |
}, |
| 532 |
} |
| 533 |
|
| 534 |
md.Header([][]string{{"NAME", "AGE"}}, ctx) |
| 535 |
|
| 536 |
// No Row() called — body alignment unresolved |
| 537 |
// Close should flush using header alignment as fallback |
| 538 |
err := md.Close() |
| 539 |
if err != nil { |
| 540 |
t.Fatalf("Close failed: %v", err) |
| 541 |
} |
| 542 |
|
| 543 |
got := buf.String() |
| 544 |
|
| 545 |
// Because column widths default to 0, the separators fall back to the min width (3 chars). |
| 546 |
// Left alignment becomes ":--" and Center alignment becomes ":-:" |
| 547 |
want := "| NAME | AGE |\n|:--|:-:|\n" |
| 548 |
|
| 549 |
if got != want { |
| 550 |
t.Errorf("Close() flush mismatch\ngot: %q\nwant: %q", got, want) |
| 551 |
} |
| 552 |
} |