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