Dates.pm - IMPORTANT fix for dmy_array (part of obejct, not class)
[koha.git] / t / Dates.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 92;
7 BEGIN {
8         use_ok('C4::Dates', qw(format_date format_date_in_iso));
9 }
10
11 sub describe ($$) {
12         my $front = sprintf("%-25s", shift);
13         my $tail = shift || 'FAILED';
14         return  "$front : $tail";
15 }
16
17 my %thash = (
18           iso  => ['2001-01-01','1989-09-21'],
19         metric => ["01-01-2001",'21-09-1989'],
20            us  => ["01-01-2001",'09-21-1989'],
21           sql  => ['20010101    010101',
22                            '19890921    143907'     ],
23 );
24
25 my ($date, $format, $today, $today0, $val, $re, $syspref);
26 my @formats = sort keys %thash;
27 diag "\n Testing Legacy Functions: format_date and format_date_in_iso";
28 ok($syspref = C4::Dates->new->format(),         "Your system preference is: $syspref");
29 print "\n";
30 foreach (@{$thash{'iso'}}) {
31         ok($val = format_date($_),                  "format_date('$_'): $val"            );
32 }
33 foreach (@{$thash{$syspref}}) {
34         ok($val = format_date_in_iso($_),           "format_date_in_iso('$_'): $val"     );
35 }
36 ok($today0 = C4::Dates->today(),                "(default) CLASS ->today : $today0" );
37 diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n";
38 print "\n";
39 foreach (@formats) {
40         my $pre = sprintf '(%-6s)', $_;
41         ok($date = C4::Dates->new(),                "$pre Date Creation   : new()");
42         ok($_ eq ($format = $date->format($_)),     "$pre format($_)      : " . ($format|| 'FAILED') );
43         ok($format = $date->visual(),                           "$pre visual()        : " . ($format|| 'FAILED') );
44         ok($today  = $date->output(),               "$pre output()        : " . ($today || 'FAILED') );
45         ok($today  = $date->today(),                "$pre object->today   : " . ($today || 'FAILED') );
46         print "\n";
47 }
48
49 diag "\nTesting with valid inputs:\n";
50 foreach $format (@formats) {
51         my $pre = sprintf '(%-6s)', $format;
52   foreach my $testval (@{$thash{ $format }}) {
53         ok($date = C4::Dates->new($testval,$format),         "$pre Date Creation   : new('$testval','$format')");
54         ok($re   = $date->regexp,                            "$pre has regexp()" );
55         ok($val  = $date->output(),                 describe("$pre output()", $val) );
56         foreach (grep {!/$format/} @formats) {
57                 ok($today = $date->output($_),          describe(sprintf("$pre output(%8s)","'$_'"), $today) );
58         }
59         ok($today  = $date->today(),                describe("$pre object->today", $today) );
60         # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today   : $today" );
61         ok($val  = $date->output(),                 describe("$pre output()", $val) );
62         # ok($format eq ($format = $date->format()),  "$pre format()        : $format" );
63         print "\n";
64   }
65 }
66
67 diag "\nTesting object independence from class\n";
68 my $in1 = '12/25/1952'; # us
69 my $in2 = '13/01/2001'; # metric
70 my $d1 = C4::Dates->new($in1, 'us');
71 my $d2 = C4::Dates->new($in2, 'metric');
72 my $out1 = $d1->output('iso');
73 my $out2 = $d2->output('iso');
74 ok($out1 ne $out2,                             "subsequent constructors get different dataspace ($out1 != $out2)");
75 diag "done.\n";