Skip to content

Commit

Permalink
Add fuzzing tests for go 1.18 to fix scrolling edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras committed Jun 11, 2022
1 parent a7e8805 commit 8493606
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*.out
*.coverage

# Fuzzing data
testdata

# Go vendor folder
vendor/

Expand Down
6 changes: 0 additions & 6 deletions table/scrolling.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,4 @@ func (m *Model) recalculateLastHorizontalColumn() {
m.maxHorizontalColumnIndex = i - m.horizontalScrollFreezeColumnsCount
}
}

if m.maxHorizontalColumnIndex <= m.horizontalScrollFreezeColumnsCount {
m.maxHorizontalColumnIndex = 0

return
}
}
96 changes: 96 additions & 0 deletions table/scrolling_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//go:build go1.18
// +build go1.18

package table

import (
"fmt"
"strings"
"testing"

tea "github.com/charmbracelet/bubbletea"
"github.com/stretchr/testify/assert"
)

// This is long because of test cases
// nolint: funlen
func FuzzHorizontalScrollingStopEdgeCases(f *testing.F) {
const (
minNameWidth = 2
maxNameWidth = 50

minColWidth = 4
maxColWidth = 50

minNumCols = 1
maxNumCols = 500

minMaxWidth = 5
maxMaxWidth = 200

borderBuffer = 4
)

f.Add(5, 3, 5, 30)
f.Fuzz(func(t *testing.T, nameWidth, colWidth, numCols, maxWidth int) {
if nameWidth < minNameWidth ||
nameWidth > maxNameWidth ||
nameWidth > maxWidth-colWidth ||
nameWidth+colWidth+borderBuffer >= maxWidth {
return
}

if colWidth < minColWidth ||
colWidth > maxColWidth ||
colWidth >= maxWidth {
return
}

if numCols < minNumCols || numCols > maxNumCols {
return
}

if maxWidth < minMaxWidth || maxWidth > maxMaxWidth {
return
}

cols := []Column{NewColumn("Name", "Name", nameWidth)}
for i := 0; i < numCols; i++ {
s := fmt.Sprintf("%d", i+1)
cols = append(cols, NewColumn(s, s, colWidth))
}

rowData := RowData{"Name": "A"}

for i := 0; i < numCols; i++ {
s := fmt.Sprintf("%d", i+1)
rowData[s] = s
}

rows := []Row{NewRow(rowData)}

model := New(cols).
WithRows(rows).
WithStaticFooter("Footer").
WithMaxTotalWidth(maxWidth).
WithHorizontalFreezeColumnCount(1).
Focused(true)

hitScrollRight := func() {
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyShiftRight})
}

// Excessive scrolling attempts to be sure
for i := 0; i < numCols*2; i++ {
hitScrollRight()
}

rendered := model.View()

assert.NotContains(t, rendered, ">")

if !strings.Contains(rendered, "…") {
assert.Contains(t, rendered, fmt.Sprintf("%d", numCols))
}
})
}
12 changes: 12 additions & 0 deletions table/scrolling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ func TestHorizontalScrollingStopEdgeCases(t *testing.T) {
maxWidth: 20,
expectedCols: []int{7, 8},
},
{
numCols: 6,
nameWidth: 5,
colWidth: 3,
maxWidth: 30,
},
{
numCols: 50,
nameWidth: 20,
colWidth: 6,
maxWidth: 31,
},
}

for i, test := range tests {
Expand Down

0 comments on commit 8493606

Please sign in to comment.