spreadsheet: add more unit tests

This commit is contained in:
Todd 2017-09-06 20:16:46 -04:00
parent 982e3a4f7d
commit 607db91c86
2 changed files with 69 additions and 8 deletions

View File

@ -62,10 +62,10 @@ func (c Cell) SetRichTextString() RichText {
// SetFormulaRaw sets the cell type to formula, and the raw formula to the given string
func (c Cell) SetFormulaRaw(s string) {
c.clearValue()
c.x.TAttr = sml.ST_CellTypeStr
c.x.F = sml.NewCT_CellFormula()
c.x.F.Content = s
c.x.V = nil
}
// SetString sets the cell type to string, and the value to the given string,
@ -235,11 +235,6 @@ func (c Cell) SetStyleIndex(idx uint32) {
func (c Cell) GetValue() (string, error) {
switch c.x.TAttr {
case sml.ST_CellTypeB:
if c.x.V == nil {
return "", nil
}
return *c.x.V, nil
case sml.ST_CellTypeInlineStr:
if c.x.Is == nil || c.x.Is.T == nil {
return "", nil
@ -254,10 +249,15 @@ func (c Cell) GetValue() (string, error) {
return "", err
}
return c.w.SharedStrings.GetString(id)
case sml.ST_CellTypeE:
case sml.ST_CellTypeN:
case sml.ST_CellTypeStr:
if c.x.F != nil {
return c.x.F.Content, nil
}
default:
if c.x.V == nil {
return "", nil
}
return *c.x.V, nil
}
return "", errors.New("unsupported cell type")
}

View File

@ -8,6 +8,7 @@
package spreadsheet_test
import (
"strconv"
"testing"
"time"
@ -133,3 +134,63 @@ func TestCellGetTime(t *testing.T) {
t.Errorf("expected f = %s, got %s", tm, f)
}
}
func TestCellClear(t *testing.T) {
wb := spreadsheet.New()
sheet := wb.AddSheet()
row := sheet.AddRow()
cell := row.AddCell()
cell.SetInlineString("a")
if cell.X().Is == nil {
t.Errorf("expected is non nil")
}
cell.SetFormulaRaw("=1+2")
if cell.X().F == nil {
t.Errorf("expected f != nilnil")
}
cell.SetDate(time.Now())
if cell.X().V == nil {
t.Errorf("expected v != nil")
}
cell.Clear()
if cell.X().F != nil {
t.Errorf("expected f = nil")
}
if cell.X().Is != nil {
t.Errorf("expected is = nil")
}
if cell.X().V != nil {
t.Errorf("expected v = nil")
}
}
func TestCellRichTextString(t *testing.T) {
wb := spreadsheet.New()
sheet := wb.AddSheet()
row := sheet.AddRow()
cell := row.AddCell()
rt := cell.SetRichTextString()
if rt.X() != cell.X().Is {
t.Errorf("rich text should wrap cell Is")
}
}
func TestCellStringByID(t *testing.T) {
wb := spreadsheet.New()
sheet := wb.AddSheet()
row := sheet.AddRow()
cell := row.AddCell()
// this isn't proper usage of SetStringByID, but it verifies
// the functionality
cell.SetStringByID(1)
v, err := strconv.ParseUint(*cell.X().V, 10, 32)
if v != 1 || err != nil {
t.Errorf("expected 1 and no error, got %d %s", v, err)
}
}