| 1 |
package tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/olekukonko/tablewriter" |
| 11 |
"github.com/olekukonko/tablewriter/renderer" |
| 12 |
"github.com/olekukonko/tablewriter/tw" |
| 13 |
) |
| 14 |
|
| 15 |
// createStreamTable creates a TableStream with the renderer for testing. |
| 16 |
func createStreamTable(t *testing.T, w *bytes.Buffer, opts ...tablewriter.Option) *tablewriter.Table { |
| 17 |
t.Helper() |
| 18 |
opts = append(opts, tablewriter.WithRenderer(renderer.NewBlueprint())) |
| 19 |
return tablewriter.NewTable(w, opts...) |
| 20 |
} |
| 21 |
|
| 22 |
func TestStreamTableDefault(t *testing.T) { |
| 23 |
var buf bytes.Buffer |
| 24 |
|
| 25 |
t.Run("disabled", func(t *testing.T) { |
| 26 |
buf.Reset() |
| 27 |
table := tablewriter.NewTable(&buf, tablewriter.WithStreaming(tw.StreamConfig{Enable: false})) |
| 28 |
|
| 29 |
// err := table.Start() |
| 30 |
// if err != nil { |
| 31 |
// t.Fatalf("Start failed: %v", err) |
| 32 |
//} |
| 33 |
|
| 34 |
table.Header([]string{"Name", "Age", "City"}) |
| 35 |
table.Append([]string{"Alice", "25", "New York"}) |
| 36 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 37 |
|
| 38 |
// err = table.Close() |
| 39 |
// if err != nil { |
| 40 |
// t.Fatalf("End failed: %v", err) |
| 41 |
//} |
| 42 |
|
| 43 |
table.Render() |
| 44 |
expected := ` |
| 45 |
┌───────┬─────┬──────────┐ |
| 46 |
│ NAME │ AGE │ CITY │ |
| 47 |
├───────┼─────┼──────────┤ |
| 48 |
│ Alice │ 25 │ New York │ |
| 49 |
│ Bob │ 30 │ Boston │ |
| 50 |
└───────┴─────┴──────────┘ |
| 51 |
` |
| 52 |
debug := visualCheck(t, "TestStreamTableDefault", buf.String(), expected) |
| 53 |
if !debug { |
| 54 |
t.Error(table.Debug()) |
| 55 |
} |
| 56 |
}) |
| 57 |
|
| 58 |
t.Run("enabled", func(t *testing.T) { |
| 59 |
buf.Reset() |
| 60 |
table := tablewriter.NewTable(&buf, tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), tablewriter.WithDebug(false)) |
| 61 |
|
| 62 |
err := table.Start() |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("Start failed: %v", err) |
| 65 |
} |
| 66 |
|
| 67 |
table.Header([]string{"Name", "Age", "City"}) |
| 68 |
table.Append([]string{"Alice", "25", "New York"}) |
| 69 |
table.Append([]string{"Bob", "30", "Boston"}) |
| 70 |
|
| 71 |
err = table.Close() |
| 72 |
if err != nil { |
| 73 |
t.Fatalf("End failed: %v", err) |
| 74 |
} |
| 75 |
|
| 76 |
expected := ` |
| 77 |
┌────────┬────────┬────────┐ |
| 78 |
│ NAME │ AGE │ CITY │ |
| 79 |
├────────┼────────┼────────┤ |
| 80 |
│ Alice │ 25 │ New │ |
| 81 |
│ │ │ York │ |
| 82 |
│ Bob │ 30 │ Boston │ |
| 83 |
└────────┴────────┴────────┘ |
| 84 |
` |
| 85 |
debug := visualCheck(t, "BasicTableRendering", buf.String(), expected) |
| 86 |
if !debug { |
| 87 |
t.Error(table.Debug()) |
| 88 |
} |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
// TestStreamBasic tests basic streaming table rendering with header, rows, and footer. |
| 93 |
func TestStreamBasic(t *testing.T) { |
| 94 |
var buf bytes.Buffer |
| 95 |
|
| 96 |
t.Run("TestStreamBasic", func(t *testing.T) { |
| 97 |
buf.Reset() |
| 98 |
st := createStreamTable(t, &buf, |
| 99 |
tablewriter.WithConfig(tablewriter.Config{ |
| 100 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 101 |
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, |
| 102 |
Footer: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, |
| 103 |
}), |
| 104 |
tablewriter.WithDebug(false), |
| 105 |
tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), |
| 106 |
) |
| 107 |
|
| 108 |
err := st.Start() |
| 109 |
if err != nil { |
| 110 |
t.Fatalf("Start failed: %v", err) |
| 111 |
} |
| 112 |
st.Header([]string{"Name", "Age", "City"}) |
| 113 |
st.Append([]string{"Alice", "25", "New York"}) |
| 114 |
st.Append([]string{"Bob", "30", "Boston"}) |
| 115 |
st.Footer([]string{"Total", "55", "*"}) |
| 116 |
err = st.Close() |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("End failed: %v", err) |
| 119 |
} |
| 120 |
|
| 121 |
// Widths: Name(5)+2=7, Age(3)+2=5, City(8)+2=10 |
| 122 |
expected := ` |
| 123 |
┌────────┬────────┬────────┐ |
| 124 |
│ NAME │ AGE │ CITY │ |
| 125 |
├────────┼────────┼────────┤ |
| 126 |
│ Alice │ 25 │ New │ |
| 127 |
│ │ │ York │ |
| 128 |
│ Bob │ 30 │ Boston │ |
| 129 |
├────────┼────────┼────────┤ |
| 130 |
│ Total │ 55 │ * │ |
| 131 |
└────────┴────────┴────────┘ |
| 132 |
` |
| 133 |
if !visualCheck(t, "StreamBasic", buf.String(), expected) { |
| 134 |
fmt.Println(st.Debug()) |
| 135 |
t.Fail() |
| 136 |
} |
| 137 |
}) |
| 138 |
|
| 139 |
t.Run("TestStreamBasicGlobal", func(t *testing.T) { |
| 140 |
buf.Reset() |
| 141 |
st := createStreamTable(t, &buf, |
| 142 |
tablewriter.WithConfig(tablewriter.Config{ |
| 143 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 144 |
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, |
| 145 |
Footer: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignRight}}, |
| 146 |
}), |
| 147 |
tablewriter.WithDebug(false), |
| 148 |
tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), |
| 149 |
) |
| 150 |
|
| 151 |
err := st.Start() |
| 152 |
if err != nil { |
| 153 |
t.Fatalf("Start failed: %v", err) |
| 154 |
} |
| 155 |
st.Header([]string{"Name", "Age", "City"}) |
| 156 |
st.Append([]string{"Alice", "25", "New York"}) |
| 157 |
st.Append([]string{"Bob", "30", "Boston"}) |
| 158 |
st.Footer([]string{"Total", "55", "*"}) |
| 159 |
|
| 160 |
err = st.Close() |
| 161 |
if err != nil { |
| 162 |
t.Fatalf("End failed: %v", err) |
| 163 |
} |
| 164 |
|
| 165 |
// Widths: Name(5)+2=7, Age(3)+2=5, City(8)+2=10 |
| 166 |
expected := ` |
| 167 |
┌────────┬────────┬────────┐ |
| 168 |
│ NAME │ AGE │ CITY │ |
| 169 |
├────────┼────────┼────────┤ |
| 170 |
│ Alice │ 25 │ New │ |
| 171 |
│ │ │ York │ |
| 172 |
│ Bob │ 30 │ Boston │ |
| 173 |
├────────┼────────┼────────┤ |
| 174 |
│ Total │ 55 │ * │ |
| 175 |
└────────┴────────┴────────┘ |
| 176 |
` |
| 177 |
if !visualCheck(t, "StreamBasic", buf.String(), expected) { |
| 178 |
fmt.Println(st.Debug()) |
| 179 |
t.Fail() |
| 180 |
} |
| 181 |
}) |
| 182 |
} |
| 183 |
|
| 184 |
// TestStreamWithFooterAlign tests streaming table with footer and custom alignments. |
| 185 |
func TestStreamWithFooterAlign(t *testing.T) { |
| 186 |
var buf bytes.Buffer |
| 187 |
st := createStreamTable(t, &buf, tablewriter.WithConfig(tablewriter.Config{ |
| 188 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 189 |
Row: tw.CellConfig{ |
| 190 |
Alignment: tw.CellAlignment{ |
| 191 |
Global: tw.AlignLeft, |
| 192 |
PerColumn: []tw.Align{tw.AlignLeft, tw.AlignCenter, tw.AlignRight}, |
| 193 |
}, |
| 194 |
}, |
| 195 |
Footer: tw.CellConfig{ |
| 196 |
Alignment: tw.CellAlignment{ |
| 197 |
Global: tw.AlignRight, |
| 198 |
PerColumn: []tw.Align{tw.AlignLeft, tw.AlignCenter, tw.AlignRight}, |
| 199 |
}, |
| 200 |
}, |
| 201 |
}), |
| 202 |
tablewriter.WithDebug(false), |
| 203 |
tablewriter.WithStreaming(tw.StreamConfig{Enable: true})) |
| 204 |
|
| 205 |
err := st.Start() |
| 206 |
if err != nil { |
| 207 |
t.Fatalf("Start failed: %v", err) |
| 208 |
} |
| 209 |
st.Header([]string{"Item", "Qty", "Price"}) // Widths: 4+2=6, 3+2=5, 5+2=7 |
| 210 |
st.Append([]string{"Item 1", "2", "1000.00"}) // Needs: 6+2=8, 1+2=3, 7+2=9 |
| 211 |
st.Append([]string{"Item 2", "10", "25.50"}) // Needs: 6+2=8, 2+2=4, 5+2=7 |
| 212 |
st.Footer([]string{"", "Total", "1025.50"}) // Needs: 0+2=2, 5+2=7, 7+2=9 |
| 213 |
err = st.Close() |
| 214 |
if err != nil { |
| 215 |
t.Fatalf("End failed: %v", err) |
| 216 |
} |
| 217 |
|
| 218 |
// Max widths: [8, 7, 9] |
| 219 |
expected := ` |
| 220 |
┌────────┬────────┬─────────┐ |
| 221 |
│ ITEM │ QTY │ PRICE │ |
| 222 |
├────────┼────────┼─────────┤ |
| 223 |
│ Item 1 │ 2 │ 1000.00 │ |
| 224 |
│ Item 2 │ 10 │ 25.50 │ |
| 225 |
├────────┼────────┼─────────┤ |
| 226 |
│ │ Total │ 1025.50 │ |
| 227 |
└────────┴────────┴─────────┘ |
| 228 |
` |
| 229 |
if !visualCheck(t, "StreamWithFooterAlign", buf.String(), expected) { |
| 230 |
fmt.Println("--- DEBUG LOG ---") |
| 231 |
fmt.Println(st.Debug().String()) |
| 232 |
t.Fail() |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// TestStreamNoHeaderASCII tests streaming table without header using ASCII symbols. |
| 237 |
func TestStreamNoHeaderASCII(t *testing.T) { |
| 238 |
var buf bytes.Buffer |
| 239 |
st := tablewriter.NewTable(&buf, |
| 240 |
tablewriter.WithConfig(tablewriter.Config{Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}}), |
| 241 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{Symbols: tw.NewSymbols(tw.StyleASCII)})), |
| 242 |
tablewriter.WithDebug(false), |
| 243 |
tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), |
| 244 |
) |
| 245 |
err := st.Start() |
| 246 |
if err != nil { |
| 247 |
t.Fatalf("Start failed: %v", err) |
| 248 |
} |
| 249 |
st.Append([]string{"Regular", "line", "1"}) // Widths: 7+2=9, 4+2=6, 1+2=3 |
| 250 |
st.Append([]string{"Thick", "thick", "2"}) |
| 251 |
|
| 252 |
err = st.Close() |
| 253 |
if err != nil { |
| 254 |
t.Fatalf("End failed: %v", err) |
| 255 |
} |
| 256 |
|
| 257 |
expected := ` |
| 258 |
+-----------+--------+--------+ |
| 259 |
| Regular | line | 1 | |
| 260 |
| Thick | thick | 2 | |
| 261 |
+-----------+--------+--------+ |
| 262 |
` |
| 263 |
if !visualCheck(t, "StreamNoHeaderASCII", buf.String(), expected) { |
| 264 |
fmt.Println(st.Debug().String()) |
| 265 |
t.Fail() |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
func TestBorders(t *testing.T) { |
| 270 |
data := [][]string{{"A", "B"}, {"C", "D"}} |
| 271 |
// widths := map[int]int{0: 3, 1: 3} // Content (1) + padding (1+1) = 3 |
| 272 |
|
| 273 |
tests := []struct { |
| 274 |
name string |
| 275 |
borders tw.Border |
| 276 |
expected string |
| 277 |
}{ |
| 278 |
{ |
| 279 |
name: "All Off", |
| 280 |
borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.Off, Bottom: tw.Off}, |
| 281 |
expected: ` |
| 282 |
A │ B |
| 283 |
C │ D |
| 284 |
`, |
| 285 |
}, |
| 286 |
{ |
| 287 |
name: "No Left/Right", |
| 288 |
borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.On, Bottom: tw.On}, |
| 289 |
expected: ` |
| 290 |
───┬─── |
| 291 |
A │ B |
| 292 |
C │ D |
| 293 |
───┴─── |
| 294 |
`, |
| 295 |
}, |
| 296 |
{ |
| 297 |
name: "No Top/Bottom", |
| 298 |
borders: tw.Border{Left: tw.On, Right: tw.On, Top: tw.Off, Bottom: tw.Off}, |
| 299 |
expected: ` |
| 300 |
│ A │ B │ |
| 301 |
│ C │ D │ |
| 302 |
`, |
| 303 |
}, |
| 304 |
{ |
| 305 |
name: "Only Left", |
| 306 |
borders: tw.Border{Left: tw.On, Right: tw.Off, Top: tw.Off, Bottom: tw.Off}, |
| 307 |
expected: ` |
| 308 |
│ A │ B |
| 309 |
│ C │ D |
| 310 |
`, |
| 311 |
}, |
| 312 |
{ |
| 313 |
name: "Default", |
| 314 |
borders: tw.Border{Left: tw.On, Right: tw.On, Top: tw.On, Bottom: tw.On}, |
| 315 |
expected: ` |
| 316 |
┌───┬───┐ |
| 317 |
│ A │ B │ |
| 318 |
│ C │ D │ |
| 319 |
└───┴───┘ |
| 320 |
`, |
| 321 |
}, |
| 322 |
} |
| 323 |
|
| 324 |
for _, tt := range tests { |
| 325 |
t.Run(tt.name, func(t *testing.T) { |
| 326 |
var buf bytes.Buffer |
| 327 |
|
| 328 |
r := renderer.NewBlueprint( |
| 329 |
tw.Rendition{ |
| 330 |
Borders: tt.borders, |
| 331 |
}, |
| 332 |
) |
| 333 |
st := tablewriter.NewTable(&buf, |
| 334 |
tablewriter.WithConfig(tablewriter.Config{Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}}), |
| 335 |
tablewriter.WithRenderer(r), |
| 336 |
tablewriter.WithDebug(false), |
| 337 |
) |
| 338 |
|
| 339 |
st.Append(data[0]) |
| 340 |
st.Append(data[1]) |
| 341 |
err := st.Render() |
| 342 |
if err != nil { |
| 343 |
t.Fatalf("End failed: %v", err) |
| 344 |
} |
| 345 |
|
| 346 |
if !visualCheck(t, "StreamBorders_"+tt.name, buf.String(), tt.expected) { |
| 347 |
fmt.Printf("--- DEBUG LOG for %s ---\n", tt.name) |
| 348 |
fmt.Println(st.Debug().String()) |
| 349 |
t.Fail() |
| 350 |
} |
| 351 |
}) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
// TestStreamTruncation tests streaming table with long content truncation. |
| 356 |
func TestStreamTruncation(t *testing.T) { |
| 357 |
var buf bytes.Buffer |
| 358 |
st := createStreamTable(t, &buf, |
| 359 |
tablewriter.WithConfig( |
| 360 |
tablewriter.Config{ |
| 361 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 362 |
Row: tw.CellConfig{ |
| 363 |
Alignment: tw.CellAlignment{ |
| 364 |
Global: tw.AlignLeft, |
| 365 |
}, |
| 366 |
Formatting: tw.CellFormatting{AutoWrap: tw.WrapTruncate}, |
| 367 |
ColMaxWidths: tw.CellWidth{Global: 13}, |
| 368 |
}, |
| 369 |
Stream: tw.StreamConfig{ |
| 370 |
Enable: true, |
| 371 |
}, |
| 372 |
Widths: tw.CellWidth{ |
| 373 |
PerColumn: map[int]int{0: 4, 1: 15, 2: 8}, |
| 374 |
}, |
| 375 |
})) |
| 376 |
|
| 377 |
err := st.Start() |
| 378 |
if err != nil { |
| 379 |
t.Fatalf("Start failed: %v", err) |
| 380 |
} |
| 381 |
st.Header([]string{"ID", "Description", "Status"}) // Fits: 2<=4, 11<=15, 6<=8 |
| 382 |
st.Append([]string{"1", "This description is quite long", "OK"}) // Truncates: 1<=4, 30>15 -> "This descript…", 2<=8 |
| 383 |
st.Append([]string{"2", "Short desc", "DONE"}) // Fits: 1<=4, 10<=15, 4<=8 |
| 384 |
st.Append([]string{"3", "Another long one needing truncation", "ERR"}) // Truncates: 1<=4, 35>15 -> "Another long …", 3<=8 |
| 385 |
err = st.Close() |
| 386 |
if err != nil { |
| 387 |
t.Fatalf("End failed: %v", err) |
| 388 |
} |
| 389 |
|
| 390 |
// Widths: [4, 15, 8] |
| 391 |
expected := ` |
| 392 |
┌────┬───────────────┬────────┐ |
| 393 |
│ ID │ DESCRIPTION │ STATUS │ |
| 394 |
├────┼───────────────┼────────┤ |
| 395 |
│ 1 │ This descri… │ OK │ |
| 396 |
│ 2 │ Short desc │ DONE │ |
| 397 |
│ 3 │ Another lon… │ ERR │ |
| 398 |
└────┴───────────────┴────────┘ |
| 399 |
` |
| 400 |
if !visualCheck(t, "StreamTruncation", buf.String(), expected) { |
| 401 |
fmt.Println("--- DEBUG LOG ---") |
| 402 |
fmt.Println(st.Debug().String()) |
| 403 |
t.Fail() |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
// TestStreamCustomPadding tests streaming table with custom padding. |
| 408 |
func TestStreamCustomPadding(t *testing.T) { |
| 409 |
var buf bytes.Buffer |
| 410 |
st := createStreamTable(t, &buf, tablewriter.WithConfig(tablewriter.Config{ |
| 411 |
Header: tw.CellConfig{ |
| 412 |
Padding: tw.CellPadding{Global: tw.Padding{Left: ">>", Right: "<<"}}, |
| 413 |
}, |
| 414 |
Row: tw.CellConfig{ |
| 415 |
Padding: tw.CellPadding{Global: tw.Padding{Left: ">>", Right: "<<"}}, |
| 416 |
}, |
| 417 |
Stream: tw.StreamConfig{ |
| 418 |
Enable: true, |
| 419 |
}, |
| 420 |
Widths: tw.CellWidth{ |
| 421 |
PerColumn: map[int]int{0: 7, 1: 7}, |
| 422 |
}, |
| 423 |
}), |
| 424 |
tablewriter.WithDebug(false)) |
| 425 |
|
| 426 |
err := st.Start() |
| 427 |
if err != nil { |
| 428 |
t.Fatalf("Start failed: %v", err) |
| 429 |
} |
| 430 |
st.Header([]string{"Head1", "Head2"}) // Truncates: 5>3 -> "He…" |
| 431 |
st.Append([]string{"R1C1", "R1C2"}) // Truncates: 4>3 -> "R1…" |
| 432 |
err = st.Close() |
| 433 |
if err != nil { |
| 434 |
t.Fatalf("End failed: %v", err) |
| 435 |
} |
| 436 |
|
| 437 |
expected := ` |
| 438 |
┌───────┬───────┐ |
| 439 |
│>>H…<<<│>>H…<<<│ |
| 440 |
├───────┼───────┤ |
| 441 |
│>>R1C<<│>>R1C<<│ |
| 442 |
└───────┴───────┘ |
| 443 |
` |
| 444 |
if !visualCheck(t, "StreamCustomPadding", buf.String(), expected) { |
| 445 |
fmt.Println("--- DEBUG LOG ---") |
| 446 |
fmt.Println(st.Debug().String()) |
| 447 |
t.Fail() |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
// TestStreamEmptyCells tests streaming table with empty and sparse cells. |
| 452 |
func TestStreamEmptyCells(t *testing.T) { |
| 453 |
var buf bytes.Buffer |
| 454 |
st := createStreamTable(t, &buf, tablewriter.WithConfig(tablewriter.Config{ |
| 455 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 456 |
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, |
| 457 |
Stream: tw.StreamConfig{ |
| 458 |
Enable: true, |
| 459 |
}, |
| 460 |
Widths: tw.CellWidth{ |
| 461 |
Global: 20, |
| 462 |
}, |
| 463 |
}), |
| 464 |
tablewriter.WithDebug(false)) |
| 465 |
|
| 466 |
err := st.Start() |
| 467 |
if err != nil { |
| 468 |
t.Fatalf("Start failed: %v", err) |
| 469 |
} |
| 470 |
st.Header([]string{"H1", "", "H3"}) // Widths: 2+2=4, 0+2=2->3, 2+2=4 |
| 471 |
st.Append([]string{"", "R1C2", ""}) // Needs: 0+2=2, 4+2=6, 0+2=2 |
| 472 |
st.Append([]string{"R2C1", "", "R2C3"}) // Needs: 4+2=6, 0+2=2, 4+2=6 |
| 473 |
st.Append([]string{"", "", ""}) // Needs: 0+2=2, 0+2=2, 0+2=2 |
| 474 |
err = st.Close() |
| 475 |
if err != nil { |
| 476 |
t.Fatalf("End failed: %v", err) |
| 477 |
} |
| 478 |
|
| 479 |
// Max widths: [6, 6, 6] |
| 480 |
expected := ` |
| 481 |
┌──────┬──────┬──────┐ |
| 482 |
│ H 1 │ │ H 3 │ |
| 483 |
├──────┼──────┼──────┤ |
| 484 |
│ │ R1C2 │ │ |
| 485 |
│ R2C1 │ │ R2C3 │ |
| 486 |
│ │ │ │ |
| 487 |
└──────┴──────┴──────┘ |
| 488 |
` |
| 489 |
if !visualCheck(t, "StreamEmptyCells", buf.String(), expected) { |
| 490 |
fmt.Println("--- DEBUG LOG ---") |
| 491 |
fmt.Println(st.Debug().String()) |
| 492 |
t.Fail() |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
// TestStreamOnlyHeader tests streaming table with only a header. |
| 497 |
func TestStreamOnlyHeader(t *testing.T) { |
| 498 |
var buf bytes.Buffer |
| 499 |
st := createStreamTable(t, &buf, tablewriter.WithConfig(tablewriter.Config{ |
| 500 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 501 |
}), |
| 502 |
tablewriter.WithDebug(false), |
| 503 |
tablewriter.WithStreaming(tw.StreamConfig{Enable: true})) |
| 504 |
|
| 505 |
err := st.Start() |
| 506 |
if err != nil { |
| 507 |
t.Fatalf("Start failed: %v", err) |
| 508 |
} |
| 509 |
st.Header([]string{"Header1", "Header2"}) // Widths: 7+2=9, 7+2=9 |
| 510 |
err = st.Close() |
| 511 |
if err != nil { |
| 512 |
t.Fatalf("End failed: %v", err) |
| 513 |
} |
| 514 |
|
| 515 |
expected := ` |
| 516 |
┌───────────┬───────────┐ |
| 517 |
│ HEADER 1 │ HEADER 2 │ |
| 518 |
└───────────┴───────────┘ |
| 519 |
` |
| 520 |
if !visualCheck(t, "StreamOnlyHeader", buf.String(), expected) { |
| 521 |
fmt.Println("--- DEBUG LOG ---") |
| 522 |
fmt.Println(st.Debug().String()) |
| 523 |
t.Fail() |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
// TestStreamOnlyHeaderNoHeaderLine tests streaming table with only a header and no header line. |
| 528 |
func TestStreamOnlyHeaderNoHeaderLine(t *testing.T) { |
| 529 |
var buf bytes.Buffer |
| 530 |
st := createStreamTable(t, &buf, tablewriter.WithConfig(tablewriter.Config{ |
| 531 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 532 |
}), |
| 533 |
tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), |
| 534 |
) |
| 535 |
|
| 536 |
err := st.Start() |
| 537 |
if err != nil { |
| 538 |
t.Fatalf("Start failed: %v", err) |
| 539 |
} |
| 540 |
st.Header([]string{"Header1", "Header2"}) // Fits: 7<=9, 7<=9 |
| 541 |
err = st.Close() |
| 542 |
if err != nil { |
| 543 |
t.Fatalf("End failed: %v", err) |
| 544 |
} |
| 545 |
|
| 546 |
expected := ` |
| 547 |
┌───────────┬───────────┐ |
| 548 |
│ HEADER 1 │ HEADER 2 │ |
| 549 |
└───────────┴───────────┘ |
| 550 |
` |
| 551 |
if !visualCheck(t, "StreamOnlyHeaderNoHeaderLine", buf.String(), expected) { |
| 552 |
fmt.Println("--- DEBUG LOG ---") |
| 553 |
fmt.Println(st.Debug().String()) |
| 554 |
t.Fail() |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
func TestStreamSlowOutput(t *testing.T) { |
| 559 |
var buf bytes.Buffer |
| 560 |
st := createStreamTable(t, &buf, tablewriter.WithConfig(tablewriter.Config{ |
| 561 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 562 |
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, |
| 563 |
}), |
| 564 |
tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), |
| 565 |
) |
| 566 |
|
| 567 |
// Test Start() |
| 568 |
err := st.Start() |
| 569 |
if err != nil { |
| 570 |
t.Fatalf("Start failed: %v", err) |
| 571 |
} |
| 572 |
buf.Reset() |
| 573 |
|
| 574 |
// Test Header() |
| 575 |
time.Sleep(100 * time.Millisecond) |
| 576 |
st.Header([]string{"Event", "Timestamp"}) |
| 577 |
lastLine := getLastContentLine(&buf) |
| 578 |
if !strings.Contains(lastLine, "EVENT") || !strings.Contains(lastLine, "TIMESTAMP") { |
| 579 |
t.Errorf("Header missing expected columns:\nGot: %q", lastLine) |
| 580 |
} |
| 581 |
buf.Reset() |
| 582 |
|
| 583 |
// Test Rows |
| 584 |
for i := 1; i <= 3; i++ { |
| 585 |
time.Sleep(100 * time.Millisecond) |
| 586 |
err = st.Append([]string{fmt.Sprintf("Row %d", i), time.Now().Format("15:04:05.000")}) |
| 587 |
if err != nil { |
| 588 |
t.Fatalf("Row %d failed: %v", i, err) |
| 589 |
} |
| 590 |
|
| 591 |
lastLine = getLastContentLine(&buf) |
| 592 |
if !strings.Contains(lastLine, fmt.Sprintf("Row %d", i)) { |
| 593 |
t.Errorf("Row %d content missing:\nGot: %q", i, lastLine) |
| 594 |
} |
| 595 |
buf.Reset() |
| 596 |
} |
| 597 |
|
| 598 |
err = st.Close() |
| 599 |
if err != nil { |
| 600 |
t.Fatalf("Close failed: %v", err) |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
func TestStreamFormating(t *testing.T) { |
| 605 |
var buf bytes.Buffer |
| 606 |
|
| 607 |
st := createStreamTable(t, &buf, |
| 608 |
tablewriter.WithConfig(tablewriter.Config{ |
| 609 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 610 |
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, |
| 611 |
Footer: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, |
| 612 |
Widths: tw.CellWidth{PerColumn: map[int]int{0: 12, 1: 8, 2: 10}}, |
| 613 |
}), |
| 614 |
|
| 615 |
tablewriter.WithDebug(false), |
| 616 |
tablewriter.WithStreaming(tw.StreamConfig{ |
| 617 |
Enable: true, |
| 618 |
}), |
| 619 |
) |
| 620 |
|
| 621 |
err := st.Start() |
| 622 |
if err != nil { |
| 623 |
t.Fatalf("Start failed: %v", err) |
| 624 |
} |
| 625 |
|
| 626 |
data := [][]any{ |
| 627 |
{Name{"Al i CE", " Ma SK"}, Age(25), "New York"}, |
| 628 |
{Name{"bOb", "mar le y"}, Age(30), "Boston"}, |
| 629 |
} |
| 630 |
|
| 631 |
st.Header([]string{"Name", "Age", "City"}) |
| 632 |
st.Bulk(data) |
| 633 |
|
| 634 |
err = st.Close() |
| 635 |
if err != nil { |
| 636 |
t.Fatalf("End failed: %v", err) |
| 637 |
} |
| 638 |
|
| 639 |
// Widths: Name(5)+2=7, Age(3)+2=5, City(8)+2=10 |
| 640 |
expected := ` |
| 641 |
┌────────────┬────────┬──────────┐ |
| 642 |
│ NAME │ AGE │ CITY │ |
| 643 |
├────────────┼────────┼──────────┤ |
| 644 |
│ Alice Mask │ 25yrs │ New York │ |
| 645 |
│ Bob Marley │ 30yrs │ Boston │ |
| 646 |
└────────────┴────────┴──────────┘ |
| 647 |
` |
| 648 |
if !visualCheck(t, "StreamBasic", buf.String(), expected) { |
| 649 |
fmt.Println(st.Debug()) |
| 650 |
t.Fail() |
| 651 |
} |
| 652 |
} |