1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
package tests
import (
"bytes"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/renderer"
"github.com/olekukonko/tablewriter/tw"
"testing"
)
func TestBug260(t *testing.T) {
var buf bytes.Buffer
tableRendition := tw.Rendition{
Borders: tw.BorderNone,
Settings: tw.Settings{
Separators: tw.Separators{
ShowHeader: tw.Off,
ShowFooter: tw.Off,
BetweenRows: tw.Off,
BetweenColumns: tw.Off,
},
},
Symbols: tw.NewSymbols(tw.StyleNone),
}
t.Run("Normal", func(t *testing.T) {
buf.Reset()
tableRenderer := renderer.NewBlueprint(tableRendition)
table := tablewriter.NewTable(
&buf,
tablewriter.WithRenderer(tableRenderer),
tablewriter.WithTableMax(120),
tablewriter.WithTrimSpace(tw.Off),
tablewriter.WithDebug(true),
tablewriter.WithPadding(tw.PaddingNone),
)
table.Append([]string{"INFO:",
"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.",
})
table.Append("INFO:",
"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.",
)
table.Render()
expected := `
INFO: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.
INFO: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.
`
debug := visualCheck(t, "TestBug252-Mixed", buf.String(), expected)
if !debug {
t.Error(table.Debug())
}
})
t.Run("Mixed", func(t *testing.T) {
buf.Reset()
tableRenderer := renderer.NewBlueprint(tableRendition)
table := tablewriter.NewTable(
&buf,
tablewriter.WithRenderer(tableRenderer),
tablewriter.WithTableMax(120),
tablewriter.WithTrimSpace(tw.Off),
tablewriter.WithDebug(true),
tablewriter.WithPadding(tw.PaddingNone),
)
table.Append([]string{"INFO:",
"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.",
})
table.Append("INFO: ",
"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.",
)
table.Render()
expected := `
INFO: 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.
INFO: 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.
`
debug := visualCheck(t, "TestBug252-Mixed", buf.String(), expected)
if !debug {
t.Error(table.Debug())
}
})
}
func TestBatchPerColumnWidths(t *testing.T) {
var buf bytes.Buffer
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
Widths: tw.CellWidth{
PerColumn: tw.NewMapper[int, int]().Set(0, 8).Set(1, 10).Set(2, 15), // Total widths: 8, 5, 15
},
Row: tw.CellConfig{
Formatting: tw.CellFormatting{
AutoWrap: tw.WrapTruncate, // Truncate content to fit
},
},
}), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
Settings: tw.Settings{
Separators: tw.Separators{
BetweenColumns: tw.On, // Separator width = 1
},
},
})))
table.Header([]string{"Name", "Age", "City"})
table.Append([]string{"Alice Smith", "25", "New York City"})
table.Append([]string{"Bob Johnson", "30", "Boston"})
table.Render()
// Expected widths:
// Col 0: 8 (content=6, pad=1+1, sep=1 for next column)
// Col 1: 5 (content=3, pad=1+1, sep=1 for next column)
// Col 2: 15 (content=13, pad=1+1, no sep at end)
expected := `
┌────────┬──────────┬───────────────┐
│ NAME │ AGE │ CITY │
├────────┼──────────┼───────────────┤
│ Alic… │ 25 │ New York City │
│ Bob … │ 30 │ Boston │
└────────┴──────────┴───────────────┘
`
if !visualCheck(t, "BatchPerColumnWidths", buf.String(), expected) {
t.Error(table.Debug())
}
}
func TestBatchGlobalWidthScaling(t *testing.T) {
var buf bytes.Buffer
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
Widths: tw.CellWidth{
Global: 20, // Total table width, including padding and separators
},
Row: tw.CellConfig{
Formatting: tw.CellFormatting{
AutoWrap: tw.WrapNormal,
},
},
}), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
Settings: tw.Settings{
Separators: tw.Separators{
BetweenColumns: tw.On, // Separator width = 1
},
},
})))
table.Header([]string{"Name", "Age", "City"})
table.Append([]string{"Alice Smith", "25", "New York City"})
table.Append([]string{"Bob Johnson", "30", "Boston"})
table.Render()
// Expected widths:
// Total width = 20, with 2 separators (2x1 = 2)
// Available for columns = 20 - 2 = 18
// 3 columns, so each ~6 (18/3), adjusted for padding and separators
// Col 0: 6 (content=4, pad=1+1, sep=1)
// Col 1: 6 (content=4, pad=1+1, sep=1)
// Col 2: 6 (content=4, pad=1+1)
expected := `
┌──────┬─────┬───────┐
│ NAME │ AGE │ CITY │
├──────┼─────┼───────┤
│ Alic │ 25 │ New Y │
│ Bob │ 30 │ Bosto │
└──────┴─────┴───────┘
`
if !visualCheck(t, "BatchGlobalWidthScaling", buf.String(), expected) {
t.Error(table.Debug())
}
}
func TestBatchWidthsWithHorizontalMerge(t *testing.T) {
var buf bytes.Buffer
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
Widths: tw.CellWidth{
PerColumn: tw.NewMapper[int, int]().Set(0, 10).Set(1, 8).Set(2, 8), // Total widths: 10, 8, 8
},
Row: tw.CellConfig{
Formatting: tw.CellFormatting{
MergeMode: tw.MergeHorizontal,
AutoWrap: tw.WrapTruncate,
},
},
}), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
Settings: tw.Settings{
Separators: tw.Separators{
BetweenColumns: tw.On, // Separator width = 1
BetweenRows: tw.On,
},
},
})))
table.Header([]string{"Name", "Status", "Status"})
table.Append([]string{"Alice", "Active", "Active"}) // Should merge Status columns
table.Append([]string{"Bob", "Inactive", "Pending"}) // No merge
table.Render()
// Expected widths:
// Col 0: 10 (content=8, pad=1+1, sep=1)
// Col 1: 8 (content=6, pad=1+1, sep=1)
// Col 2: 8 (content=6, pad=1+1)
// Merged Col 1+2: 8 + 8 - 1 (no separator between) = 15 (content=13, pad=1+1)
expected := `
┌──────────┬────────┬────────┐
│ NAME │ STATUS │ STATUS │
├──────────┼────────┴────────┤
│ Alice │ Active │
├──────────┼────────┬────────┤
│ Bob │ Inac… │ Pend… │
└──────────┴────────┴────────┘
`
if !visualCheck(t, "BatchWidthsWithHorizontalMerge", buf.String(), expected) {
t.Error(table.Debug())
}
}
func TestWrapBreakWithConstrainedWidthsNoRightPadding(t *testing.T) {
var buf bytes.Buffer
table := tablewriter.NewTable(&buf,
tablewriter.WithTrimSpace(tw.Off),
tablewriter.WithHeaderAutoFormat(tw.Off),
tablewriter.WithConfig(tablewriter.Config{
Widths: tw.CellWidth{
PerColumn: tw.NewMapper[int, int]().Set(0, 4).Set(1, 4).Set(2, 6).Set(3, 7),
},
Row: tw.CellConfig{
Formatting: tw.CellFormatting{
AutoWrap: tw.WrapBreak,
},
Padding: tw.CellPadding{
Global: tw.PaddingNone,
},
},
}),
)
headers := []string{"a", "b", "c", "d"}
table.Header(headers)
data := [][]string{
{"aa", "bb", "cc", "dd"},
{"aaa", "bbb", "ccc", "ddd"},
{"aaaa", "bbbb", "cccc", "dddd"},
{"aaaaa", "bbbbb", "ccccc", "ddddd"},
}
table.Bulk(data)
table.Render()
expected := `
┌────┬────┬──────┬───────┐
│ A │ B │ C │ D │
├────┼────┼──────┼───────┤
│aa │bb │cc │dd │
│aaa │bbb │ccc │ddd │
│aaaa│bbbb│cccc │dddd │
│aaa↩│bbb↩│ccccc │ddddd │
│aa │bb │ │ │
└────┴────┴──────┴───────┘
`
if !visualCheck(t, "WrapBreakWithConstrainedWidthsNoRightPadding", buf.String(), expected) {
t.Error(table.Debug())
}
}
func TestCompatMode(t *testing.T) {
var buf bytes.Buffer
table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
Header: tw.CellConfig{Formatting: tw.CellFormatting{MergeMode: tw.MergeHorizontal}},
Behavior: tw.Behavior{Compact: tw.Compact{Merge: tw.On}},
Debug: true,
}))
x := "This is a long header that makes the table look too wide"
table.Header([]string{x, x})
table.Append([]string{"Key", "Value"})
table.Render()
expected := `
┌──────────────────────────────────────────────────────────┐
│ THIS IS A LONG HEADER THAT MAKES THE TABLE LOOK TOO WIDE │
├────────────────────────────┬─────────────────────────────┤
│ Key │ Value │
└────────────────────────────┴─────────────────────────────┘
`
if !visualCheck(t, "TestCompatMode", buf.String(), expected) {
t.Error(table.Debug())
}
}
|