Skip to content

Commit

Permalink
spreadsheet: add unit tests for default number formats
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaliance committed Sep 20, 2017
1 parent 6a8920a commit 5f6ed3a
Showing 1 changed file with 71 additions and 20 deletions.
91 changes: 71 additions & 20 deletions spreadsheet/defaultnumberformats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,64 @@ import (
"baliance.com/gooxml/spreadsheet"
)

var standardFmtTestData = []struct {
Inp float64
Fmt spreadsheet.StandardFormat
Exp string
}{
{0, spreadsheet.StandardFormatGeneral, "0"},
{1.23, spreadsheet.StandardFormatGeneral, "1.23"},
{5, spreadsheet.StandardFormatWholeNumber, "5"},
{5, spreadsheet.StandardFormat2, "5.00"},
{5, spreadsheet.StandardFormat3, "5"},
{5123, spreadsheet.StandardFormat3, "5,123"},
{5123, spreadsheet.StandardFormat4, "5,123.00"},
{0, spreadsheet.StandardFormatPercent, "0%"},
{.25, spreadsheet.StandardFormatPercent, "25%"},
{1.2, spreadsheet.StandardFormatPercent, "120%"},
{0, spreadsheet.StandardFormat10, "0.00%"},
{.2502, spreadsheet.StandardFormat10, "25.02%"},
{1.2, spreadsheet.StandardFormat10, "120.00%"},
{0.5, spreadsheet.StandardFormat11, "5.00E-01"},
{0.5, spreadsheet.StandardFormat12, "1/2"},
{1.5, spreadsheet.StandardFormat12, "1 1/2"},
{3.25, spreadsheet.StandardFormat12, "3 1/4"},
{1.5, spreadsheet.StandardFormat13, "1 1/2"},
// Excel gives 2 9/31 here, but 2 20/69 is closer to 2.29 and is what we
// compute. I'm not sure what logic Excel is using, so I'm not going to
// bother investigating for now.
{2.29, spreadsheet.StandardFormat13, "2 20/69"},

{42996.6996269676, spreadsheet.StandardFormat14, "9/18/17"},
{42996.6996269676, spreadsheet.StandardFormat15, "18-Sep-17"},
{42996.6996269676, spreadsheet.StandardFormat16, "18-Sep"},
{42996.6996269676, spreadsheet.StandardFormat17, "Sep-17"},
{42996.6996269676, spreadsheet.StandardFormat18, "4:47 PM"},
{42996.6996269676, spreadsheet.StandardFormat19, "4:47:28 PM"},
{42996.6996269676, spreadsheet.StandardFormat20, "4:47"},
{42996.6996269676, spreadsheet.StandardFormat21, "4:47:28"},
{42996.6996269676, spreadsheet.StandardFormat22, "9/18/17 4:47"},

{1234, spreadsheet.StandardFormat37, "1,234 "},
{-1234, spreadsheet.StandardFormat37, "(1,234)"},
{1234, spreadsheet.StandardFormat38, "1,234 "},
{-1234, spreadsheet.StandardFormat38, "(1,234)"},
{1234, spreadsheet.StandardFormat39, "1,234.00"},
{-1234, spreadsheet.StandardFormat39, "(1,234.00)"},
{1234, spreadsheet.StandardFormat40, "1,234.00"},
{-1234, spreadsheet.StandardFormat40, "(1,234.00)"},

{1.23, spreadsheet.StandardFormat45, "31:12"},
{1.2, spreadsheet.StandardFormat46, "28:48:00"},
{1.234, spreadsheet.StandardFormat47, "36:57.6"},
{1234, spreadsheet.StandardFormat48, "1.2E+3"},
}

func TestDefaultFormats(t *testing.T) {
td := []struct {
Inp float64
Fmt spreadsheet.StandardFormat
Exp string
}{
{0, spreadsheet.StandardFormatGeneral, "0"},
{1.23, spreadsheet.StandardFormatGeneral, "1.23"},
{5, spreadsheet.StandardFormatWholeNumber, "5"},
{5, spreadsheet.StandardFormat2, "5.00"},
{5, spreadsheet.StandardFormat3, "5"},
{5123, spreadsheet.StandardFormat3, "5,123"},
{5123, spreadsheet.StandardFormat4, "5,123.00"},
{0, spreadsheet.StandardFormatPercent, "0%"},
{.25, spreadsheet.StandardFormatPercent, "25%"},
{1.2, spreadsheet.StandardFormatPercent, "120%"},
{0, spreadsheet.StandardFormat10, "0.00%"},
{.2502, spreadsheet.StandardFormat10, "25.02%"},
{1.2, spreadsheet.StandardFormat10, "120.00%"},
}
wb := spreadsheet.New()
sheet := wb.AddSheet()
row := sheet.AddRow()
for _, tc := range td {
for _, tc := range standardFmtTestData {
cell := row.AddCell()
cell.SetNumber(tc.Inp)

Expand All @@ -51,3 +85,20 @@ func TestDefaultFormats(t *testing.T) {
}
}
}

func BenchmarkDefaultFormat(b *testing.B) {
wb := spreadsheet.New()
sheet := wb.AddSheet()
row := sheet.AddRow()
cell := row.AddCell()
cell.SetNumber(1.234)

for _, tc := range standardFmtTestData {
cs := wb.StyleSheet.AddCellStyle()
cs.SetNumberFormatStandard(tc.Fmt)
cell.SetStyle(cs)
for i := 0; i < b.N; i++ {
cell.GetFormattedValue()
}
}
}

0 comments on commit 5f6ed3a

Please sign in to comment.