1package shakers 2 3import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/go-check/check" 9) 10 11func init() { 12 check.Suite(&CommonCheckerS{}) 13} 14 15type CommonCheckerS struct{} 16 17func (s *CommonCheckerS) TestEqualsInfo(c *check.C) { 18 testInfo(c, Equals, "Equals", []string{"obtained", "expected"}) 19} 20 21func (s *CommonCheckerS) TestEqualsValidsEquals(c *check.C) { 22 myTime, err := time.Parse("2006-01-02", "2018-01-01") 23 if err != nil { 24 c.Fatal(err) 25 } 26 27 testCheck(c, Equals, true, "", "string", "string") 28 testCheck(c, Equals, true, "", 0, 0) 29 testCheck(c, Equals, true, "", anEqualer{1}, anEqualer{1}) 30 testCheck(c, Equals, true, "", myTime, myTime) 31 testCheck(c, Equals, true, "", myTime, "2018-01-01") 32 testCheck(c, Equals, true, "", myTime, "2018-01-01T00:00:00Z") 33} 34 35func (s *CommonCheckerS) TestEqualsValidsDifferent(c *check.C) { 36 myTime1, err := time.Parse("2006-01-02", "2018-01-01") 37 if err != nil { 38 c.Fatal(err) 39 } 40 myTime2, err := time.Parse("2006-01-02", "2018-01-02") 41 if err != nil { 42 c.Fatal(err) 43 } 44 45 testCheck(c, Equals, false, "", "string", "astring") 46 testCheck(c, Equals, false, "", 0, 1) 47 testCheck(c, Equals, false, "", anEqualer{1}, anEqualer{0}) 48 testCheck(c, Equals, false, "", myTime1, myTime2) 49 testCheck(c, Equals, false, "", myTime1, "2018-01-02") 50 testCheck(c, Equals, false, "", myTime1, "2018-01-02T00:00:00Z") 51} 52 53func (s *CommonCheckerS) TestEqualsInvalids(c *check.C) { 54 // Incompatible type time.Time 55 testCheck(c, Equals, false, "obtained value and expected value have not the same type.", "2015-01-01", time.Now()) 56 testCheck(c, Equals, false, "expected must be a Time struct, or parseable.", time.Now(), 0) 57 58 // Incompatible type Equaler 59 testCheck(c, Equals, false, "expected value must be an Equaler - implementing Equal(Equaler).", anEqualer{0}, 0) 60 61 testCheck(c, Equals, false, "obtained value and expected value have not the same type.", 0, anEqualer{0}) 62 63 // Nils 64 testCheck(c, Equals, false, "obtained value and expected value have not the same type.", nil, 0) 65 testCheck(c, Equals, false, "obtained value and expected value have not the same type.", 0, nil) 66} 67 68type anEqualer struct { 69 value int 70} 71 72func (a anEqualer) Equal(b Equaler) bool { 73 if bEqualer, ok := b.(anEqualer); ok { 74 return a.value == bEqualer.value 75 } 76 return false 77} 78 79func (s *CommonCheckerS) TestGreaterThanInfo(c *check.C) { 80 testInfo(c, GreaterThan, "GreaterThan", []string{"obtained", "expected"}) 81} 82 83func (s *CommonCheckerS) TestGreaterThanValidsGreater(c *check.C) { 84 myTime, err := time.Parse("2006-01-02", "2018-01-01") 85 if err != nil { 86 c.Fatal(err) 87 } 88 89 testCheck(c, GreaterThan, true, "", 2, 1) 90 testCheck(c, GreaterThan, true, "", 0, -1) 91 testCheck(c, GreaterThan, true, "", float32(2), float32(0)) 92 testCheck(c, GreaterThan, true, "", float64(2), float64(0)) 93 testCheck(c, GreaterThan, true, "", byte(2), byte(1)) 94 testCheck(c, GreaterThan, true, "", int(2), int(1)) 95 testCheck(c, GreaterThan, true, "", int8(2), int8(1)) 96 testCheck(c, GreaterThan, true, "", int16(2), int16(1)) 97 testCheck(c, GreaterThan, true, "", int32(2), int32(1)) 98 testCheck(c, GreaterThan, true, "", int64(2), int64(1)) 99 testCheck(c, GreaterThan, true, "", uint(2), uint(1)) 100 testCheck(c, GreaterThan, true, "", uint8(2), uint8(1)) 101 testCheck(c, GreaterThan, true, "", uint16(2), uint16(1)) 102 testCheck(c, GreaterThan, true, "", uint32(2), uint32(1)) 103 testCheck(c, GreaterThan, true, "", uint64(2), uint64(1)) 104 testCheck(c, GreaterThan, true, "", myTime, "2017-12-31") 105} 106 107func (s *CommonCheckerS) TestGreaterThanValidsDifferent(c *check.C) { 108 myTime, err := time.Parse("2006-01-02", "2018-01-01") 109 if err != nil { 110 c.Fatal(err) 111 } 112 113 testCheck(c, GreaterThan, false, "", 1, 2) 114 testCheck(c, GreaterThan, false, "", -1, 0) 115 testCheck(c, GreaterThan, false, "", float32(0), float32(2)) 116 testCheck(c, GreaterThan, false, "", float64(0), float64(2)) 117 testCheck(c, GreaterThan, false, "", byte(1), byte(2)) 118 testCheck(c, GreaterThan, false, "", int(1), int(2)) 119 testCheck(c, GreaterThan, false, "", int8(1), int8(2)) 120 testCheck(c, GreaterThan, false, "", int16(1), int16(2)) 121 testCheck(c, GreaterThan, false, "", int32(1), int32(2)) 122 testCheck(c, GreaterThan, false, "", int64(1), int64(2)) 123 testCheck(c, GreaterThan, false, "", uint(1), uint(2)) 124 testCheck(c, GreaterThan, false, "", uint8(1), uint8(2)) 125 testCheck(c, GreaterThan, false, "", uint16(1), uint16(2)) 126 testCheck(c, GreaterThan, false, "", uint32(1), uint32(2)) 127 testCheck(c, GreaterThan, false, "", uint64(1), uint64(2)) 128 testCheck(c, GreaterThan, false, "", myTime, "2018-01-01") 129 testCheck(c, GreaterThan, false, "", myTime, "2018-01-02") 130} 131 132func (s *CommonCheckerS) TestGreaterThanInvalids(c *check.C) { 133 myTime, err := time.Parse("2006-01-02", "2018-01-01") 134 if err != nil { 135 c.Fatal(err) 136 } 137 138 testCheck(c, GreaterThan, false, "obtained value and expected value have not the same type.", 1, "string") 139 testCheck(c, GreaterThan, false, "obtained value and expected value have not the same type.", "string", 1) 140 testCheck(c, GreaterThan, false, "obtained value and expected value have not the same type.", 1, complex128(1+2i)) 141 testCheck(c, GreaterThan, false, "expected must be a Time struct, or parseable.", myTime, 1) 142 testCheck(c, GreaterThan, false, "expected must be a Time struct, or parseable.", myTime, "invalid") 143} 144 145func (s *CommonCheckerS) TestGreaterOrEqualThanInfo(c *check.C) { 146 testInfo(c, GreaterOrEqualThan, "GreaterOrEqualThan", []string{"obtained", "expected"}) 147} 148 149func (s *CommonCheckerS) TestGreaterOrEqualThanValidsGreaterOrEquals(c *check.C) { 150 myTime, err := time.Parse("2006-01-02", "2018-01-01") 151 if err != nil { 152 c.Fatal(err) 153 } 154 155 testCheck(c, GreaterOrEqualThan, true, "", 2, 1) 156 testCheck(c, GreaterOrEqualThan, true, "", 0, -1) 157 testCheck(c, GreaterOrEqualThan, true, "", float32(2), float32(0)) 158 testCheck(c, GreaterOrEqualThan, true, "", float64(2), float64(0)) 159 testCheck(c, GreaterOrEqualThan, true, "", byte(2), byte(1)) 160 testCheck(c, GreaterOrEqualThan, true, "", int(2), int(1)) 161 testCheck(c, GreaterOrEqualThan, true, "", int8(2), int8(1)) 162 testCheck(c, GreaterOrEqualThan, true, "", int16(2), int16(1)) 163 testCheck(c, GreaterOrEqualThan, true, "", int32(2), int32(1)) 164 testCheck(c, GreaterOrEqualThan, true, "", int64(2), int64(1)) 165 testCheck(c, GreaterOrEqualThan, true, "", uint(2), uint(1)) 166 testCheck(c, GreaterOrEqualThan, true, "", uint8(2), uint8(1)) 167 testCheck(c, GreaterOrEqualThan, true, "", uint16(2), uint16(1)) 168 testCheck(c, GreaterOrEqualThan, true, "", uint32(2), uint32(1)) 169 testCheck(c, GreaterOrEqualThan, true, "", uint64(2), uint64(1)) 170 testCheck(c, GreaterOrEqualThan, true, "", myTime, "2017-12-31") 171 testCheck(c, GreaterOrEqualThan, true, "", 2, 2) 172 testCheck(c, GreaterOrEqualThan, true, "", -1, -1) 173 testCheck(c, GreaterOrEqualThan, true, "", float32(2), float32(2)) 174 testCheck(c, GreaterOrEqualThan, true, "", float64(2), float64(2)) 175 testCheck(c, GreaterOrEqualThan, true, "", byte(2), byte(2)) 176 testCheck(c, GreaterOrEqualThan, true, "", int(2), int(2)) 177 testCheck(c, GreaterOrEqualThan, true, "", int8(2), int8(2)) 178 testCheck(c, GreaterOrEqualThan, true, "", int16(2), int16(2)) 179 testCheck(c, GreaterOrEqualThan, true, "", int32(2), int32(2)) 180 testCheck(c, GreaterOrEqualThan, true, "", int64(2), int64(2)) 181 testCheck(c, GreaterOrEqualThan, true, "", uint(2), uint(2)) 182 testCheck(c, GreaterOrEqualThan, true, "", uint8(2), uint8(2)) 183 testCheck(c, GreaterOrEqualThan, true, "", uint16(2), uint16(2)) 184 testCheck(c, GreaterOrEqualThan, true, "", uint32(2), uint32(2)) 185 testCheck(c, GreaterOrEqualThan, true, "", uint64(2), uint64(2)) 186 // testCheck(c, GreaterOrEqualThan, true, "", myTime, "2018-01-01") 187} 188 189func (s *CommonCheckerS) TestGreaterOrEqualThanValidsDifferent(c *check.C) { 190 myTime, err := time.Parse("2006-01-02", "2018-01-01") 191 if err != nil { 192 c.Fatal(err) 193 } 194 195 testCheck(c, GreaterOrEqualThan, false, "", 1, 2) 196 testCheck(c, GreaterOrEqualThan, false, "", -1, 0) 197 testCheck(c, GreaterOrEqualThan, false, "", float32(0), float32(2)) 198 testCheck(c, GreaterOrEqualThan, false, "", float64(0), float64(2)) 199 testCheck(c, GreaterOrEqualThan, false, "", byte(1), byte(2)) 200 testCheck(c, GreaterOrEqualThan, false, "", int(1), int(2)) 201 testCheck(c, GreaterOrEqualThan, false, "", int8(1), int8(2)) 202 testCheck(c, GreaterOrEqualThan, false, "", int16(1), int16(2)) 203 testCheck(c, GreaterOrEqualThan, false, "", int32(1), int32(2)) 204 testCheck(c, GreaterOrEqualThan, false, "", int64(1), int64(2)) 205 testCheck(c, GreaterOrEqualThan, false, "", uint(1), uint(2)) 206 testCheck(c, GreaterOrEqualThan, false, "", uint8(1), uint8(2)) 207 testCheck(c, GreaterOrEqualThan, false, "", uint16(1), uint16(2)) 208 testCheck(c, GreaterOrEqualThan, false, "", uint32(1), uint32(2)) 209 testCheck(c, GreaterOrEqualThan, false, "", uint64(1), uint64(2)) 210 testCheck(c, GreaterOrEqualThan, false, "", myTime, "2018-01-01") 211 testCheck(c, GreaterOrEqualThan, false, "", myTime, "2018-01-02") 212} 213 214func (s *CommonCheckerS) TestGreaterOrEqualThanInvalids(c *check.C) { 215 myTime, err := time.Parse("2006-01-02", "2018-01-01") 216 if err != nil { 217 c.Fatal(err) 218 } 219 220 testCheck(c, GreaterOrEqualThan, false, "obtained value and expected value have not the same type.", 1, "string") 221 testCheck(c, GreaterOrEqualThan, false, "obtained value and expected value have not the same type.", "string", 1) 222 testCheck(c, GreaterOrEqualThan, false, "obtained value and expected value have not the same type.", 1, complex128(1+2i)) 223 testCheck(c, GreaterOrEqualThan, false, "expected must be a Time struct, or parseable.", myTime, 1) 224 testCheck(c, GreaterOrEqualThan, false, "expected must be a Time struct, or parseable.", myTime, "invalid") 225} 226 227func (s *CommonCheckerS) TestLessThanInfo(c *check.C) { 228 testInfo(c, LessThan, "LessThan", []string{"obtained", "expected"}) 229} 230 231func (s *CommonCheckerS) TestLessThanValidsLess(c *check.C) { 232 myTime, err := time.Parse("2006-01-02", "2018-01-01") 233 if err != nil { 234 c.Fatal(err) 235 } 236 237 testCheck(c, LessThan, true, "", 1, 2) 238 testCheck(c, LessThan, true, "", -1, 0) 239 testCheck(c, LessThan, true, "", float32(0), float32(2)) 240 testCheck(c, LessThan, true, "", float64(0), float64(2)) 241 testCheck(c, LessThan, true, "", byte(1), byte(2)) 242 testCheck(c, LessThan, true, "", int(1), int(2)) 243 testCheck(c, LessThan, true, "", int8(1), int8(2)) 244 testCheck(c, LessThan, true, "", int16(1), int16(2)) 245 testCheck(c, LessThan, true, "", int32(1), int32(2)) 246 testCheck(c, LessThan, true, "", int64(1), int64(2)) 247 testCheck(c, LessThan, true, "", uint(1), uint(2)) 248 testCheck(c, LessThan, true, "", uint8(1), uint8(2)) 249 testCheck(c, LessThan, true, "", uint16(1), uint16(2)) 250 testCheck(c, LessThan, true, "", uint32(1), uint32(2)) 251 testCheck(c, LessThan, true, "", uint64(1), uint64(2)) 252 testCheck(c, LessThan, true, "", myTime, "2018-01-02") 253} 254 255func (s *CommonCheckerS) TestLessThanValidsDifferent(c *check.C) { 256 myTime, err := time.Parse("2006-01-02", "2018-01-01") 257 if err != nil { 258 c.Fatal(err) 259 } 260 261 testCheck(c, LessThan, false, "", 2, 1) 262 testCheck(c, LessThan, false, "", 0, -1) 263 testCheck(c, LessThan, false, "", float32(2), float32(0)) 264 testCheck(c, LessThan, false, "", float64(2), float64(0)) 265 testCheck(c, LessThan, false, "", byte(2), byte(1)) 266 testCheck(c, LessThan, false, "", int(2), int(1)) 267 testCheck(c, LessThan, false, "", int8(2), int8(1)) 268 testCheck(c, LessThan, false, "", int16(2), int16(1)) 269 testCheck(c, LessThan, false, "", int32(2), int32(1)) 270 testCheck(c, LessThan, false, "", int64(2), int64(1)) 271 testCheck(c, LessThan, false, "", uint(2), uint(1)) 272 testCheck(c, LessThan, false, "", uint8(2), uint8(1)) 273 testCheck(c, LessThan, false, "", uint16(2), uint16(1)) 274 testCheck(c, LessThan, false, "", uint32(2), uint32(1)) 275 testCheck(c, LessThan, false, "", uint64(2), uint64(1)) 276 testCheck(c, LessThan, false, "", myTime, "2018-01-01") 277 testCheck(c, LessThan, false, "", myTime, "2017-12-31") 278} 279 280func (s *CommonCheckerS) TestLessThanInvalids(c *check.C) { 281 myTime, err := time.Parse("2006-01-02", "2018-01-01") 282 if err != nil { 283 c.Fatal(err) 284 } 285 286 testCheck(c, LessThan, false, "obtained value and expected value have not the same type.", 1, "string") 287 testCheck(c, LessThan, false, "obtained value and expected value have not the same type.", "string", 1) 288 testCheck(c, LessThan, false, "obtained value and expected value have not the same type.", 1, complex128(1+2i)) 289 testCheck(c, LessThan, false, "expected must be a Time struct, or parseable.", myTime, 1) 290 testCheck(c, LessThan, false, "expected must be a Time struct, or parseable.", myTime, "invalid") 291} 292 293func (s *CommonCheckerS) TestLessEqualThanInfo(c *check.C) { 294 testInfo(c, LessOrEqualThan, "LessOrEqualThan", []string{"obtained", "expected"}) 295} 296 297func (s *CommonCheckerS) TestLessOrEqualThanValidsLessOrEquals(c *check.C) { 298 myTime, err := time.Parse("2006-01-02", "2018-01-01") 299 if err != nil { 300 c.Fatal(err) 301 } 302 303 testCheck(c, LessOrEqualThan, true, "", 1, 2) 304 testCheck(c, LessOrEqualThan, true, "", -1, 0) 305 testCheck(c, LessOrEqualThan, true, "", float32(0), float32(2)) 306 testCheck(c, LessOrEqualThan, true, "", float64(0), float64(2)) 307 testCheck(c, LessOrEqualThan, true, "", byte(1), byte(2)) 308 testCheck(c, LessOrEqualThan, true, "", int(1), int(2)) 309 testCheck(c, LessOrEqualThan, true, "", int8(1), int8(2)) 310 testCheck(c, LessOrEqualThan, true, "", int16(1), int16(2)) 311 testCheck(c, LessOrEqualThan, true, "", int32(1), int32(2)) 312 testCheck(c, LessOrEqualThan, true, "", int64(1), int64(2)) 313 testCheck(c, LessOrEqualThan, true, "", uint(1), uint(2)) 314 testCheck(c, LessOrEqualThan, true, "", uint8(1), uint8(2)) 315 testCheck(c, LessOrEqualThan, true, "", uint16(1), uint16(2)) 316 testCheck(c, LessOrEqualThan, true, "", uint32(1), uint32(2)) 317 testCheck(c, LessOrEqualThan, true, "", uint64(1), uint64(2)) 318 testCheck(c, LessOrEqualThan, true, "", myTime, "2018-01-02") 319 testCheck(c, LessOrEqualThan, true, "", 1, 1) 320 testCheck(c, LessOrEqualThan, true, "", -1, -1) 321 testCheck(c, LessOrEqualThan, true, "", float32(2), float32(2)) 322 testCheck(c, LessOrEqualThan, true, "", float64(2), float64(2)) 323 testCheck(c, LessOrEqualThan, true, "", byte(2), byte(2)) 324 testCheck(c, LessOrEqualThan, true, "", int(2), int(2)) 325 testCheck(c, LessOrEqualThan, true, "", int8(2), int8(2)) 326 testCheck(c, LessOrEqualThan, true, "", int16(2), int16(2)) 327 testCheck(c, LessOrEqualThan, true, "", int32(2), int32(2)) 328 testCheck(c, LessOrEqualThan, true, "", int64(2), int64(2)) 329 testCheck(c, LessOrEqualThan, true, "", uint(2), uint(2)) 330 testCheck(c, LessOrEqualThan, true, "", uint8(2), uint8(2)) 331 testCheck(c, LessOrEqualThan, true, "", uint16(2), uint16(2)) 332 testCheck(c, LessOrEqualThan, true, "", uint32(2), uint32(2)) 333 testCheck(c, LessOrEqualThan, true, "", uint64(2), uint64(2)) 334 // testCheck(c, LessOrEqualThan, true, "", myTime, "2018-01-02") 335} 336 337func (s *CommonCheckerS) TestLessOrEqualThanValidsDifferent(c *check.C) { 338 myTime, err := time.Parse("2006-01-02", "2018-01-01") 339 if err != nil { 340 c.Fatal(err) 341 } 342 343 testCheck(c, LessOrEqualThan, false, "", 2, 1) 344 testCheck(c, LessOrEqualThan, false, "", 0, -1) 345 testCheck(c, LessOrEqualThan, false, "", float32(2), float32(0)) 346 testCheck(c, LessOrEqualThan, false, "", float64(2), float64(0)) 347 testCheck(c, LessOrEqualThan, false, "", byte(2), byte(1)) 348 testCheck(c, LessOrEqualThan, false, "", int(2), int(1)) 349 testCheck(c, LessOrEqualThan, false, "", int8(2), int8(1)) 350 testCheck(c, LessOrEqualThan, false, "", int16(2), int16(1)) 351 testCheck(c, LessOrEqualThan, false, "", int32(2), int32(1)) 352 testCheck(c, LessOrEqualThan, false, "", int64(2), int64(1)) 353 testCheck(c, LessOrEqualThan, false, "", uint(2), uint(1)) 354 testCheck(c, LessOrEqualThan, false, "", uint8(2), uint8(1)) 355 testCheck(c, LessOrEqualThan, false, "", uint16(2), uint16(1)) 356 testCheck(c, LessOrEqualThan, false, "", uint32(2), uint32(1)) 357 testCheck(c, LessOrEqualThan, false, "", uint64(2), uint64(1)) 358 testCheck(c, LessOrEqualThan, false, "", myTime, "2018-01-01") 359 testCheck(c, LessOrEqualThan, false, "", myTime, "2017-12-31") 360} 361 362func (s *CommonCheckerS) TestLessOrEqualThanInvalids(c *check.C) { 363 myTime, err := time.Parse("2006-01-02", "2018-01-01") 364 if err != nil { 365 c.Fatal(err) 366 } 367 368 testCheck(c, LessOrEqualThan, false, "obtained value and expected value have not the same type.", 1, "string") 369 testCheck(c, LessOrEqualThan, false, "obtained value and expected value have not the same type.", "string", 1) 370 testCheck(c, LessOrEqualThan, false, "obtained value and expected value have not the same type.", 1, complex128(1+2i)) 371 testCheck(c, LessOrEqualThan, false, "expected must be a Time struct, or parseable.", myTime, 1) 372 testCheck(c, LessOrEqualThan, false, "expected must be a Time struct, or parseable.", myTime, "invalid") 373} 374 375func Test(t *testing.T) { 376 check.TestingT(t) 377} 378 379func testInfo(c *check.C, checker check.Checker, name string, paramNames []string) { 380 info := checker.Info() 381 if info.Name != name { 382 c.Fatalf("Got name %s, expected %s", info.Name, name) 383 } 384 if !reflect.DeepEqual(info.Params, paramNames) { 385 c.Fatalf("Got param names %#v, expected %#v", info.Params, paramNames) 386 } 387} 388 389func testCheck(c *check.C, checker check.Checker, expectedResult bool, expectedError string, params ...interface{}) ([]interface{}, []string) { 390 info := checker.Info() 391 if len(params) != len(info.Params) { 392 c.Fatalf("unexpected param count in test; expected %d got %d", len(info.Params), len(params)) 393 } 394 names := append([]string{}, info.Params...) 395 result, error := checker.Check(params, names) 396 if result != expectedResult || error != expectedError { 397 c.Fatalf("%s.Check(%#v) returned (%#v, %#v) rather than (%#v, %#v)", 398 info.Name, params, result, error, expectedResult, expectedError) 399 } 400 return params, names 401} 402