~ubuntu-branches/ubuntu/utopic/golang-testify/utopic-proposed

« back to all changes in this revision

Viewing changes to assert/assertions_test.go

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2014-07-15 16:31:44 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140715163144-a386kng0pp20yxcz
Tags: 0.0~git20140717-1
* New upstream release.
* Set branch in Vcs-Git header.
* Add Built-Using header.

Show diffs side-by-side

added added

removed removed

Lines of Context:
87
87
        if !Equal(mockT, nil, nil) {
88
88
                t.Error("Equal should return true")
89
89
        }
 
90
        if !Equal(mockT, int32(123), int64(123)) {
 
91
                t.Error("Equal should return true")
 
92
        }
 
93
        if !Equal(mockT, int64(123), uint64(123)) {
 
94
                t.Error("Equal should return true")
 
95
        }
90
96
 
91
97
}
92
98
 
269
275
func TestEqual_Funcs(t *testing.T) {
270
276
 
271
277
        type f func() int
272
 
        var f1 f = func() int { return 1 }
273
 
        var f2 f = func() int { return 2 }
274
 
 
275
 
        var f1_copy f = f1
276
 
 
277
 
        Equal(t, f1_copy, f1, "Funcs are the same and should be considered equal")
 
278
        f1 := func() int { return 1 }
 
279
        f2 := func() int { return 2 }
 
280
 
 
281
        f1Copy := f1
 
282
 
 
283
        Equal(t, f1Copy, f1, "Funcs are the same and should be considered equal")
278
284
        NotEqual(t, f1, f2, "f1 and f2 are different")
279
285
 
280
286
}
284
290
        mockT := new(testing.T)
285
291
 
286
292
        // start with a nil error
287
 
        var err error = nil
 
293
        var err error
288
294
 
289
295
        True(t, NoError(mockT, err), "NoError should return True for nil arg")
290
296
 
291
297
        // now set an error
292
 
        err = errors.New("Some error")
 
298
        err = errors.New("some error")
293
299
 
294
300
        False(t, NoError(mockT, err), "NoError with error should return False")
295
301
 
300
306
        mockT := new(testing.T)
301
307
 
302
308
        // start with a nil error
303
 
        var err error = nil
 
309
        var err error
304
310
 
305
311
        False(t, Error(mockT, err), "Error should return False for nil arg")
306
312
 
307
313
        // now set an error
308
 
        err = errors.New("Some error")
 
314
        err = errors.New("some error")
309
315
 
310
316
        True(t, Error(mockT, err), "Error with error should return True")
311
317
 
315
321
        mockT := new(testing.T)
316
322
 
317
323
        // start with a nil error
318
 
        var err error = nil
 
324
        var err error
319
325
        False(t, EqualError(mockT, err, ""),
320
326
                "EqualError should return false for nil arg")
321
327
 
322
328
        // now set an error
323
 
        err = errors.New("Some error")
 
329
        err = errors.New("some error")
324
330
        False(t, EqualError(mockT, err, "Not some error"),
325
331
                "EqualError should return false for different error string")
326
 
        True(t, EqualError(mockT, err, "Some error"),
 
332
        True(t, EqualError(mockT, err, "some error"),
327
333
                "EqualError should return true")
328
334
}
329
335
 
330
336
func Test_isEmpty(t *testing.T) {
331
337
 
332
 
        ch_with_value := make(chan struct{}, 1)
333
 
        ch_with_value <- struct{}{}
 
338
        chWithValue := make(chan struct{}, 1)
 
339
        chWithValue <- struct{}{}
334
340
 
335
341
        True(t, isEmpty(""))
336
342
        True(t, isEmpty(nil))
337
343
        True(t, isEmpty([]string{}))
338
344
        True(t, isEmpty(0))
 
345
        True(t, isEmpty(int32(0)))
 
346
        True(t, isEmpty(int64(0)))
339
347
        True(t, isEmpty(false))
340
348
        True(t, isEmpty(map[string]string{}))
341
349
        True(t, isEmpty(new(time.Time)))
346
354
        False(t, isEmpty(1))
347
355
        False(t, isEmpty(true))
348
356
        False(t, isEmpty(map[string]string{"Hello": "World"}))
349
 
        False(t, isEmpty(ch_with_value))
 
357
        False(t, isEmpty(chWithValue))
350
358
 
351
359
}
352
360
 
