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