From 40ff5dc1a7d7aa42f5db9cf9dfe858cc3820b44e Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 23 Mar 2019 20:08:06 +0800 Subject: refactor: handler error instead of panic, Exported functions: SetCellStyle InsertCol RemoveCol RemoveRow InsertRow DuplicateRow DuplicateRowTo SetRowHeight GetRowHeight GetCellValue GetCellFormula GetCellHyperLink SetCellHyperLink SetCellInt SetCellBool SetCellFloat SetCellStr SetCellDefault GetCellStyle SetCellValue MergeCell SetSheetRow SetRowVisible GetRowVisible SetRowOutlineLevel GetRowOutlineLevel GetRows Columns SearchSheet AddTable GetPicture AutoFilter GetColVisible SetColVisible GetColOutlineLevel SetColOutlineLevel SetColWidth GetColWidth inner functions: adjustHelper adjustMergeCells adjustAutoFilter prepareCell setDefaultTimeStyle timeToExcelTime addDrawingChart addDrawingVML addDrawingPicture getTotalRowsCols checkRow addDrawingShape addTable --- cell_test.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'cell_test.go') diff --git a/cell_test.go b/cell_test.go index 12efc17..7b1381f 100644 --- a/cell_test.go +++ b/cell_test.go @@ -17,8 +17,9 @@ func TestCheckCellInArea(t *testing.T) { for _, expectedTrueCellInArea := range expectedTrueCellInAreaList { cell := expectedTrueCellInArea[0] area := expectedTrueCellInArea[1] - - assert.Truef(t, checkCellInArea(cell, area), + ok, err := checkCellInArea(cell, area) + assert.NoError(t, err) + assert.Truef(t, ok, "Expected cell %v to be in area %v, got false\n", cell, area) } @@ -31,14 +32,15 @@ func TestCheckCellInArea(t *testing.T) { for _, expectedFalseCellInArea := range expectedFalseCellInAreaList { cell := expectedFalseCellInArea[0] area := expectedFalseCellInArea[1] - - assert.Falsef(t, checkCellInArea(cell, area), + ok, err := checkCellInArea(cell, area) + assert.NoError(t, err) + assert.Falsef(t, ok, "Expected cell %v not to be inside of area %v, but got true\n", cell, area) } - assert.Panics(t, func() { - checkCellInArea("AA0", "Z0:AB1") - }) + ok, err := checkCellInArea("AA0", "Z0:AB1") + assert.EqualError(t, err, `cannot convert cell "AA0" to coordinates: invalid cell name "AA0"`) + assert.False(t, ok) } func TestSetCellFloat(t *testing.T) { @@ -47,20 +49,28 @@ func TestSetCellFloat(t *testing.T) { f := NewFile() f.SetCellFloat(sheet, "A1", 123.0, -1, 64) f.SetCellFloat(sheet, "A2", 123.0, 1, 64) - assert.Equal(t, "123", f.GetCellValue(sheet, "A1"), "A1 should be 123") - assert.Equal(t, "123.0", f.GetCellValue(sheet, "A2"), "A2 should be 123.0") + val, err := f.GetCellValue(sheet, "A1") + assert.NoError(t, err) + assert.Equal(t, "123", val, "A1 should be 123") + val, err = f.GetCellValue(sheet, "A2") + assert.NoError(t, err) + assert.Equal(t, "123.0", val, "A2 should be 123.0") }) t.Run("with a decimal and precision limit", func(t *testing.T) { f := NewFile() f.SetCellFloat(sheet, "A1", 123.42, 1, 64) - assert.Equal(t, "123.4", f.GetCellValue(sheet, "A1"), "A1 should be 123.4") + val, err := f.GetCellValue(sheet, "A1") + assert.NoError(t, err) + assert.Equal(t, "123.4", val, "A1 should be 123.4") }) t.Run("with a decimal and no limit", func(t *testing.T) { f := NewFile() f.SetCellFloat(sheet, "A1", 123.42, -1, 64) - assert.Equal(t, "123.42", f.GetCellValue(sheet, "A1"), "A1 should be 123.42") + val, err := f.GetCellValue(sheet, "A1") + assert.NoError(t, err) + assert.Equal(t, "123.42", val, "A1 should be 123.42") }) } @@ -68,6 +78,7 @@ func ExampleFile_SetCellFloat() { f := NewFile() var x = 3.14159265 f.SetCellFloat("Sheet1", "A1", x, 2, 64) - fmt.Println(f.GetCellValue("Sheet1", "A1")) + val, _ := f.GetCellValue("Sheet1", "A1") + fmt.Println(val) // Output: 3.14 } -- cgit v1.2.1