summaryrefslogtreecommitdiff
path: root/rows_test.go
diff options
context:
space:
mode:
authorVeniamin Albaev <albenik@gmail.com>2019-03-19 19:14:41 +0300
committerxuri <xuri.me@gmail.com>2019-03-20 00:14:41 +0800
commitdc01264562e6e88d77a28042408029770ea32df4 (patch)
treef3d8fd1627fb71676bab59fe2fa1c9b076b360d8 /rows_test.go
parent092f16c744c40e85be5cf6128dfb35c96e7df78b (diff)
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes. But have to go deeper, do fixes, after do related fixes and again and again. Major improvements: 1. Tests made stronger again (But still be weak). 2. "Empty" returns for incorrect input replaces with panic. 3. Check for correct col/row/cell naming & addressing by default. 4. Removed huge amount of duplicated code. 5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all, and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName(). 6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc). * Formatting fixes
Diffstat (limited to 'rows_test.go')
-rw-r--r--rows_test.go174
1 files changed, 90 insertions, 84 deletions
diff --git a/rows_test.go b/rows_test.go
index b83d377..50e26dd 100644
--- a/rows_test.go
+++ b/rows_test.go
@@ -3,44 +3,38 @@ package excelize
import (
"fmt"
"path/filepath"
- "strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRows(t *testing.T) {
+ const sheet2 = "Sheet2"
+
xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) {
t.FailNow()
}
- rows, err := xlsx.Rows("Sheet2")
+ rows, err := xlsx.Rows(sheet2)
if !assert.NoError(t, err) {
t.FailNow()
}
- rowStrs := make([][]string, 0)
- var i = 0
+ collectedRows := make([][]string, 0)
for rows.Next() {
- i++
- columns := rows.Columns()
- rowStrs = append(rowStrs, columns)
+ collectedRows = append(collectedRows, trimSliceSpace(rows.Columns()))
}
-
if !assert.NoError(t, rows.Error()) {
t.FailNow()
}
- dstRows := xlsx.GetRows("Sheet2")
- if !assert.Equal(t, len(rowStrs), len(dstRows)) {
- t.FailNow()
+ returnedRows := xlsx.GetRows(sheet2)
+ for i := range returnedRows {
+ returnedRows[i] = trimSliceSpace(returnedRows[i])
}
-
- for i := 0; i < len(rowStrs); i++ {
- if !assert.Equal(t, trimSliceSpace(dstRows[i]), trimSliceSpace(rowStrs[i])) {
- t.FailNow()
- }
+ if !assert.Equal(t, collectedRows, returnedRows) {
+ t.FailNow()
}
r := Rows{}
@@ -60,8 +54,13 @@ func TestRowHeight(t *testing.T) {
xlsx := NewFile()
sheet1 := xlsx.GetSheetName(1)
- xlsx.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0) // should no effect
- assert.Equal(t, defaultRowHeightPixels, xlsx.GetRowHeight("Sheet1", 0))
+ assert.Panics(t, func() {
+ xlsx.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0)
+ })
+
+ assert.Panics(t, func() {
+ xlsx.GetRowHeight("Sheet1", 0)
+ })
xlsx.SetRowHeight(sheet1, 1, 111.0)
assert.Equal(t, 111.0, xlsx.GetRowHeight(sheet1, 1))
@@ -77,32 +76,47 @@ func TestRowHeight(t *testing.T) {
convertColWidthToPixels(0)
}
+func TestRowVisibility(t *testing.T) {
+ xlsx, err := prepareTestBook1()
+ if !assert.NoError(t, err) {
+ t.FailNow()
+ }
+
+ xlsx.SetRowVisible("Sheet3", 2, false)
+ xlsx.SetRowVisible("Sheet3", 2, true)
+ xlsx.GetRowVisible("Sheet3", 2)
+
+ assert.Panics(t, func() {
+ xlsx.SetRowVisible("Sheet3", 0, true)
+ })
+
+ assert.Panics(t, func() {
+ xlsx.GetRowVisible("Sheet3", 0)
+ })
+
+ assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestRowVisibility.xlsx")))
+}
+
func TestRemoveRow(t *testing.T) {
xlsx := NewFile()
sheet1 := xlsx.GetSheetName(1)
r := xlsx.workSheetReader(sheet1)
const (
- cellCount = 10
- rowCount = 10
+ colCount = 10
+ rowCount = 10
)
- for j := 1; j <= cellCount; j++ {
- for i := 1; i <= rowCount; i++ {
- axis := ToAlphaString(i) + strconv.Itoa(j)
- xlsx.SetCellStr(sheet1, axis, axis)
- }
- }
+ fillCells(xlsx, sheet1, colCount, rowCount)
+
xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
- xlsx.RemoveRow(sheet1, -1)
- if !assert.Len(t, r.SheetData.Row, rowCount) {
- t.FailNow()
- }
+ assert.Panics(t, func() {
+ xlsx.RemoveRow(sheet1, -1)
+ })
- xlsx.RemoveRow(sheet1, 0)
- if !assert.Len(t, r.SheetData.Row, rowCount) {
- t.FailNow()
- }
+ assert.Panics(t, func() {
+ xlsx.RemoveRow(sheet1, 0)
+ })
xlsx.RemoveRow(sheet1, 4)
if !assert.Len(t, r.SheetData.Row, rowCount-1) {
@@ -150,26 +164,20 @@ func TestInsertRow(t *testing.T) {
r := xlsx.workSheetReader(sheet1)
const (
- cellCount = 10
- rowCount = 10
+ colCount = 10
+ rowCount = 10
)
- for j := 1; j <= cellCount; j++ {
- for i := 1; i < rowCount; i++ {
- axis := ToAlphaString(i) + strconv.Itoa(j)
- xlsx.SetCellStr(sheet1, axis, axis)
- }
- }
+ fillCells(xlsx, sheet1, colCount, rowCount)
+
xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
- xlsx.InsertRow(sheet1, -1)
- if !assert.Len(t, r.SheetData.Row, rowCount) {
- t.FailNow()
- }
+ assert.Panics(t, func() {
+ xlsx.InsertRow(sheet1, -1)
+ })
- xlsx.InsertRow(sheet1, 0)
- if !assert.Len(t, r.SheetData.Row, rowCount) {
- t.FailNow()
- }
+ assert.Panics(t, func() {
+ xlsx.InsertRow(sheet1, 0)
+ })
xlsx.InsertRow(sheet1, 1)
if !assert.Len(t, r.SheetData.Row, rowCount+1) {
@@ -304,19 +312,24 @@ func TestDuplicateRow(t *testing.T) {
t.Run("ZeroWithNoRows", func(t *testing.T) {
xlsx := NewFile()
- xlsx.DuplicateRow(sheet, 0)
+ assert.Panics(t, func() {
+ xlsx.DuplicateRow(sheet, 0)
+ })
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.ZeroWithNoRows"))) {
t.FailNow()
}
+
assert.Equal(t, "", xlsx.GetCellValue(sheet, "A1"))
assert.Equal(t, "", xlsx.GetCellValue(sheet, "B1"))
assert.Equal(t, "", xlsx.GetCellValue(sheet, "A2"))
assert.Equal(t, "", xlsx.GetCellValue(sheet, "B2"))
+
expect := map[string]string{
"A1": "", "B1": "",
"A2": "", "B2": "",
}
+
for cell, val := range expect {
if !assert.Equal(t, val, xlsx.GetCellValue(sheet, cell), cell) {
t.FailNow()
@@ -444,31 +457,19 @@ func TestDuplicateRowInvalidRownum(t *testing.T) {
"B3": "B3 Value",
}
- testRows := []int{-2, -1}
-
- testRowPairs := []struct {
- row1 int
- row2 int
- }{
- {-1, -1},
- {-1, 0},
- {-1, 1},
- {0, -1},
- {0, 0},
- {0, 1},
- {1, -1},
- {1, 1},
- {1, 0},
- }
+ invalidIndexes := []int{-100, -2, -1, 0}
- for i, row := range testRows {
- name := fmt.Sprintf("TestRow_%d", i+1)
+ for _, row := range invalidIndexes {
+ name := fmt.Sprintf("%d", row)
t.Run(name, func(t *testing.T) {
xlsx := NewFile()
for col, val := range cells {
xlsx.SetCellStr(sheet, col, val)
}
- xlsx.DuplicateRow(sheet, row)
+
+ assert.Panics(t, func() {
+ xlsx.DuplicateRow(sheet, row)
+ })
for col, val := range cells {
if !assert.Equal(t, val, xlsx.GetCellValue(sheet, col)) {
@@ -479,22 +480,27 @@ func TestDuplicateRowInvalidRownum(t *testing.T) {
})
}
- for i, pair := range testRowPairs {
- name := fmt.Sprintf("TestRowPair_%d", i+1)
- t.Run(name, func(t *testing.T) {
- xlsx := NewFile()
- for col, val := range cells {
- xlsx.SetCellStr(sheet, col, val)
- }
- xlsx.DuplicateRowTo(sheet, pair.row1, pair.row2)
+ for _, row1 := range invalidIndexes {
+ for _, row2 := range invalidIndexes {
+ name := fmt.Sprintf("[%d,%d]", row1, row2)
+ t.Run(name, func(t *testing.T) {
+ xlsx := NewFile()
+ for col, val := range cells {
+ xlsx.SetCellStr(sheet, col, val)
+ }
- for col, val := range cells {
- if !assert.Equal(t, val, xlsx.GetCellValue(sheet, col)) {
- t.FailNow()
+ assert.Panics(t, func() {
+ xlsx.DuplicateRowTo(sheet, row1, row2)
+ })
+
+ for col, val := range cells {
+ if !assert.Equal(t, val, xlsx.GetCellValue(sheet, col)) {
+ t.FailNow()
+ }
}
- }
- assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, name)))
- })
+ assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, name)))
+ })
+ }
}
}