353
361
func TestEmpty(t *testing.T) {
354
362
 
355
363
        mockT := new(testing.T)
356
 
        ch_with_value := make(chan struct{}, 1)
357
 
        ch_with_value <- struct{}{}
 
364
        chWithValue := make(chan struct{}, 1)
 
365
        chWithValue <- struct{}{}
358
366
 
359
367
        True(t, Empty(mockT, ""), "Empty string is empty")
360
368
        True(t, Empty(mockT, nil), "Nil is empty")
368
376
        False(t, Empty(mockT, []string{"something"}), "Non empty string array is not empty")
369
377
        False(t, Empty(mockT, 1), "Non-zero int value is not empty")
370
378
        False(t, Empty(mockT, true), "True value is not empty")
371
 
        False(t, Empty(mockT, ch_with_value), "Channel with values is not empty")
 
379
        False(t, Empty(mockT, chWithValue), "Channel with values is not empty")
372
380
}
373
381
 
374
382
func TestNotEmpty(t *testing.T) {
375
383
 
376
384
        mockT := new(testing.T)
377
 
        ch_with_value := make(chan struct{}, 1)
378
 
        ch_with_value <- struct{}{}
 
385
        chWithValue := make(chan struct{}, 1)
 
386
        chWithValue <- struct{}{}
379
387
 
380
388
        False(t, NotEmpty(mockT, ""), "Empty string is empty")
381
389
        False(t, NotEmpty(mockT, nil), "Nil is empty")
389
397
        True(t, NotEmpty(mockT, []string{"something"}), "Non empty string array is not empty")
390
398
        True(t, NotEmpty(mockT, 1), "Non-zero int value is not empty")
391
399
        True(t, NotEmpty(mockT, true), "True value is not empty")
392
 
        True(t, NotEmpty(mockT, ch_with_value), "Channel with values is not empty")
 
400
        True(t, NotEmpty(mockT, chWithValue), "Channel with values is not empty")
 
401
}
 
402
 
 
403
func Test_getLen(t *testing.T) {
 
404
        falseCases := []interface{}{
 
405
                nil,
 
406
                0,
 
407
                true,
 
408
                false,
 
409
                'A',
 
410
                struct{}{},
 
411
        }
 
412
        for _, v := range falseCases {
 
413
                ok, l := getLen(v)
 
414
                False(t, ok, "Expected getLen fail to get length of %#v", v)
 
415
                Equal(t, 0, l, "getLen should return 0 for %#v", v)
 
416
        }
 
417
 
 
418
        ch := make(chan int, 5)
 
419
        ch <- 1
 
420
        ch <- 2
 
421
        ch <- 3
 
422
        trueCases := []struct {
 
423
                v interface{}
 
424
                l int
 
425
        }{
 
426
                {[]int{1, 2, 3}, 3},
 
427
                {[...]int{1, 2, 3}, 3},
 
428
                {"ABC", 3},
 
429
                {map[int]int{1: 2, 2: 4, 3: 6}, 3},
 
430
                {ch, 3},
 
431
 
 
432
                {[]int{}, 0},
 
433
                {map[int]int{}, 0},
 
434
                {make(chan int), 0},
 
435
 
 
436
                {[]int(nil), 0},
 
437
                {map[int]int(nil), 0},
 
438
                {(chan int)(nil), 0},
 
439
        }
 
440
 
 
441
        for _, c := range trueCases {
 
442
                ok, l := getLen(c.v)
 
443
                True(t, ok, "Expected getLen success to get length of %#v", c.v)
 
444
                Equal(t, c.l, l)
 
445
        }
 
446
}
 
