HTML::Template => HTML::Template::Pro
[koha.git] / t / Dates.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 91;
7 BEGIN {
8         use_ok('C4::Dates', qw(format_date format_date_in_iso));
9 }
10
11 my %thash = (
12           iso  => ['2001-01-01','1989-09-21'],
13         metric => ["01-01-2001",'21-09-1989'],
14            us  => ["01-01-2001",'09-21-1989'],
15           sql  => ['20010101    010101',
16                            '19890921    143907'     ],
17 );
18
19 my ($date, $format, $today, $today0, $val, $re, $syspref);
20 my @formats = sort keys %thash;
21 diag "\n Testing Legacy Functions: format_date and format_date_in_iso";
22 ok($syspref = C4::Dates->new->format(),         "Your system preference is: $syspref");
23 print "\n";
24 foreach (@{$thash{'iso'}}) {
25         ok($val = format_date($_),                  "format_date('$_'): $val"            );
26 }
27 foreach (@{$thash{$syspref}}) {
28         ok($val = format_date_in_iso($_),           "format_date_in_iso('$_'): $val"     );
29 }
30 ok($today0 = C4::Dates->today(),                "(default) CLASS ->today : $today0" );
31 diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n";
32 print "\n";
33 foreach (@formats) {
34         my $pre = sprintf '(%-6s)', $_;
35         ok($date = C4::Dates->new(),                "$pre Date Creation   : new()");
36         ok($_ eq ($format = $date->format($_)),     "$pre format($_)      : $format" );
37         ok($format = $date->visual(),                           "$pre visual()        : $format" );
38         ok($today  = $date->output(),               "$pre output()        : $today" );
39         ok($today  = $date->today(),                "$pre object->today   : $today" );
40         print "\n";
41 }
42
43 diag "\nTesting with inputs:\n";
44 foreach $format (@formats) {
45         my $pre = sprintf '(%-6s)', $format;
46   foreach my $testval (@{$thash{ $format }}) {
47         ok($date = C4::Dates->new($testval,$format),      "$pre Date Creation   : new('$testval','$format')");
48         ok($re   = $date->regexp,                   "$pre has regexp()" );
49         ok($val  = $date->output(),                 "$pre output()        : $val" );
50         foreach (grep {!/$format/} @formats) {
51                 ok($today = $date->output($_),          "$pre output(" . sprintf("%8s","'$_'") . "): $today");
52         }
53         ok($today  = $date->today(),                "$pre object->today   : $today" );
54         # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today   : $today" );
55         ok($val  = $date->output(),                 "$pre output()        : $val" );
56         # ok($format eq ($format = $date->format()),  "$pre format()        : $format" );
57         print "\n";
58   }
59 }
60
61 diag "done.\n";