launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/testing/checkers/bool_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package checkers_test
     5  
     6  import (
     7  	"errors"
     8  	"os"
     9  
    10  	gc "launchpad.net/gocheck"
    11  
    12  	jc "launchpad.net/juju-core/testing/checkers"
    13  )
    14  
    15  type BoolSuite struct{}
    16  
    17  var _ = gc.Suite(&BoolSuite{})
    18  
    19  func (s *BoolSuite) TestIsTrue(c *gc.C) {
    20  	c.Assert(true, jc.IsTrue)
    21  	c.Assert(false, gc.Not(jc.IsTrue))
    22  
    23  	result, msg := jc.IsTrue.Check([]interface{}{false}, nil)
    24  	c.Assert(result, gc.Equals, false)
    25  	c.Assert(msg, gc.Equals, "")
    26  
    27  	result, msg = jc.IsTrue.Check([]interface{}{"foo"}, nil)
    28  	c.Assert(result, gc.Equals, false)
    29  	c.Check(msg, gc.Equals, `expected type bool, received type string`)
    30  
    31  	result, msg = jc.IsTrue.Check([]interface{}{42}, nil)
    32  	c.Assert(result, gc.Equals, false)
    33  	c.Assert(msg, gc.Equals, `expected type bool, received type int`)
    34  }
    35  
    36  func (s *BoolSuite) TestIsFalse(c *gc.C) {
    37  	c.Assert(false, jc.IsFalse)
    38  	c.Assert(true, gc.Not(jc.IsFalse))
    39  }
    40  
    41  func is42(i int) bool {
    42  	return i == 42
    43  }
    44  
    45  var satisfiesTests = []struct {
    46  	f      interface{}
    47  	arg    interface{}
    48  	result bool
    49  	msg    string
    50  }{{
    51  	f:      is42,
    52  	arg:    42,
    53  	result: true,
    54  }, {
    55  	f:      is42,
    56  	arg:    41,
    57  	result: false,
    58  }, {
    59  	f:      is42,
    60  	arg:    "",
    61  	result: false,
    62  	msg:    "wrong argument type string for func(int) bool",
    63  }, {
    64  	f:      os.IsNotExist,
    65  	arg:    errors.New("foo"),
    66  	result: false,
    67  }, {
    68  	f:      os.IsNotExist,
    69  	arg:    os.ErrNotExist,
    70  	result: true,
    71  }, {
    72  	f:      os.IsNotExist,
    73  	arg:    nil,
    74  	result: false,
    75  }, {
    76  	f:      func(chan int) bool { return true },
    77  	arg:    nil,
    78  	result: true,
    79  }, {
    80  	f:      func(func()) bool { return true },
    81  	arg:    nil,
    82  	result: true,
    83  }, {
    84  	f:      func(interface{}) bool { return true },
    85  	arg:    nil,
    86  	result: true,
    87  }, {
    88  	f:      func(map[string]bool) bool { return true },
    89  	arg:    nil,
    90  	result: true,
    91  }, {
    92  	f:      func(*int) bool { return true },
    93  	arg:    nil,
    94  	result: true,
    95  }, {
    96  	f:      func([]string) bool { return true },
    97  	arg:    nil,
    98  	result: true,
    99  }}
   100  
   101  func (s *BoolSuite) TestSatisfies(c *gc.C) {
   102  	for i, test := range satisfiesTests {
   103  		c.Logf("test %d. %T %T", i, test.f, test.arg)
   104  		result, msg := jc.Satisfies.Check([]interface{}{test.arg, test.f}, nil)
   105  		c.Check(result, gc.Equals, test.result)
   106  		c.Check(msg, gc.Equals, test.msg)
   107  	}
   108  }
   109  
   110  func (s *BoolSuite) TestDeepEquals(c *gc.C) {
   111  	for i, test := range deepEqualTests {
   112  		c.Logf("test %d. %v == %v is %v", i, test.a, test.b, test.eq)
   113  		result, msg := jc.DeepEquals.Check([]interface{}{test.a, test.b}, nil)
   114  		c.Check(result, gc.Equals, test.eq)
   115  		if test.eq {
   116  			c.Check(msg, gc.Equals, "")
   117  		} else {
   118  			c.Check(msg, gc.Not(gc.Equals), "")
   119  		}
   120  	}
   121  }