447
 
 
448
func TestLen(t *testing.T) {
 
449
        mockT := new(testing.T)
 
450
 
 
451
        False(t, Len(mockT, nil, 0), "nil does not have length")
 
452
        False(t, Len(mockT, 0, 0), "int does not have length")
 
453
        False(t, Len(mockT, true, 0), "true does not have length")
 
454
        False(t, Len(mockT, false, 0), "false does not have length")
 
455
        False(t, Len(mockT, 'A', 0), "Rune does not have length")
 
456
        False(t, Len(mockT, struct{}{}, 0), "Struct does not have length")
 
457
 
 
458
        ch := make(chan int, 5)
 
459
        ch <- 1
 
460
        ch <- 2
 
461
        ch <- 3
 
462
 
 
463
        cases := []struct {
 
464
                v interface{}
 
465
                l int
 
466
        }{
 
467
                {[]int{1, 2, 3}, 3},
 
468
                {[...]int{1, 2, 3}, 3},
 
469
                {"ABC", 3},
 
470
                {map[int]int{1: 2, 2: 4, 3: 6}, 3},
 
471
                {ch, 3},
 
472
 
 
473
                {[]int{}, 0},
 
474
                {map[int]int{}, 0},
 
475
                {make(chan int), 0},
 
476
 
 
477
                {[]int(nil), 0},
 
478
                {map[int]int(nil), 0},
 
479
                {(chan int)(nil), 0},
 
480
        }
 
481
 
 
482
        for _, c := range cases {
 
483
                True(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l)
 
484
        }
393
485
}
394
486
 
395
487
func TestWithinDuration(t *testing.T) {
410
502
        False(t, WithinDuration(mockT, a, b, -11*time.Second), "A 10s difference is not within a 9s time difference")
411
503
        False(t, WithinDuration(mockT, b, a, -11*time.Second), "A 10s difference is not within a 9s time difference")
412
504
}
 
505
 
 
506
func TestInDelta(t *testing.T) {
 
507
        mockT := new(testing.T)
 
508
 
 
509
        True(t, InDelta(mockT, 1.001, 1, 0.01), "|1.001 - 1| <= 0.01")
 
510
        True(t, InDelta(mockT, 1, 1.001, 0.01), "|1 - 1.001| <= 0.01")
 
511
        True(t, InDelta(mockT, 1, 2, 1), "|1 - 2| <= 1")
 
512
        False(t, InDelta(mockT, 1, 2, 0.5), "Expected |1 - 2| <= 0.5 to fail")
 
513
        False(t, InDelta(mockT, 2, 1, 0.5), "Expected |2 - 1| <= 0.5 to fail")
 
514
        False(t, InDelta(mockT, "", nil, 1), "Expected non numerals to fail")
 
515
 
 
516
        cases := []struct {
 
517
                a, b  interface{}
 
518
                delta float64
 
519
        }{
 
520
                {uint8(2), uint8(1), 1},
 
521
                {uint16(2), uint16(1), 1},
 
522
                {uint32(2), uint32(1), 1},
 
523
                {uint64(2), uint64(1), 1},
 
524
 
 
525
                {int(2), int(1), 1},
 
526
                {int8(2), int8(1), 1},
 
527
                {int16(2), int16(1), 1},
 
528
                {int32(2), int32(1), 1},
 
529
                {int64(2), int64(1), 1},
 
530
 
 
531
                {float32(2), float32(1), 1},
 
532
                {float64(2), float64(1), 1},
 
533
        }
 
534
 
 
535
        for _, tc := range cases {
 
536
                True(t, InDelta(mockT, tc.a, tc.b, tc.delta), "Expected |%V - %V| <= %v", tc.a, tc.b, tc.delta)
 
537
        }
 
538
}