Skip to content

Commit

Permalink
Fix cell range parsing and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarleton committed Apr 25, 2018
1 parent 1387ac9 commit 16c0ae2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func (c CellPos) A1Notation() string {
return fmt.Sprintf("%s%d", aRangeLetter(c.Col), c.Row + 1)
}

func (c CellPos) RangeForData(data [][]string) CellRange {
bottomLeft := CellPos{c.Row + len(data) - 1, c.Col + len(data[0]) - 1}

return CellRange{Start: c, End: bottomLeft}
}

type CellRange struct {
Start CellPos
End CellPos
Expand Down
33 changes: 33 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,36 @@ func TestCellPosA1Notation(t *testing.T) {
}
}

var rangeTests = []struct{
topLeft CellPos
width int
height int
expected string
}{
{CellPos{}, 1, 1, "A1:A1"},
{CellPos{}, 1, 2, "A1:A2"},
{CellPos{}, 2, 2, "A1:B2"},
{CellPos{0, 10}, 2, 2, "K1:L2"},
{CellPos{10, 3}, 2, 3, "D11:E13"},
}

func TestRange(t *testing.T) {
for _, tt := range rangeTests {
var data [][]string
for i := 0; i < tt.height; i++ {
var row []string
for j := 0; j < tt.width; j++ {
row = append(row, "1")
}
data = append(data, row)
}

got := tt.topLeft.RangeForData(data).String()

if got != tt.expected {
t.Errorf("Wanted %s, but got %s for %+v, table: %v", tt.expected, got, tt, data)
}

}
}

2 changes: 1 addition & 1 deletion spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (s *Sheet) UpdateFromPosition(data [][]string, start CellPos) error {
converted = append(converted, strToInterface(row))
}

cellRange := DefaultRange(data)
cellRange := start.RangeForData(data)

sheetRange := fmt.Sprintf("%s!%s", s.Title(), cellRange.String())

Expand Down
2 changes: 1 addition & 1 deletion spreadsheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var tsvTests = []struct {

func TestTsvToArr(t *testing.T) {
for _, tt := range tsvTests {
got := TsvToArr(strings.NewReader(tt.data))
got := TsvToArr(strings.NewReader(tt.data), "\t")
if len(got) != len(tt.result) {
t.Errorf("For \n%s\n, mismatched rows. wanted %v, but got %v", tt.data, tt.result, got)
}
Expand Down

0 comments on commit 16c0ae2

Please sign in to comment.