New object-oriented date module to soon replace Date.pm.
[koha.git] / t / Dates.t
1 #!/bin/perl
2
3 use Test::More tests => 83;
4 BEGIN {
5                 use_ok('C4::Dates');
6 }
7 use_ok( CGI::Carp );
8 CGI::Carp->fatalsToBrowser(0);
9
10 my %thash = (
11           iso  => ['2001-01-01','1989-09-21'],
12         metric => ["01-01-2001",'21-09-1989'],
13            us  => ["01-01-2001",'09-21-1989'],
14           sql  => ['20010101    010101',
15                            '19890921    143907'     ],
16 );
17
18 my @formats = sort keys %thash;
19 diag "\nNote: CGI::Carp may throw an initial error here.  Ignore that.\n";
20 diag "Testing " . scalar(@formats) . " formats.\nTesting no input:\n";
21 my ($today, $today0, $val, $re);
22 ok($today0 = C4::Dates->today(),                "(default) CLASS ->today : $today0" );
23 foreach (@formats) {
24         my $pre = sprintf '(%-6s)', $_;
25         ok($date = C4::Dates->new(),                "$pre Date Creation   : new()");
26         ok($_ eq ($format = $date->format($_)),     "$pre format($_)      : $format" );
27         ok($today  = $date->output(),               "$pre output()        : $today" );
28         ok($today  = $date->today(),                "$pre object->today   : $today" );
29         print "\n";
30 }
31
32 foreach my $format (@formats) {
33         my $pre = sprintf '(%-6s)', $format;
34   foreach my $testval (@{$thash{ $format }}) {
35         ok($date = C4::Dates->new($testval,$format),      "$pre Date Creation   : new('$testval','$format')");
36         ok($re   = $date->regexp,                   "$pre has regexp()" );
37         ok($val  = $date->output(),                 "$pre output()        : $val" );
38         foreach (grep {!/$format/} @formats) {
39                 ok($today = $date->output($_),          "$pre output(" . sprintf("%8s","'$_'") . "): $today");
40         }
41         ok($today  = $date->today(),                "$pre object->today   : $today" );
42         # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today   : $today" );
43         ok($val  = $date->output(),                 "$pre output()        : $val" );
44         # ok($format eq ($format = $date->format()),  "$pre format()        : $format" );
45         print "\n";
46   }
47 }
48
49 diag "done.\n";