| 1 |
package tests // Use _test package to test as a user |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/csv" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/olekukonko/tablewriter" |
| 10 |
"github.com/olekukonko/tablewriter/renderer" // For direct renderer use if needed |
| 11 |
"github.com/olekukonko/tablewriter/tw" |
| 12 |
) |
| 13 |
|
| 14 |
const csvTestData = `Name,Department,Salary |
| 15 |
Alice,Engineering,120000 |
| 16 |
Bob,Marketing,85000 |
| 17 |
Charlie,Engineering,135000 |
| 18 |
Diana,HR,70000 |
| 19 |
` |
| 20 |
|
| 21 |
func getCSVReaderFromString(data string) *csv.Reader { |
| 22 |
stringReader := strings.NewReader(data) |
| 23 |
return csv.NewReader(stringReader) |
| 24 |
} |
| 25 |
|
| 26 |
func TestTable_Configure_Basic(t *testing.T) { |
| 27 |
var buf bytes.Buffer |
| 28 |
csvReader := getCSVReaderFromString(csvTestData) |
| 29 |
|
| 30 |
table, err := tablewriter.NewCSVReader(&buf, csvReader, true) |
| 31 |
if err != nil { |
| 32 |
t.Fatalf("NewCSVReader failed: %v", err) |
| 33 |
} |
| 34 |
|
| 35 |
// Check initial default config values (examples) |
| 36 |
if table.Config().Header.Alignment.Global != tw.AlignCenter { |
| 37 |
t.Errorf("Expected initial header alignment to be Center, got %s", table.Config().Header.Alignment.Global) |
| 38 |
} |
| 39 |
if table.Config().Behavior.TrimSpace != tw.On { // Default from defaultConfig() |
| 40 |
t.Errorf("Expected initial TrimSpace to be On, got %s", table.Config().Behavior.TrimSpace) |
| 41 |
} |
| 42 |
|
| 43 |
table.Configure(func(cfg *tablewriter.Config) { |
| 44 |
cfg.Header.Alignment.Global = tw.AlignLeft |
| 45 |
cfg.Row.Alignment.Global = tw.AlignRight |
| 46 |
cfg.Behavior.TrimSpace = tw.Off |
| 47 |
cfg.Debug = true // This should enable the logger |
| 48 |
}) |
| 49 |
|
| 50 |
// Check that Table.config was updated |
| 51 |
if table.Config().Header.Alignment.Global != tw.AlignLeft { |
| 52 |
t.Errorf("Expected configured header alignment to be Left, got %s", table.Config().Header.Alignment.Global) |
| 53 |
} |
| 54 |
if table.Config().Row.Alignment.Global != tw.AlignRight { |
| 55 |
t.Errorf("Expected configured row alignment to be Right, got %s", table.Config().Row.Alignment.Global) |
| 56 |
} |
| 57 |
if table.Config().Behavior.TrimSpace != tw.Off { |
| 58 |
t.Errorf("Expected configured TrimSpace to be Off, got %s", table.Config().Behavior.TrimSpace) |
| 59 |
} |
| 60 |
if !table.Config().Debug { |
| 61 |
t.Errorf("Expected configured Debug to be true") |
| 62 |
} |
| 63 |
|
| 64 |
// Render and check output (visual check will confirm alignment and trimming) |
| 65 |
err = table.Render() |
| 66 |
if err != nil { |
| 67 |
t.Fatalf("Render failed: %v", err) |
| 68 |
} |
| 69 |
|
| 70 |
// What visualCheck will see (assuming Blueprint respects CellContext.Align passed from table.config): |
| 71 |
expectedAfterConfigure := ` |
| 72 |
┌─────────┬─────────────┬────────┐ |
| 73 |
│ NAME │ DEPARTMENT │ SALARY │ |
| 74 |
├─────────┼─────────────┼────────┤ |
| 75 |
│ Alice │ Engineering │ 120000 │ |
| 76 |
│ Bob │ Marketing │ 85000 │ |
| 77 |
│ Charlie │ Engineering │ 135000 │ |
| 78 |
│ Diana │ HR │ 70000 │ |
| 79 |
└─────────┴─────────────┴────────┘ |
| 80 |
` |
| 81 |
if !visualCheck(t, "TestTable_Configure_Basic", buf.String(), expectedAfterConfigure) { |
| 82 |
t.Logf("Debug trace from table:\n%s", table.Debug().String()) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func TestTable_Options_WithRendition_Borderless(t *testing.T) { |
| 87 |
var buf bytes.Buffer |
| 88 |
csvReader := getCSVReaderFromString(csvTestData) // Ensure csvTestData is defined |
| 89 |
|
| 90 |
table, err := tablewriter.NewCSVReader(&buf, csvReader, true, tablewriter.WithDebug(true)) |
| 91 |
if err != nil { |
| 92 |
t.Fatalf("NewCSVReader failed: %v", err) |
| 93 |
} |
| 94 |
|
| 95 |
// Initially, it should have default borders |
| 96 |
table.Render() |
| 97 |
initialOutputWithBorders := buf.String() |
| 98 |
buf.Reset() // Clear buffer for next render |
| 99 |
|
| 100 |
if !strings.Contains(initialOutputWithBorders, "┌───") { // Basic check for default border |
| 101 |
t.Errorf("Expected initial render to have borders, but got:\n%s", initialOutputWithBorders) |
| 102 |
} |
| 103 |
|
| 104 |
// Define a TRULY borderless and line-less rendition |
| 105 |
borderlessRendition := tw.Rendition{ |
| 106 |
Borders: tw.Border{ // Explicitly set all borders to Off |
| 107 |
Left: tw.Off, |
| 108 |
Right: tw.Off, |
| 109 |
Top: tw.Off, |
| 110 |
Bottom: tw.Off, |
| 111 |
}, |
| 112 |
// Using StyleNone for symbols means no visible characters for borders/lines if they were on. |
| 113 |
// For a "markdown-like but no lines" look, you might use StyleMarkdown and then turn off lines/separators. |
| 114 |
// For true "no visual structure", StyleNone is good. |
| 115 |
Symbols: tw.NewSymbols(tw.StyleNone), |
| 116 |
Settings: tw.Settings{ |
| 117 |
Lines: tw.Lines{ // Explicitly set all line drawing to Off |
| 118 |
ShowTop: tw.Off, |
| 119 |
ShowBottom: tw.Off, |
| 120 |
ShowHeaderLine: tw.Off, |
| 121 |
ShowFooterLine: tw.Off, |
| 122 |
}, |
| 123 |
Separators: tw.Separators{ // Explicitly set all separators to Off |
| 124 |
ShowHeader: tw.Off, |
| 125 |
ShowFooter: tw.Off, |
| 126 |
BetweenRows: tw.Off, |
| 127 |
BetweenColumns: tw.Off, |
| 128 |
}, |
| 129 |
}, |
| 130 |
} |
| 131 |
|
| 132 |
table.Options( |
| 133 |
tablewriter.WithRendition(borderlessRendition), |
| 134 |
) |
| 135 |
|
| 136 |
// Render again |
| 137 |
err = table.Render() |
| 138 |
if err != nil { |
| 139 |
t.Fatalf("Render after WithRendition failed: %v", err) |
| 140 |
} |
| 141 |
|
| 142 |
// Expected output: Plain text, no borders, no lines, no separators. |
| 143 |
// Content alignment will be default (Header:Center, Row:Left) because |
| 144 |
// Table.config was not modified for alignments in this test. |
| 145 |
expectedOutputBorderless := ` |
| 146 |
NAME DEPARTMENT SALARY |
| 147 |
Alice Engineering 120000 |
| 148 |
Bob Marketing 85000 |
| 149 |
Charlie Engineering 135000 |
| 150 |
Diana HR 70000 |
| 151 |
` |
| 152 |
if !visualCheck(t, "TestTable_Options_WithRendition_Borderless", buf.String(), expectedOutputBorderless) { |
| 153 |
t.Logf("Initial output with borders was:\n%s", initialOutputWithBorders) // For context |
| 154 |
t.Logf("Debug trace from table after borderless rendition:\n%s", table.Debug().String()) |
| 155 |
} |
| 156 |
|
| 157 |
// Verify renderer's internal config was changed |
| 158 |
if bp, ok := table.Renderer().(*renderer.Blueprint); ok { |
| 159 |
currentRendererCfg := bp.Config() |
| 160 |
|
| 161 |
if currentRendererCfg.Borders.Left.Enabled() { |
| 162 |
t.Errorf("Blueprint Borders.Left should be OFF, but is ON") |
| 163 |
} |
| 164 |
if currentRendererCfg.Borders.Right.Enabled() { |
| 165 |
t.Errorf("Blueprint Borders.Right should be OFF, but is ON") |
| 166 |
} |
| 167 |
if currentRendererCfg.Borders.Top.Enabled() { |
| 168 |
t.Errorf("Blueprint Borders.Top should be OFF, but is ON") |
| 169 |
} |
| 170 |
if currentRendererCfg.Borders.Bottom.Enabled() { |
| 171 |
t.Errorf("Blueprint Borders.Bottom should be OFF, but is ON") |
| 172 |
} |
| 173 |
|
| 174 |
if currentRendererCfg.Settings.Lines.ShowHeaderLine.Enabled() { |
| 175 |
t.Errorf("Blueprint Settings.Lines.ShowHeaderLine should be OFF, but is ON") |
| 176 |
} |
| 177 |
if currentRendererCfg.Settings.Lines.ShowTop.Enabled() { |
| 178 |
t.Errorf("Blueprint Settings.Lines.ShowTop should be OFF, but is ON") |
| 179 |
} |
| 180 |
if currentRendererCfg.Settings.Separators.BetweenColumns.Enabled() { |
| 181 |
t.Errorf("Blueprint Settings.Separators.BetweenColumns should be OFF, but is ON") |
| 182 |
} |
| 183 |
|
| 184 |
// Check symbols if relevant (StyleNone should have empty symbols) |
| 185 |
if currentRendererCfg.Symbols.Column() != "" { |
| 186 |
t.Errorf("Blueprint Symbols.Column should be empty for StyleNone, got '%s'", currentRendererCfg.Symbols.Column()) |
| 187 |
} |
| 188 |
} else { |
| 189 |
t.Logf("Renderer is not *renderer.Blueprint, skipping detailed internal config check. Type is %T", table.Renderer()) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// Assume csvTestData and getCSVReaderFromString are defined as in previous examples: |
| 194 |
const csvTestDataForPartial = `Name,Department,Salary |
| 195 |
Alice,Engineering,120000 |
| 196 |
Bob,Marketing,85000 |
| 197 |
` |
| 198 |
|
| 199 |
func getCSVReaderFromStringForPartial(data string) *csv.Reader { |
| 200 |
stringReader := strings.NewReader(data) |
| 201 |
return csv.NewReader(stringReader) |
| 202 |
} |
| 203 |
|
| 204 |
// Assume csvTestDataForPartial and getCSVReaderFromStringForPartial are defined |
| 205 |
const csvTestDataForPartialUpdate = `Name,Department,Salary |
| 206 |
Alice,Engineering,120000 |
| 207 |
Bob,Marketing,85000 |
| 208 |
` |
| 209 |
|
| 210 |
func getCSVReaderFromStringForPartialUpdate(data string) *csv.Reader { |
| 211 |
stringReader := strings.NewReader(data) |
| 212 |
return csv.NewReader(stringReader) |
| 213 |
} |
| 214 |
|
| 215 |
func TestTable_Options_WithRendition_PartialUpdate(t *testing.T) { |
| 216 |
var buf bytes.Buffer |
| 217 |
csvReader := getCSVReaderFromStringForPartialUpdate(csvTestDataForPartialUpdate) |
| 218 |
|
| 219 |
// Define an explicitly borderless and line-less initial rendition |
| 220 |
initiallyAllOffRendition := tw.Rendition{ |
| 221 |
Borders: tw.Border{ |
| 222 |
Left: tw.Off, |
| 223 |
Right: tw.Off, |
| 224 |
Top: tw.Off, |
| 225 |
Bottom: tw.Off, |
| 226 |
}, |
| 227 |
Symbols: tw.NewSymbols(tw.StyleNone), // StyleNone should render no visible symbols |
| 228 |
Settings: tw.Settings{ |
| 229 |
Lines: tw.Lines{ |
| 230 |
ShowTop: tw.Off, |
| 231 |
ShowBottom: tw.Off, |
| 232 |
ShowHeaderLine: tw.Off, |
| 233 |
ShowFooterLine: tw.Off, |
| 234 |
}, |
| 235 |
Separators: tw.Separators{ |
| 236 |
ShowHeader: tw.Off, |
| 237 |
ShowFooter: tw.Off, |
| 238 |
BetweenRows: tw.Off, |
| 239 |
BetweenColumns: tw.Off, |
| 240 |
}, |
| 241 |
}, |
| 242 |
} |
| 243 |
|
| 244 |
// Create table with this explicitly "all off" rendition |
| 245 |
table, err := tablewriter.NewCSVReader(&buf, csvReader, true, |
| 246 |
tablewriter.WithDebug(true), |
| 247 |
tablewriter.WithRenderer(renderer.NewBlueprint(initiallyAllOffRendition)), |
| 248 |
) |
| 249 |
if err != nil { |
| 250 |
t.Fatalf("NewCSVReader with initial 'all off' rendition failed: %v", err) |
| 251 |
} |
| 252 |
|
| 253 |
// Render to confirm initial state (should be very plain) |
| 254 |
table.Render() |
| 255 |
outputAfterInitialAllOff := buf.String() |
| 256 |
buf.Reset() // Clear buffer for the next render |
| 257 |
|
| 258 |
// Check the initial plain output (content only, no borders/lines) |
| 259 |
expectedInitialPlainOutput := ` |
| 260 |
NAME DEPARTMENT SALARY |
| 261 |
Alice Engineering 120000 |
| 262 |
Bob Marketing 85000 |
| 263 |
` |
| 264 |
if !visualCheck(t, "TestTable_Options_WithRendition_PartialUpdate_InitialState", outputAfterInitialAllOff, expectedInitialPlainOutput) { |
| 265 |
t.Errorf("Initial render was not plain as expected.") |
| 266 |
t.Logf("Initial 'all off' output was:\n%s", outputAfterInitialAllOff) |
| 267 |
} |
| 268 |
|
| 269 |
partialRenditionUpdate := tw.Rendition{ |
| 270 |
Borders: tw.Border{Top: tw.On, Bottom: tw.On}, // Left/Right are 0 (unspecified in this struct literal) |
| 271 |
Symbols: tw.NewSymbols(tw.StyleHeavy), |
| 272 |
Settings: tw.Settings{ |
| 273 |
Lines: tw.Lines{ShowTop: tw.On, ShowBottom: tw.On}, // Enable drawing of these lines |
| 274 |
// Separators are zero-value, so they will remain Off from 'initiallyAllOffRendition' |
| 275 |
}, |
| 276 |
} |
| 277 |
|
| 278 |
// Apply the partial update using Options |
| 279 |
table.Options( |
| 280 |
tablewriter.WithRendition(partialRenditionUpdate), |
| 281 |
) |
| 282 |
|
| 283 |
// Render again |
| 284 |
err = table.Render() |
| 285 |
if err != nil { |
| 286 |
t.Fatalf("Render after partial WithRendition failed: %v", err) |
| 287 |
} |
| 288 |
outputAfterPartialUpdate := buf.String() |
| 289 |
|
| 290 |
expectedOutputPartialBorders := ` |
| 291 |
━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 292 |
NAME DEPARTMENT SALARY |
| 293 |
Alice Engineering 120000 |
| 294 |
Bob Marketing 85000 |
| 295 |
━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 296 |
` |
| 297 |
|
| 298 |
if !visualCheck(t, "TestTable_Options_WithRendition_PartialUpdate_FinalState", outputAfterPartialUpdate, expectedOutputPartialBorders) { |
| 299 |
t.Logf("Initial 'all off' output was:\n%s", outputAfterInitialAllOff) // For context |
| 300 |
t.Logf("Debug trace from table after partial update:\n%s", table.Debug().String()) |
| 301 |
} |
| 302 |
|
| 303 |
// Verify the renderer's internal configuration reflects the partial update correctly. |
| 304 |
if bp, ok := table.Renderer().(*renderer.Blueprint); ok { |
| 305 |
currentRendererCfg := bp.Config() |
| 306 |
|
| 307 |
if !currentRendererCfg.Borders.Top.Enabled() { |
| 308 |
t.Errorf("Blueprint Borders.Top should be ON, but is OFF") |
| 309 |
} |
| 310 |
if !currentRendererCfg.Borders.Bottom.Enabled() { |
| 311 |
t.Errorf("Blueprint Borders.Bottom should be ON, but is OFF") |
| 312 |
} |
| 313 |
if currentRendererCfg.Borders.Left.Enabled() { |
| 314 |
t.Errorf("Blueprint Borders.Left should remain OFF, but is ON") |
| 315 |
} |
| 316 |
if currentRendererCfg.Borders.Right.Enabled() { |
| 317 |
t.Errorf("Blueprint Borders.Right should remain OFF, but is ON") |
| 318 |
} |
| 319 |
|
| 320 |
if currentRendererCfg.Symbols.Row() != "━" { // From StyleHeavy |
| 321 |
t.Errorf("Blueprint Symbols.Row is not '━' (Heavy), got '%s'", currentRendererCfg.Symbols.Row()) |
| 322 |
} |
| 323 |
// Column symbol check might be less relevant if BetweenColumns is Off, but good for completeness. |
| 324 |
if currentRendererCfg.Symbols.Column() != "┃" { // From StyleHeavy |
| 325 |
t.Errorf("Blueprint Symbols.Column is not '┃' (Heavy), got '%s'", currentRendererCfg.Symbols.Column()) |
| 326 |
} |
| 327 |
|
| 328 |
// Check Settings.Lines |
| 329 |
if !currentRendererCfg.Settings.Lines.ShowTop.Enabled() { |
| 330 |
t.Errorf("Blueprint Settings.Lines.ShowTop should be ON, but is OFF") |
| 331 |
} |
| 332 |
if !currentRendererCfg.Settings.Lines.ShowBottom.Enabled() { |
| 333 |
t.Errorf("Blueprint Settings.Lines.ShowBottom should be ON, but is OFF") |
| 334 |
} |
| 335 |
if currentRendererCfg.Settings.Lines.ShowHeaderLine.Enabled() { |
| 336 |
t.Errorf("Blueprint Settings.Lines.ShowHeaderLine should remain OFF, but is ON") |
| 337 |
} |
| 338 |
|
| 339 |
// Check Settings.Separators |
| 340 |
if currentRendererCfg.Settings.Separators.BetweenColumns.Enabled() { |
| 341 |
t.Errorf("Blueprint Settings.Separators.BetweenColumns should remain OFF, but is ON") |
| 342 |
} |
| 343 |
} else { |
| 344 |
t.Logf("Renderer is not *renderer.Blueprint, skipping detailed internal config check. Type is %T", table.Renderer()) |
| 345 |
} |
| 346 |
} |