launchpad.net/gocheck@v0.0.0-20140225173054-000000000087/checkers_test.go (about)

     1  package gocheck_test
     2  
     3  import (
     4  	"errors"
     5  	"launchpad.net/gocheck"
     6  	"reflect"
     7  	"runtime"
     8  )
     9  
    10  type CheckersS struct{}
    11  
    12  var _ = gocheck.Suite(&CheckersS{})
    13  
    14  func testInfo(c *gocheck.C, checker gocheck.Checker, name string, paramNames []string) {
    15  	info := checker.Info()
    16  	if info.Name != name {
    17  		c.Fatalf("Got name %s, expected %s", info.Name, name)
    18  	}
    19  	if !reflect.DeepEqual(info.Params, paramNames) {
    20  		c.Fatalf("Got param names %#v, expected %#v", info.Params, paramNames)
    21  	}
    22  }
    23  
    24  func testCheck(c *gocheck.C, checker gocheck.Checker, result bool, error string, params ...interface{}) ([]interface{}, []string) {
    25  	info := checker.Info()
    26  	if len(params) != len(info.Params) {
    27  		c.Fatalf("unexpected param count in test; expected %d got %d", len(info.Params), len(params))
    28  	}
    29  	names := append([]string{}, info.Params...)
    30  	result_, error_ := checker.Check(params, names)
    31  	if result_ != result || error_ != error {
    32  		c.Fatalf("%s.Check(%#v) returned (%#v, %#v) rather than (%#v, %#v)",
    33  			info.Name, params, result_, error_, result, error)
    34  	}
    35  	return params, names
    36  }
    37  
    38  func (s *CheckersS) TestComment(c *gocheck.C) {
    39  	bug := gocheck.Commentf("a %d bc", 42)
    40  	comment := bug.CheckCommentString()
    41  	if comment != "a 42 bc" {
    42  		c.Fatalf("Commentf returned %#v", comment)
    43  	}
    44  }
    45  
    46  func (s *CheckersS) TestIsNil(c *gocheck.C) {
    47  	testInfo(c, gocheck.IsNil, "IsNil", []string{"value"})
    48  
    49  	testCheck(c, gocheck.IsNil, true, "", nil)
    50  	testCheck(c, gocheck.IsNil, false, "", "a")
    51  
    52  	testCheck(c, gocheck.IsNil, true, "", (chan int)(nil))
    53  	testCheck(c, gocheck.IsNil, false, "", make(chan int))
    54  	testCheck(c, gocheck.IsNil, true, "", (error)(nil))
    55  	testCheck(c, gocheck.IsNil, false, "", errors.New(""))
    56  	testCheck(c, gocheck.IsNil, true, "", ([]int)(nil))
    57  	testCheck(c, gocheck.IsNil, false, "", make([]int, 1))
    58  	testCheck(c, gocheck.IsNil, false, "", int(0))
    59  }
    60  
    61  func (s *CheckersS) TestNotNil(c *gocheck.C) {
    62  	testInfo(c, gocheck.NotNil, "NotNil", []string{"value"})
    63  
    64  	testCheck(c, gocheck.NotNil, false, "", nil)
    65  	testCheck(c, gocheck.NotNil, true, "", "a")
    66  
    67  	testCheck(c, gocheck.NotNil, false, "", (chan int)(nil))
    68  	testCheck(c, gocheck.NotNil, true, "", make(chan int))
    69  	testCheck(c, gocheck.NotNil, false, "", (error)(nil))
    70  	testCheck(c, gocheck.NotNil, true, "", errors.New(""))
    71  	testCheck(c, gocheck.NotNil, false, "", ([]int)(nil))
    72  	testCheck(c, gocheck.NotNil, true, "", make([]int, 1))
    73  }
    74  
    75  func (s *CheckersS) TestNot(c *gocheck.C) {
    76  	testInfo(c, gocheck.Not(gocheck.IsNil), "Not(IsNil)", []string{"value"})
    77  
    78  	testCheck(c, gocheck.Not(gocheck.IsNil), false, "", nil)
    79  	testCheck(c, gocheck.Not(gocheck.IsNil), true, "", "a")
    80  }
    81  
    82  type simpleStruct struct {
    83  	i int
    84  }
    85  
    86  func (s *CheckersS) TestEquals(c *gocheck.C) {
    87  	testInfo(c, gocheck.Equals, "Equals", []string{"obtained", "expected"})
    88  
    89  	// The simplest.
    90  	testCheck(c, gocheck.Equals, true, "", 42, 42)
    91  	testCheck(c, gocheck.Equals, false, "", 42, 43)
    92  
    93  	// Different native types.
    94  	testCheck(c, gocheck.Equals, false, "", int32(42), int64(42))
    95  
    96  	// With nil.
    97  	testCheck(c, gocheck.Equals, false, "", 42, nil)
    98  
    99  	// Slices
   100  	testCheck(c, gocheck.Equals, false, "runtime error: comparing uncomparable type []uint8", []byte{1, 2}, []byte{1, 2})
   101  
   102  	// Struct values
   103  	testCheck(c, gocheck.Equals, true, "", simpleStruct{1}, simpleStruct{1})
   104  	testCheck(c, gocheck.Equals, false, "", simpleStruct{1}, simpleStruct{2})
   105  
   106  	// Struct pointers
   107  	testCheck(c, gocheck.Equals, false, "", &simpleStruct{1}, &simpleStruct{1})
   108  	testCheck(c, gocheck.Equals, false, "", &simpleStruct{1}, &simpleStruct{2})
   109  }
   110  
   111  func (s *CheckersS) TestDeepEquals(c *gocheck.C) {
   112  	testInfo(c, gocheck.DeepEquals, "DeepEquals", []string{"obtained", "expected"})
   113  
   114  	// The simplest.
   115  	testCheck(c, gocheck.DeepEquals, true, "", 42, 42)
   116  	testCheck(c, gocheck.DeepEquals, false, "", 42, 43)
   117  
   118  	// Different native types.
   119  	testCheck(c, gocheck.DeepEquals, false, "", int32(42), int64(42))
   120  
   121  	// With nil.
   122  	testCheck(c, gocheck.DeepEquals, false, "", 42, nil)
   123  
   124  	// Slices
   125  	testCheck(c, gocheck.DeepEquals, true, "", []byte{1, 2}, []byte{1, 2})
   126  	testCheck(c, gocheck.DeepEquals, false, "", []byte{1, 2}, []byte{1, 3})
   127  
   128  	// Struct values
   129  	testCheck(c, gocheck.DeepEquals, true, "", simpleStruct{1}, simpleStruct{1})
   130  	testCheck(c, gocheck.DeepEquals, false, "", simpleStruct{1}, simpleStruct{2})
   131  
   132  	// Struct pointers
   133  	testCheck(c, gocheck.DeepEquals, true, "", &simpleStruct{1}, &simpleStruct{1})
   134  	testCheck(c, gocheck.DeepEquals, false, "", &simpleStruct{1}, &simpleStruct{2})
   135  }
   136  
   137  func (s *CheckersS) TestHasLen(c *gocheck.C) {
   138  	testInfo(c, gocheck.HasLen, "HasLen", []string{"obtained", "n"})
   139  
   140  	testCheck(c, gocheck.HasLen, true, "", "abcd", 4)
   141  	testCheck(c, gocheck.HasLen, true, "", []int{1, 2}, 2)
   142  	testCheck(c, gocheck.HasLen, false, "", []int{1, 2}, 3)
   143  
   144  	testCheck(c, gocheck.HasLen, false, "n must be an int", []int{1, 2}, "2")
   145  	testCheck(c, gocheck.HasLen, false, "obtained value type has no length", nil, 2)
   146  }
   147  
   148  func (s *CheckersS) TestErrorMatches(c *gocheck.C) {
   149  	testInfo(c, gocheck.ErrorMatches, "ErrorMatches", []string{"value", "regex"})
   150  
   151  	testCheck(c, gocheck.ErrorMatches, false, "Error value is nil", nil, "some error")
   152  	testCheck(c, gocheck.ErrorMatches, false, "Value is not an error", 1, "some error")
   153  	testCheck(c, gocheck.ErrorMatches, true, "", errors.New("some error"), "some error")
   154  	testCheck(c, gocheck.ErrorMatches, true, "", errors.New("some error"), "so.*or")
   155  
   156  	// Verify params mutation
   157  	params, names := testCheck(c, gocheck.ErrorMatches, false, "", errors.New("some error"), "other error")
   158  	c.Assert(params[0], gocheck.Equals, "some error")
   159  	c.Assert(names[0], gocheck.Equals, "error")
   160  }
   161  
   162  func (s *CheckersS) TestMatches(c *gocheck.C) {
   163  	testInfo(c, gocheck.Matches, "Matches", []string{"value", "regex"})
   164  
   165  	// Simple matching
   166  	testCheck(c, gocheck.Matches, true, "", "abc", "abc")
   167  	testCheck(c, gocheck.Matches, true, "", "abc", "a.c")
   168  
   169  	// Must match fully
   170  	testCheck(c, gocheck.Matches, false, "", "abc", "ab")
   171  	testCheck(c, gocheck.Matches, false, "", "abc", "bc")
   172  
   173  	// String()-enabled values accepted
   174  	testCheck(c, gocheck.Matches, true, "", reflect.ValueOf("abc"), "a.c")
   175  	testCheck(c, gocheck.Matches, false, "", reflect.ValueOf("abc"), "a.d")
   176  
   177  	// Some error conditions.
   178  	testCheck(c, gocheck.Matches, false, "Obtained value is not a string and has no .String()", 1, "a.c")
   179  	testCheck(c, gocheck.Matches, false, "Can't compile regex: error parsing regexp: missing closing ]: `[c$`", "abc", "a[c")
   180  }
   181  
   182  func (s *CheckersS) TestPanics(c *gocheck.C) {
   183  	testInfo(c, gocheck.Panics, "Panics", []string{"function", "expected"})
   184  
   185  	// Some errors.
   186  	testCheck(c, gocheck.Panics, false, "Function has not panicked", func() bool { return false }, "BOOM")
   187  	testCheck(c, gocheck.Panics, false, "Function must take zero arguments", 1, "BOOM")
   188  
   189  	// Plain strings.
   190  	testCheck(c, gocheck.Panics, true, "", func() { panic("BOOM") }, "BOOM")
   191  	testCheck(c, gocheck.Panics, false, "", func() { panic("KABOOM") }, "BOOM")
   192  	testCheck(c, gocheck.Panics, true, "", func() bool { panic("BOOM") }, "BOOM")
   193  
   194  	// Error values.
   195  	testCheck(c, gocheck.Panics, true, "", func() { panic(errors.New("BOOM")) }, errors.New("BOOM"))
   196  	testCheck(c, gocheck.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))
   197  
   198  	type deep struct{ i int }
   199  	// Deep value
   200  	testCheck(c, gocheck.Panics, true, "", func() { panic(&deep{99}) }, &deep{99})
   201  
   202  	// Verify params/names mutation
   203  	params, names := testCheck(c, gocheck.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))
   204  	c.Assert(params[0], gocheck.ErrorMatches, "KABOOM")
   205  	c.Assert(names[0], gocheck.Equals, "panic")
   206  
   207  	// Verify a nil panic
   208  	testCheck(c, gocheck.Panics, true, "", func() { panic(nil) }, nil)
   209  	testCheck(c, gocheck.Panics, false, "", func() { panic(nil) }, "NOPE")
   210  }
   211  
   212  func (s *CheckersS) TestPanicMatches(c *gocheck.C) {
   213  	testInfo(c, gocheck.PanicMatches, "PanicMatches", []string{"function", "expected"})
   214  
   215  	// Error matching.
   216  	testCheck(c, gocheck.PanicMatches, true, "", func() { panic(errors.New("BOOM")) }, "BO.M")
   217  	testCheck(c, gocheck.PanicMatches, false, "", func() { panic(errors.New("KABOOM")) }, "BO.M")
   218  
   219  	// Some errors.
   220  	testCheck(c, gocheck.PanicMatches, false, "Function has not panicked", func() bool { return false }, "BOOM")
   221  	testCheck(c, gocheck.PanicMatches, false, "Function must take zero arguments", 1, "BOOM")
   222  
   223  	// Plain strings.
   224  	testCheck(c, gocheck.PanicMatches, true, "", func() { panic("BOOM") }, "BO.M")
   225  	testCheck(c, gocheck.PanicMatches, false, "", func() { panic("KABOOM") }, "BOOM")
   226  	testCheck(c, gocheck.PanicMatches, true, "", func() bool { panic("BOOM") }, "BO.M")
   227  
   228  	// Verify params/names mutation
   229  	params, names := testCheck(c, gocheck.PanicMatches, false, "", func() { panic(errors.New("KABOOM")) }, "BOOM")
   230  	c.Assert(params[0], gocheck.Equals, "KABOOM")
   231  	c.Assert(names[0], gocheck.Equals, "panic")
   232  
   233  	// Verify a nil panic
   234  	testCheck(c, gocheck.PanicMatches, false, "Panic value is not a string or an error", func() { panic(nil) }, "")
   235  }
   236  
   237  func (s *CheckersS) TestFitsTypeOf(c *gocheck.C) {
   238  	testInfo(c, gocheck.FitsTypeOf, "FitsTypeOf", []string{"obtained", "sample"})
   239  
   240  	// Basic types
   241  	testCheck(c, gocheck.FitsTypeOf, true, "", 1, 0)
   242  	testCheck(c, gocheck.FitsTypeOf, false, "", 1, int64(0))
   243  
   244  	// Aliases
   245  	testCheck(c, gocheck.FitsTypeOf, false, "", 1, errors.New(""))
   246  	testCheck(c, gocheck.FitsTypeOf, false, "", "error", errors.New(""))
   247  	testCheck(c, gocheck.FitsTypeOf, true, "", errors.New("error"), errors.New(""))
   248  
   249  	// Structures
   250  	testCheck(c, gocheck.FitsTypeOf, false, "", 1, simpleStruct{})
   251  	testCheck(c, gocheck.FitsTypeOf, false, "", simpleStruct{42}, &simpleStruct{})
   252  	testCheck(c, gocheck.FitsTypeOf, true, "", simpleStruct{42}, simpleStruct{})
   253  	testCheck(c, gocheck.FitsTypeOf, true, "", &simpleStruct{42}, &simpleStruct{})
   254  
   255  	// Some bad values
   256  	testCheck(c, gocheck.FitsTypeOf, false, "Invalid sample value", 1, interface{}(nil))
   257  	testCheck(c, gocheck.FitsTypeOf, false, "", interface{}(nil), 0)
   258  }
   259  
   260  func (s *CheckersS) TestImplements(c *gocheck.C) {
   261  	testInfo(c, gocheck.Implements, "Implements", []string{"obtained", "ifaceptr"})
   262  
   263  	var e error
   264  	var re runtime.Error
   265  	testCheck(c, gocheck.Implements, true, "", errors.New(""), &e)
   266  	testCheck(c, gocheck.Implements, false, "", errors.New(""), &re)
   267  
   268  	// Some bad values
   269  	testCheck(c, gocheck.Implements, false, "ifaceptr should be a pointer to an interface variable", 0, errors.New(""))
   270  	testCheck(c, gocheck.Implements, false, "ifaceptr should be a pointer to an interface variable", 0, interface{}(nil))
   271  	testCheck(c, gocheck.Implements, false, "", interface{}(nil), &e)
   272  }