6 use Test::More tests => 126;
10 use_ok('C4::Dates', qw(format_date format_date_in_iso));
14 my $front = sprintf("%-25s", shift);
15 my $tail = shift || 'FAILED';
16 return "$front : $tail";
20 iso => ['2001-01-01','1989-09-21','1952-01-00', '1989-09-21 13:46:02'],
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',
28 my ($date, $format, $today, $today0, $val, $re, $syspref);
29 my @formats = sort keys %thash;
30 my $fake_syspref_default = 'us';
31 my $fake_syspref = (@ARGV) ? shift : $ENV{KOHA_TEST_DATE_FORMAT};
33 diag "You asked for date format '$fake_syspref'.";
34 unless (scalar grep {/^$fake_syspref$/} @formats) {
35 diag "Warning: Unkown date format '$fake_syspref', reverting to default '$fake_syspref_default'.";
36 $fake_syspref = $fake_syspref_default;
39 $fake_syspref or $fake_syspref = $fake_syspref_default;
40 $C4::Dates::prefformat = $fake_syspref; # So Dates doesn't have to ask the DB anything.
44 In order to run without DB access, this test will substitute '$fake_syspref'
45 as your default date format. Export environmental variable KOHA_TEST_DATE_FORMAT
46 to override this default, or pass the value as an argument to this test script.
48 NOTE: we test for the system handling dd=00 and 00 for TIME values,
49 therefore you *should* see some warnings 'Illegal date specified' related to those.
51 Testing Legacy Functions: format_date and format_date_in_iso
55 ok($syspref = C4::Dates->new->format(), "Your system preference is: $syspref");
57 foreach (@{$thash{'iso'}}) {
58 ok($val = format_date($_), "format_date('$_'): $val" );
60 foreach (@{$thash{$syspref}}) {
61 ok($val = format_date_in_iso($_), "format_date_in_iso('$_'): $val" );
63 ok($today0 = C4::Dates->today(), "(default) CLASS ->today : $today0" );
64 diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n";
67 my $pre = sprintf '(%-6s)', $_;
68 ok($date = C4::Dates->new(), "$pre Date Creation : new()");
69 ok($_ eq ($format = $date->format($_)), "$pre format($_) : " . ($format|| 'FAILED') );
70 ok($format = $date->visual(), "$pre visual() : " . ($format|| 'FAILED') );
71 ok($today = $date->output(), "$pre output() : " . ($today || 'FAILED') );
72 ok($today = $date->today(), "$pre object->today : " . ($today || 'FAILED') );
76 diag "\nTesting with valid inputs:\n";
77 foreach $format (@formats) {
78 my $pre = sprintf '(%-6s)', $format;
79 foreach my $testval (@{$thash{ $format }}) {
80 ok($date = C4::Dates->new($testval,$format), "$pre Date Creation : new('$testval','$format')");
81 ok($re = $date->regexp, "$pre has regexp()" );
82 ok($val = $date->output(), describe("$pre output()", $val) );
83 foreach (grep {!/$format/} @formats) {
84 ok($today = $date->output($_), describe(sprintf("$pre output(%8s)","'$_'"), $today) );
86 ok($today = $date->today(), describe("$pre object->today", $today) );
87 # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today : $today" );
88 ok($val = $date->output(), describe("$pre output()", $val) );
89 # ok($format eq ($format = $date->format()), "$pre format() : $format" );
94 diag "\nTesting object independence from class\n";
95 my $in1 = '12/25/1952'; # us
96 my $in2 = '13/01/2001'; # metric
97 my $d1 = C4::Dates->new($in1, 'us');
98 my $d2 = C4::Dates->new($in2, 'metric');
99 my $out1 = $d1->output('iso');
100 my $out2 = $d2->output('iso');
101 ok($out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)");