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