Skip to content

Commit

Permalink
Merge pull request #112 from tealeg/skm_update_bool
Browse files Browse the repository at this point in the history
Improve boolean check.
  • Loading branch information
tealeg committed May 13, 2015
2 parents 2ba0648 + c3027b8 commit 8662d55
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 8662d55

Please sign in to comment.