Skip to content

Commit

Permalink
Merge pull request juju#34 from juju/100-fix-panic-testing-zero-inter…
Browse files Browse the repository at this point in the history
…face-value-to-bool

testing/checkers: fix panic in IsTrue
  • Loading branch information
davecheney committed Sep 10, 2014
2 parents 7348fa1 + 87af103 commit 422bbb7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions checkers/bool.go
@@ -1,4 +1,4 @@
// Copyright 2013 Canonical Ltd.
// Copyright 2011 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package checkers
Expand Down Expand Up @@ -27,7 +27,9 @@ var IsFalse gc.Checker = gc.Not(IsTrue)
func (checker *isTrueChecker) Check(params []interface{}, names []string) (result bool, error string) {

value := reflect.ValueOf(params[0])

if !value.IsValid() {
return false, fmt.Sprintf("expected type bool, received %s", value)
}
switch value.Kind() {
case reflect.Bool:
return value.Bool(), ""
Expand Down
8 changes: 6 additions & 2 deletions checkers/bool_test.go
Expand Up @@ -31,11 +31,15 @@ func (s *BoolSuite) TestIsTrue(c *gc.C) {
result, msg = jc.IsTrue.Check([]interface{}{42}, nil)
c.Assert(result, gc.Equals, false)
c.Assert(msg, gc.Equals, `expected type bool, received type int`)

result, msg = jc.IsTrue.Check([]interface{}{nil}, nil)
c.Assert(result, gc.Equals, false)
c.Assert(msg, gc.Equals, `expected type bool, received <invalid Value>`)
}

func (s *BoolSuite) TestIsFalse(c *gc.C) {
c.Assert(false, jc.IsFalse)
c.Assert(true, gc.Not(jc.IsFalse))
c.Check(false, jc.IsFalse)
c.Check(true, gc.Not(jc.IsFalse))
}

func is42(i int) bool {
Expand Down

0 comments on commit 422bbb7

Please sign in to comment.