From c3027b84ef450ee8ee5025c6272150aac28bddb1 Mon Sep 17 00:00:00 2001 From: Shawn Milochik Date: Sat, 18 Apr 2015 09:37:41 -0400 Subject: [PATCH] Proposal for #106. Change the behavior of Cell.Bool to be technically more correct. --- cell.go | 11 ++++++++++- cell_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cell.go b/cell.go index 43c37895..40983f5f 100644 --- a/cell.go +++ b/cell.go @@ -155,7 +155,16 @@ func (c *Cell) SetBool(b bool) { // TODO: Determine if the current return value is // appropriate for types other than CellTypeBool. func (c *Cell) Bool() bool { - return c.Value == "1" + // If bool, just return the value. + if c.cellType == CellTypeBool { + return c.Value == "1" + } + // If numeric, base it on a non-zero. + if c.cellType == CellTypeNumeric { + return c.Value != "0" + } + // Return whether there's an empty string. + return c.Value != "" } // SetFormula sets the format string for a cell. diff --git a/cell_test.go b/cell_test.go index 4044438d..773ac2b8 100644 --- a/cell_test.go +++ b/cell_test.go @@ -295,3 +295,27 @@ func (s *CellSuite) TestOddInput(c *C) { cell.numFmt = "@" c.Assert(cell.String(), Equals, odd) } + +// TestBool tests basic Bool getting and setting booleans. +func (s *CellSuite) TestBool(c *C) { + cell := Cell{} + cell.SetBool(true) + c.Assert(cell.Value, Equals, "1") + c.Assert(cell.Bool(), Equals, true) + cell.SetBool(false) + c.Assert(cell.Value, Equals, "0") + c.Assert(cell.Bool(), Equals, false) +} + +// TestStringBool tests calling Bool on a non CellTypeBool value. +func (s *CellSuite) TestStringBool(c *C) { + cell := Cell{} + cell.SetInt(0) + c.Assert(cell.Bool(), Equals, false) + cell.SetInt(1) + c.Assert(cell.Bool(), Equals, true) + cell.SetString("") + c.Assert(cell.Bool(), Equals, false) + cell.SetString("0") + c.Assert(cell.Bool(), Equals, true) +}