launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/testing/checkers/checker_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  	"testing"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	jc "launchpad.net/juju-core/testing/checkers"
    12  )
    13  
    14  func Test(t *testing.T) { gc.TestingT(t) }
    15  
    16  type CheckerSuite struct{}
    17  
    18  var _ = gc.Suite(&CheckerSuite{})
    19  
    20  func (s *CheckerSuite) TestHasPrefix(c *gc.C) {
    21  	c.Assert("foo bar", jc.HasPrefix, "foo")
    22  	c.Assert("foo bar", gc.Not(jc.HasPrefix), "omg")
    23  }
    24  
    25  func (s *CheckerSuite) TestHasSuffix(c *gc.C) {
    26  	c.Assert("foo bar", jc.HasSuffix, "bar")
    27  	c.Assert("foo bar", gc.Not(jc.HasSuffix), "omg")
    28  }
    29  
    30  func (s *CheckerSuite) TestContains(c *gc.C) {
    31  	c.Assert("foo bar baz", jc.Contains, "foo")
    32  	c.Assert("foo bar baz", jc.Contains, "bar")
    33  	c.Assert("foo bar baz", jc.Contains, "baz")
    34  	c.Assert("foo bar baz", gc.Not(jc.Contains), "omg")
    35  }
    36  
    37  func (s *CheckerSuite) TestSameContents(c *gc.C) {
    38  	//// positive cases ////
    39  
    40  	// same
    41  	c.Check(
    42  		[]int{1, 2, 3}, jc.SameContents,
    43  		[]int{1, 2, 3})
    44  
    45  	// empty
    46  	c.Check(
    47  		[]int{}, jc.SameContents,
    48  		[]int{})
    49  
    50  	// single
    51  	c.Check(
    52  		[]int{1}, jc.SameContents,
    53  		[]int{1})
    54  
    55  	// different order
    56  	c.Check(
    57  		[]int{1, 2, 3}, jc.SameContents,
    58  		[]int{3, 2, 1})
    59  
    60  	// multiple copies of same
    61  	c.Check(
    62  		[]int{1, 1, 2}, jc.SameContents,
    63  		[]int{2, 1, 1})
    64  
    65  	type test struct {
    66  		s string
    67  		i int
    68  	}
    69  
    70  	// test structs
    71  	c.Check(
    72  		[]test{{"a", 1}, {"b", 2}}, jc.SameContents,
    73  		[]test{{"b", 2}, {"a", 1}})
    74  
    75  	//// negative cases ////
    76  
    77  	// different contents
    78  	c.Check(
    79  		[]int{1, 3, 2, 5}, gc.Not(jc.SameContents),
    80  		[]int{5, 2, 3, 4})
    81  
    82  	// different size slices
    83  	c.Check(
    84  		[]int{1, 2, 3}, gc.Not(jc.SameContents),
    85  		[]int{1, 2})
    86  
    87  	// different counts of same items
    88  	c.Check(
    89  		[]int{1, 1, 2}, gc.Not(jc.SameContents),
    90  		[]int{1, 2, 2})
    91  
    92  	/// Error cases ///
    93  	//  note: for these tests, we can't use gc.Not, since Not passes the error value through
    94  	// and checks with a non-empty error always count as failed
    95  	// Oddly, there doesn't seem to actually be a way to check for an error from a Checker.
    96  
    97  	// different type
    98  	res, err := jc.SameContents.Check([]interface{}{
    99  		[]string{"1", "2"},
   100  		[]int{1, 2},
   101  	}, []string{})
   102  	c.Check(res, jc.IsFalse)
   103  	c.Check(err, gc.Not(gc.Equals), "")
   104  
   105  	// obtained not a slice
   106  	res, err = jc.SameContents.Check([]interface{}{
   107  		"test",
   108  		[]int{1},
   109  	}, []string{})
   110  	c.Check(res, jc.IsFalse)
   111  	c.Check(err, gc.Not(gc.Equals), "")
   112  
   113  	// expected not a slice
   114  	res, err = jc.SameContents.Check([]interface{}{
   115  		[]int{1},
   116  		"test",
   117  	}, []string{})
   118  	c.Check(res, jc.IsFalse)
   119  	c.Check(err, gc.Not(gc.Equals), "")
   120  }