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'],
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 diag "\n Testing Legacy Functions: format_date and format_date_in_iso";
31 ok($syspref = C4::Dates->new->format(), "Your system preference is: $syspref");
33 foreach (@{$thash{'iso'}}) {
34 ok($val = format_date($_), "format_date('$_'): $val" );
36 foreach (@{$thash{$syspref}}) {
37 ok($val = format_date_in_iso($_), "format_date_in_iso('$_'): $val" );
39 ok($today0 = C4::Dates->today(), "(default) CLASS ->today : $today0" );
40 diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n";
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') );
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) );
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" );
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)");