Skip to content

Commit

Permalink
Export 7 errors so users can act differently on different type of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Dec 6, 2021
1 parent 3325c39 commit 44a13aa
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 130 deletions.
14 changes: 7 additions & 7 deletions adjust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAdjustMergeCells(t *testing.T) {
},
},
},
}, rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
}, rows, 0, 0), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
assert.EqualError(t, f.adjustMergeCells(&xlsxWorksheet{
MergeCells: &xlsxMergeCells{
Cells: []*xlsxMergeCell{
Expand All @@ -26,7 +26,7 @@ func TestAdjustMergeCells(t *testing.T) {
},
},
},
}, rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
}, rows, 0, 0), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
assert.NoError(t, f.adjustMergeCells(&xlsxWorksheet{
MergeCells: &xlsxMergeCells{
Cells: []*xlsxMergeCell{
Expand Down Expand Up @@ -265,12 +265,12 @@ func TestAdjustAutoFilter(t *testing.T) {
AutoFilter: &xlsxAutoFilter{
Ref: "A:B1",
},
}, rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
}, rows, 0, 0), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
AutoFilter: &xlsxAutoFilter{
Ref: "A1:B",
},
}, rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
}, rows, 0, 0), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
}

func TestAdjustHelper(t *testing.T) {
Expand All @@ -281,8 +281,8 @@ func TestAdjustHelper(t *testing.T) {
f.Sheet.Store("xl/worksheets/sheet2.xml", &xlsxWorksheet{
AutoFilter: &xlsxAutoFilter{Ref: "A1:B"}})
// testing adjustHelper with illegal cell coordinates.
assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
// testing adjustHelper on not exists worksheet.
assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN is not exist")
}
Expand All @@ -298,7 +298,7 @@ func TestAdjustCalcChain(t *testing.T) {
assert.NoError(t, f.InsertRow("Sheet1", 1))

f.CalcChain.C[1].R = "invalid coordinates"
assert.EqualError(t, f.InsertCol("Sheet1", "A"), `cannot convert cell "invalid coordinates" to coordinates: invalid cell name "invalid coordinates"`)
assert.EqualError(t, f.InsertCol("Sheet1", "A"), newCellNameToCoordinatesError("invalid coordinates", newInvalidCellNameError("invalid coordinates")).Error())
f.CalcChain = nil
assert.NoError(t, f.InsertCol("Sheet1", "A"))
}
22 changes: 11 additions & 11 deletions calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ func TestCalcCellValue(t *testing.T) {
// ABS
"=ABS()": "ABS requires 1 numeric argument",
`=ABS("X")`: "strconv.ParseFloat: parsing \"X\": invalid syntax",
"=ABS(~)": `invalid column name "~"`,
"=ABS(~)": newInvalidColumnNameError("~").Error(),
// ACOS
"=ACOS()": "ACOS requires 1 numeric argument",
`=ACOS("X")`: "strconv.ParseFloat: parsing \"X\": invalid syntax",
Expand Down Expand Up @@ -2699,15 +2699,15 @@ func TestCalcCellValue(t *testing.T) {
// COLUMN
"=COLUMN(1,2)": "COLUMN requires at most 1 argument",
"=COLUMN(\"\")": "invalid reference",
"=COLUMN(Sheet1)": "invalid column name \"Sheet1\"",
"=COLUMN(Sheet1!A1!B1)": "invalid column name \"Sheet1\"",
"=COLUMN(Sheet1)": newInvalidColumnNameError("Sheet1").Error(),
"=COLUMN(Sheet1!A1!B1)": newInvalidColumnNameError("Sheet1").Error(),
// COLUMNS
"=COLUMNS()": "COLUMNS requires 1 argument",
"=COLUMNS(1)": "invalid reference",
"=COLUMNS(\"\")": "invalid reference",
"=COLUMNS(Sheet1)": "invalid column name \"Sheet1\"",
"=COLUMNS(Sheet1!A1!B1)": "invalid column name \"Sheet1\"",
"=COLUMNS(Sheet1!Sheet1)": "invalid column name \"Sheet1\"",
"=COLUMNS(Sheet1)": newInvalidColumnNameError("Sheet1").Error(),
"=COLUMNS(Sheet1!A1!B1)": newInvalidColumnNameError("Sheet1").Error(),
"=COLUMNS(Sheet1!Sheet1)": newInvalidColumnNameError("Sheet1").Error(),
// HLOOKUP
"=HLOOKUP()": "HLOOKUP requires at least 3 arguments",
"=HLOOKUP(D2,D1,1,FALSE)": "HLOOKUP requires second argument of table array",
Expand Down Expand Up @@ -2751,15 +2751,15 @@ func TestCalcCellValue(t *testing.T) {
// ROW
"=ROW(1,2)": "ROW requires at most 1 argument",
"=ROW(\"\")": "invalid reference",
"=ROW(Sheet1)": "invalid column name \"Sheet1\"",
"=ROW(Sheet1!A1!B1)": "invalid column name \"Sheet1\"",
"=ROW(Sheet1)": newInvalidColumnNameError("Sheet1").Error(),
"=ROW(Sheet1!A1!B1)": newInvalidColumnNameError("Sheet1").Error(),
// ROWS
"=ROWS()": "ROWS requires 1 argument",
"=ROWS(1)": "invalid reference",
"=ROWS(\"\")": "invalid reference",
"=ROWS(Sheet1)": "invalid column name \"Sheet1\"",
"=ROWS(Sheet1!A1!B1)": "invalid column name \"Sheet1\"",
"=ROWS(Sheet1!Sheet1)": "invalid column name \"Sheet1\"",
"=ROWS(Sheet1)": newInvalidColumnNameError("Sheet1").Error(),
"=ROWS(Sheet1!A1!B1)": newInvalidColumnNameError("Sheet1").Error(),
"=ROWS(Sheet1!Sheet1)": newInvalidColumnNameError("Sheet1").Error(),
// Web Functions
// ENCODEURL
"=ENCODEURL()": "ENCODEURL requires 1 argument",
Expand Down
20 changes: 10 additions & 10 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ func TestCheckCellInArea(t *testing.T) {
}

ok, err := f.checkCellInArea("A1", "A:B")
assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, err, newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
assert.False(t, ok)

ok, err = f.checkCellInArea("AA0", "Z0:AB1")
assert.EqualError(t, err, `cannot convert cell "AA0" to coordinates: invalid cell name "AA0"`)
assert.EqualError(t, err, newCellNameToCoordinatesError("AA0", newInvalidCellNameError("AA0")).Error())
assert.False(t, ok)
}

Expand Down Expand Up @@ -146,13 +146,13 @@ func TestSetCellFloat(t *testing.T) {
assert.Equal(t, "123.42", val, "A1 should be 123.42")
})
f := NewFile()
assert.EqualError(t, f.SetCellFloat(sheet, "A", 123.42, -1, 64), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.SetCellFloat(sheet, "A", 123.42, -1, 64), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
}

func TestSetCellValue(t *testing.T) {
f := NewFile()
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
}

func TestSetCellValues(t *testing.T) {
Expand All @@ -175,7 +175,7 @@ func TestSetCellValues(t *testing.T) {

func TestSetCellBool(t *testing.T) {
f := NewFile()
assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
}

func TestSetCellTime(t *testing.T) {
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestGetCellType(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, CellTypeString, cellType)
_, err = f.GetCellType("Sheet1", "A")
assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, err, newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
}

func TestGetCellFormula(t *testing.T) {
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestSetCellFormula(t *testing.T) {
assert.NoError(t, f.SetCellFormula("Sheet1", "C19", "SUM(Sheet2!D2,Sheet2!D9)"))

// Test set cell formula with illegal rows number.
assert.EqualError(t, f.SetCellFormula("Sheet1", "C", "SUM(Sheet2!D2,Sheet2!D9)"), `cannot convert cell "C" to coordinates: invalid cell name "C"`)
assert.EqualError(t, f.SetCellFormula("Sheet1", "C", "SUM(Sheet2!D2,Sheet2!D9)"), newCellNameToCoordinatesError("C", newInvalidCellNameError("C")).Error())

assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellFormula1.xlsx")))
assert.NoError(t, f.Close())
Expand Down Expand Up @@ -523,7 +523,7 @@ func TestGetCellRichText(t *testing.T) {
assert.EqualError(t, err, "sheet SheetN is not exist")
// Test set cell rich text with illegal cell coordinates
_, err = f.GetCellRichText("Sheet1", "A")
assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, err, newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
}
func TestSetCellRichText(t *testing.T) {
f := NewFile()
Expand Down Expand Up @@ -603,7 +603,7 @@ func TestSetCellRichText(t *testing.T) {
// Test set cell rich text on not exists worksheet
assert.EqualError(t, f.SetCellRichText("SheetN", "A1", richTextRun), "sheet SheetN is not exist")
// Test set cell rich text with illegal cell coordinates
assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
richTextRun = []RichTextRun{{Text: strings.Repeat("s", TotalCellChars+1)}}
// Test set cell rich text with characters over the maximum limit
assert.EqualError(t, f.SetCellRichText("Sheet1", "A1", richTextRun), ErrCellCharsLength.Error())
Expand Down
6 changes: 3 additions & 3 deletions chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestChartSize(t *testing.T) {

func TestAddDrawingChart(t *testing.T) {
f := NewFile()
assert.EqualError(t, f.addDrawingChart("SheetN", "", "", 0, 0, 0, nil), `cannot convert cell "" to coordinates: invalid cell name ""`)
assert.EqualError(t, f.addDrawingChart("SheetN", "", "", 0, 0, 0, nil), newCellNameToCoordinatesError("", newInvalidCellNameError("")).Error())
}

func TestAddChart(t *testing.T) {
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestAddChart(t *testing.T) {
}
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddChart.xlsx")))
// Test with illegal cell coordinates
assert.EqualError(t, f.AddChart("Sheet2", "A", `{"type":"col","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"2D Column Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.AddChart("Sheet2", "A", `{"type":"col","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"2D Column Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
// Test with unsupported chart type
assert.EqualError(t, f.AddChart("Sheet2", "BD32", `{"type":"unknown","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Bubble 3D Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`), "unsupported chart type unknown")
// Test add combo chart with invalid format set
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestDeleteChart(t *testing.T) {
// Test delete chart on not exists worksheet.
assert.EqualError(t, f.DeleteChart("SheetN", "A1"), "sheet SheetN is not exist")
// Test delete chart with invalid coordinates.
assert.EqualError(t, f.DeleteChart("Sheet1", ""), `cannot convert cell "" to coordinates: invalid cell name ""`)
assert.EqualError(t, f.DeleteChart("Sheet1", ""), newCellNameToCoordinatesError("", newInvalidCellNameError("")).Error())
// Test delete chart on no chart worksheet.
assert.NoError(t, NewFile().DeleteChart("Sheet1", "A1"))
assert.NoError(t, f.Close())
Expand Down
Loading

0 comments on commit 44a13aa

Please sign in to comment.