File: rand_date.t

package info (click to toggle)
libdata-random-perl 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 688 kB
  • sloc: perl: 406; sh: 11; makefile: 10
file content (75 lines) | stat: -rw-r--r-- 1,577 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use strict;
use warnings;
use Test::More;

use Data::Random qw( rand_date );
use Time::Piece;

my $today = localtime;

my $min_date = Time::Piece->strptime($today->ymd, "%Y-%m-%d");
my $max_date = $min_date->add_years(1);

my @tests = (
  {
    name => 'no args',
    args => {},
    min  => $today->ymd,
    max  => $today->add_years(1)->ymd,
  },
  {
    name => 'min',
    args => {
      min => '1979-08-02',
    },
    min => '1979-08-02',
    max => '1980-08-02',
  },
  {
    name => 'min && max',
    args => {
      min => '2015-3-1',
      max => '2015-5-10',
    },
    min => '2015-03-01',
    max => '2015-05-10',
  },
  {
    name => 'min now',
    args => {
      min => 'now',
    },
    min => $today->ymd,
    max => $today->add_years(1)->ymd,
  },
  {
    name => 'max now',
    args => {
      min => '2014-07-11',
      max => 'now',
    },
    min => '2014-07-11',
    max => $today->ymd,
  },
);

for my $test (@tests) {
  note "Running $test->{name}";

  # creating Time::Piece objects from 'min' and 'max' values.
  my $min_date = Time::Piece->strptime($test->{min},"%Y-%m-%d");
  my $max_date = Time::Piece->strptime($test->{max},"%Y-%m-%d");

  for ( 0..999 ) {
    my $rand_date = rand_date(%{$test->{args}});
    note "Result: $rand_date";
    like($rand_date, qr/^\d{4}-\d{2}-\d{2}$/, 'rand_date format');

    my $result   = Time::Piece->strptime($rand_date,  "%Y-%m-%d");
    cmp_ok($result, '>=', $min_date, 'rand_date not smaller than minimum');
    cmp_ok($result, '<=', $max_date, 'rand_date not bigger than maximum');
  }
}

done_testing;