Skip to content

Commit 7b191c5

Browse files
bashbunniandreynering
authored andcommitted
fix(test): make table wrapping tests use golden files
1 parent 9cfb7dd commit 7b191c5

14 files changed

+463
-48
lines changed

โ€Žtable/resizing.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,13 @@ func (r *resizer) expandTableWidth() (colWidths, rowHeights []int) {
186186
shorterColumnIndex := 0
187187
shorterColumnWidth := math.MaxInt32
188188

189-
for i, width := range colWidths {
189+
for j, width := range colWidths {
190+
if width == r.columns[j].fixedWidth {
191+
continue
192+
}
190193
if width < shorterColumnWidth {
191194
shorterColumnWidth = width
192-
shorterColumnIndex = i
195+
shorterColumnIndex = j
193196
}
194197
}
195198

@@ -216,6 +219,9 @@ func (r *resizer) shrinkTableWidth() (colWidths, rowHeights []int) {
216219
bigColumnWidth := -math.MaxInt32
217220

218221
for j, width := range colWidths {
222+
if width == r.columns[j].fixedWidth {
223+
continue
224+
}
219225
if veryBigOnly {
220226
if width >= (r.tableWidth/2) && width > bigColumnWidth { //nolint:mnd
221227
bigColumnWidth = width
@@ -248,6 +254,9 @@ func (r *resizer) shrinkTableWidth() (colWidths, rowHeights []int) {
248254
biggestDiffToMedianIndex := -math.MaxInt32
249255

250256
for j, width := range colWidths {
257+
if width == r.columns[j].fixedWidth {
258+
continue
259+
}
251260
diffToMedian := width - r.columns[j].median
252261
if diffToMedian > 0 && diffToMedian > biggestDiffToMedian {
253262
biggestDiffToMedian = diffToMedian

โ€Žtable/table_test.go

Lines changed: 80 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,17 +1334,17 @@ func TestContentWrapping_WithPadding(t *testing.T) {
13341334
data [][]string
13351335
}{
13361336
{
1337-
"long row content",
1337+
"LongRowContent",
13381338
[]string{"Name", "Description", "Type", "Required", "Default"},
13391339
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
13401340
},
13411341
{
1342-
"missing row content",
1342+
"MissingRowContent",
13431343
[]string{"Name", "Description", "Type", "Required", "Default"},
13441344
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "", ""}},
13451345
},
13461346
{
1347-
"long header content, long and short rows",
1347+
"LongHeaderContentLongAndShortRows",
13481348
[]string{"Destination", "Why are you going on this trip? Is it a hot or cold climate?", "Affordability"},
13491349
[][]string{
13501350
{"Mexico", "I want to go somewhere hot, dry, and affordable. Mexico has really good food, just don't drink tap water!", "$"},
@@ -1353,7 +1353,7 @@ func TestContentWrapping_WithPadding(t *testing.T) {
13531353
},
13541354
},
13551355
{
1356-
"Long text, different languages",
1356+
"LongTextDifferentLanguages",
13571357
[]string{"Hello", "ไฝ ๅฅฝ", "ู…ุฑุญุจู‹ุง", "์•ˆ๋…•ํ•˜์„ธ์š”"},
13581358
[][]string{
13591359
{
@@ -1368,17 +1368,24 @@ func TestContentWrapping_WithPadding(t *testing.T) {
13681368
},
13691369
}
13701370

1371+
defaultWidth := 80
13711372
for _, tc := range tests {
1372-
table := New().
1373-
Headers(tc.headers...).
1374-
Rows(tc.data...).
1375-
StyleFunc(func(_, col int) lipgloss.Style {
1376-
return lipgloss.NewStyle().Padding(0, 1)
1377-
})
1378-
table.Width(80)
1379-
1380-
t.Log(lipgloss.Width(table.String()))
1381-
t.Log("\n" + table.String() + "\n")
1373+
t.Run(tc.name, func(t *testing.T) {
1374+
table := New().
1375+
Headers(tc.headers...).
1376+
Rows(tc.data...).
1377+
StyleFunc(func(_, col int) lipgloss.Style {
1378+
return lipgloss.NewStyle().Padding(0, 1)
1379+
})
1380+
table.Width(defaultWidth)
1381+
1382+
// check total width.
1383+
if got := lipgloss.Width(table.String()); got != defaultWidth {
1384+
t.Fatalf("Table is not the correct width. got %d, want %d", got, defaultWidth)
1385+
t.Log(table.String())
1386+
}
1387+
golden.RequireEqual(t, []byte(table.String()))
1388+
})
13821389
}
13831390
}
13841391

@@ -1389,17 +1396,17 @@ func TestContentWrapping_WithMargins(t *testing.T) {
13891396
data [][]string
13901397
}{
13911398
{
1392-
"long row content",
1399+
"LongRowContent",
13931400
[]string{"Name", "Description", "Type", "Required", "Default"},
13941401
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
13951402
},
13961403
{
1397-
"missing row content",
1404+
"MissingRowContent",
13981405
[]string{"Name", "Description", "Type", "Required", "Default"},
13991406
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "", ""}},
14001407
},
14011408
{
1402-
"long header content, long and short rows",
1409+
"LongHeaderContentLongAndShortRows",
14031410
[]string{"Destination", "Why are you going on this trip? Is it a hot or cold climate?", "Affordability"},
14041411
[][]string{
14051412
{"Mexico", "I want to go somewhere hot, dry, and affordable. Mexico has really good food, just don't drink tap water!", "$"},
@@ -1408,7 +1415,7 @@ func TestContentWrapping_WithMargins(t *testing.T) {
14081415
},
14091416
},
14101417
{
1411-
"Long text, different languages",
1418+
"LongTextDifferentLanguages",
14121419
[]string{"Hello", "ไฝ ๅฅฝ", "ู…ุฑุญุจู‹ุง", "์•ˆ๋…•ํ•˜์„ธ์š”"},
14131420
[][]string{
14141421
{
@@ -1424,16 +1431,16 @@ func TestContentWrapping_WithMargins(t *testing.T) {
14241431
}
14251432

14261433
for _, tc := range tests {
1427-
table := New().
1428-
Headers(tc.headers...).
1429-
Rows(tc.data...).
1430-
StyleFunc(func(row, col int) lipgloss.Style {
1431-
return lipgloss.NewStyle().Margin(0, 4)
1432-
})
1433-
table.Width(80)
1434-
1435-
t.Log(lipgloss.Width(table.String()))
1436-
t.Log("\n" + table.String() + "\n")
1434+
t.Run(tc.name, func(t *testing.T) {
1435+
table := New().
1436+
Headers(tc.headers...).
1437+
Rows(tc.data...).
1438+
StyleFunc(func(row, col int) lipgloss.Style {
1439+
return lipgloss.NewStyle().Margin(0, 4)
1440+
})
1441+
table.Width(80)
1442+
golden.RequireEqual(t, []byte(table.String()))
1443+
})
14371444
}
14381445
}
14391446

@@ -1444,17 +1451,17 @@ func TestContentWrapping_ColumnWidth(t *testing.T) {
14441451
data [][]string
14451452
}{
14461453
{
1447-
"long row content",
1454+
"LongRowContent",
14481455
[]string{"Name", "Description", "Type", "Required", "Default"},
14491456
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "hello", "yep"}},
14501457
},
14511458
{
1452-
"missing row content",
1459+
"MissingRowContent",
14531460
[]string{"Name", "Description", "Type", "Required", "Default"},
14541461
[][]string{{"command", "A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.", "yes", "", ""}},
14551462
},
14561463
{
1457-
"long header content, long and short rows",
1464+
"LongHeaderContentLongAndShortRows",
14581465
[]string{"Destination", "Why are you going on this trip? Is it a hot or cold climate?", "Affordability"},
14591466
[][]string{
14601467
{"Mexico", "I want to go somewhere hot, dry, and affordable. Mexico has really good food, just don't drink tap water!", "$"},
@@ -1463,7 +1470,7 @@ func TestContentWrapping_ColumnWidth(t *testing.T) {
14631470
},
14641471
},
14651472
{
1466-
"Long text, different languages",
1473+
"LongTextDifferentLanguages",
14671474
[]string{"Hello", "ไฝ ๅฅฝ", "ู…ุฑุญุจู‹ุง", "์•ˆ๋…•ํ•˜์„ธ์š”"},
14681475
[][]string{
14691476
{
@@ -1477,25 +1484,52 @@ func TestContentWrapping_ColumnWidth(t *testing.T) {
14771484
},
14781485
},
14791486
}
1487+
defaultWidth := 80
14801488
for _, tc := range tests {
1481-
table := New().
1482-
Headers(tc.headers...).
1483-
Rows(tc.data...).
1484-
StyleFunc(func(row, col int) lipgloss.Style {
1485-
if row == 0 && col == 1 {
1486-
return lipgloss.NewStyle().Width(15)
1487-
}
1488-
return lipgloss.NewStyle()
1489-
})
1490-
table.Width(80)
1491-
t.Log(lipgloss.Width(table.String()))
1492-
t.Log("\n" + table.String() + "\n")
1489+
t.Run(tc.name, func(t *testing.T) {
1490+
table := New().
1491+
Headers(tc.headers...).
1492+
Rows(tc.data...).
1493+
StyleFunc(func(row, col int) lipgloss.Style {
1494+
// If we set a specific cell width, it should count for all rows
1495+
// in that column.
1496+
if row == 0 && col == 1 {
1497+
return lipgloss.NewStyle().Width(30)
1498+
}
1499+
// Set a column's width directly.
1500+
if col == 2 {
1501+
return lipgloss.NewStyle().Width(5)
1502+
}
1503+
return lipgloss.NewStyle()
1504+
})
1505+
table.Width(defaultWidth)
1506+
// check total width.
1507+
if got := lipgloss.Width(table.String()); got != defaultWidth {
1508+
t.Log(table.String())
1509+
t.Fatalf("Table is not the correct width. got %d, want %d", got, defaultWidth)
1510+
}
1511+
1512+
// check that width is overridden with a small value.
1513+
if table.widths[2] != 5 {
1514+
t.Log(table.String())
1515+
t.Fatalf("Did not set correct width value at column at index %d.\ngot %d, want %d", 2, table.widths[2], 5)
1516+
}
1517+
1518+
// check that width is overridden with a wide value.
1519+
if table.widths[1] != 30 {
1520+
t.Log(table.String())
1521+
t.Fatalf("Did not set correct width value at column at index %d.\ngot %d, want %d", 1, table.widths[1], 30)
1522+
}
1523+
1524+
t.Log(table.widths[2])
1525+
golden.RequireEqual(t, []byte(table.String()))
1526+
})
14931527
}
14941528
}
14951529

14961530
// Test truncation for overflow and no wrap when combined.
14971531
func TestTableOverFlowNoWrap(t *testing.T) {
1498-
// Long text, different languages
1532+
// LongTextDifferentLanguages
14991533
headers := []string{"Hello", "ไฝ ๅฅฝ", "ู…ุฑุญุจู‹ุง", "์•ˆ๋…•ํ•˜์„ธ์š”"}
15001534
data := [][]string{
15011535
{
@@ -1685,7 +1719,7 @@ func stripString(str string) string {
16851719
// Examples
16861720

16871721
func ExampleTable_Wrap() {
1688-
// Long text, different languages
1722+
// LongTextDifferentLanguages
16891723
headers := []string{"Hello", "ไฝ ๅฅฝ", "ู…ุฑุญุจู‹ุง", "์•ˆ๋…•ํ•˜์„ธ์š”"}
16901724
data := [][]string{
16911725
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ•ฎ
2+
โ”‚Destination โ”‚Why are you going on this triโ€ฆโ”‚Affoโ€ฆโ”‚
3+
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”ค
4+
โ”‚Mexico โ”‚I want to go somewhere hot, โ”‚$ โ”‚
5+
โ”‚ โ”‚dry, and affordable. Mexico โ”‚ โ”‚
6+
โ”‚ โ”‚has really good food, just โ”‚ โ”‚
7+
โ”‚ โ”‚don't drink tap water! โ”‚ โ”‚
8+
โ”‚New York โ”‚I'm thinking about going โ”‚$$$ โ”‚
9+
โ”‚ โ”‚during the Christmas season toโ”‚ โ”‚
10+
โ”‚ โ”‚check out Rockefeller center. โ”‚ โ”‚
11+
โ”‚ โ”‚Might be cold though... โ”‚ โ”‚
12+
โ”‚California โ”‚ โ”‚$$$ โ”‚
13+
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ•ฏ
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
2+
โ”‚Name โ”‚Description โ”‚Type โ”‚Required โ”‚Default โ”‚
3+
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
4+
โ”‚command โ”‚A command to be executed โ”‚yes โ”‚hello โ”‚yep โ”‚
5+
โ”‚ โ”‚inside the container to assessโ”‚ โ”‚ โ”‚ โ”‚
6+
โ”‚ โ”‚its health. Each space โ”‚ โ”‚ โ”‚ โ”‚
7+
โ”‚ โ”‚delimited token of the commandโ”‚ โ”‚ โ”‚ โ”‚
8+
โ”‚ โ”‚is a separate array element. โ”‚ โ”‚ โ”‚ โ”‚
9+
โ”‚ โ”‚Commands exiting 0 are โ”‚ โ”‚ โ”‚ โ”‚
10+
โ”‚ โ”‚considered to be successful โ”‚ โ”‚ โ”‚ โ”‚
11+
โ”‚ โ”‚probes, whilst all other exit โ”‚ โ”‚ โ”‚ โ”‚
12+
โ”‚ โ”‚codes are considered failures.โ”‚ โ”‚ โ”‚ โ”‚
13+
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

0 commit comments

Comments
ย (0)