Lines 4-9
use strict;
Link Here
|
4 |
use warnings; |
4 |
use warnings; |
5 |
|
5 |
|
6 |
use Test::More tests => 327; |
6 |
use Test::More tests => 327; |
|
|
7 |
use Test::Warn; |
7 |
|
8 |
|
8 |
BEGIN { |
9 |
BEGIN { |
9 |
use FindBin; |
10 |
use FindBin; |
Lines 11-16
BEGIN {
Link Here
|
11 |
use_ok( 'C4::Dates', qw(format_date format_date_in_iso) ); |
12 |
use_ok( 'C4::Dates', qw(format_date format_date_in_iso) ); |
12 |
} |
13 |
} |
13 |
|
14 |
|
|
|
15 |
sub isBadDate { |
16 |
my ($tdate,$tformat) = @_; |
17 |
my $retval = 0; |
18 |
if (index('iso,us,metric',$tformat)>=0) { |
19 |
if ($tdate =~ /^00*-/ || $tdate =~ /-00*-/ || |
20 |
$tdate =~ /-00*$/ || $tdate =~ /-00* /) { |
21 |
|
22 |
$retval = 1; |
23 |
} |
24 |
} |
25 |
elsif ($tformat eq 'sql') { |
26 |
if ($tdate =~ /^0000/ || $tdate =~ /^....00/ || |
27 |
$tdate =~ /^......00/) { |
28 |
|
29 |
$retval = 1; |
30 |
} |
31 |
} |
32 |
return $retval; |
33 |
} |
34 |
|
14 |
sub describe ($$) { |
35 |
sub describe ($$) { |
15 |
my $front = sprintf( "%-25s", shift ); |
36 |
my $front = sprintf( "%-25s", shift ); |
16 |
my $tail = shift || 'FAILED'; |
37 |
my $tail = shift || 'FAILED'; |
Lines 41-71
if ($fake_syspref) {
Link Here
|
41 |
$fake_syspref or $fake_syspref = $fake_syspref_default; |
62 |
$fake_syspref or $fake_syspref = $fake_syspref_default; |
42 |
$C4::Dates::prefformat = $fake_syspref; # So Dates doesn't have to ask the DB anything. |
63 |
$C4::Dates::prefformat = $fake_syspref; # So Dates doesn't have to ask the DB anything. |
43 |
|
64 |
|
44 |
diag <<EndOfDiag; |
65 |
print <<EndOfDiag; |
45 |
|
66 |
|
46 |
In order to run without DB access, this test will substitute '$fake_syspref' |
67 |
In order to run without DB access, this test will substitute |
47 |
as your default date format. Export environmental variable KOHA_TEST_DATE_FORMAT |
68 |
'$fake_syspref' as your default date format. Export |
48 |
to override this default, or pass the value as an argument to this test script. |
69 |
environmental variable KOHA_TEST_DATE_FORMAT to override this |
|
|
70 |
default, or pass the value as an argument to this test script. |
49 |
|
71 |
|
50 |
NOTE: we test for the system handling dd=00 and 00 for TIME values, therefore |
72 |
NOTE: we test for the system handling dd=00 and 00 for TIME |
51 |
you SHOULD see some warnings like: |
73 |
values, but you should not see any warnings. |
52 |
Illegal date specified (year = 1952, month = 1, day = 00) at t/Dates.t ... |
|
|
53 |
|
54 |
Testing Legacy Functions: format_date and format_date_in_iso |
55 |
|
74 |
|
56 |
EndOfDiag |
75 |
EndOfDiag |
57 |
|
76 |
|
|
|
77 |
# Testing Legacy Functions: format_date and format_date_in_iso |
58 |
ok( $syspref = C4::Dates->new->format(), "Your system preference is: $syspref" ); |
78 |
ok( $syspref = C4::Dates->new->format(), "Your system preference is: $syspref" ); |
59 |
print "\n"; |
|
|
60 |
foreach ( @{ $thash{'iso'} } ) { |
79 |
foreach ( @{ $thash{'iso'} } ) { |
61 |
ok( $val = format_date($_), "format_date('$_'): $val" ); |
80 |
if (isBadDate($_,'iso')==1) { |
|
|
81 |
warning_like { $val = format_date($_); } qr/Illegal date/, |
82 |
"format_date('$_'): Warning as expected"; |
83 |
} |
84 |
else { |
85 |
ok( $val = format_date($_), "format_date('$_'): $val" ); |
86 |
} |
62 |
} |
87 |
} |
63 |
foreach ( @{ $thash{$syspref} } ) { |
88 |
foreach ( @{ $thash{$syspref} } ) { |
64 |
ok( $val = format_date_in_iso($_), "format_date_in_iso('$_'): $val" ); |
89 |
if (isBadDate($_,$syspref)==1) { |
|
|
90 |
warning_like { $val = format_date_in_iso($_); } qr/Illegal date/, |
91 |
"format_date_in_iso('$_'): Warning as expected"; |
92 |
} |
93 |
else { |
94 |
ok( $val = format_date_in_iso($_), "format_date_in_iso('$_'): $val" ); |
95 |
} |
65 |
} |
96 |
} |
66 |
ok( $today0 = C4::Dates->today(), "(default) CLASS ->today : $today0" ); |
97 |
ok( $today0 = C4::Dates->today(), "(default) CLASS ->today : $today0" ); |
67 |
diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n"; |
98 |
#diag "Testing " . scalar(@formats) . " formats.\nTesting no input (defaults):"; |
68 |
print "\n"; |
|
|
69 |
foreach (@formats) { |
99 |
foreach (@formats) { |
70 |
my $pre = sprintf '(%-6s)', $_; |
100 |
my $pre = sprintf '(%-6s)', $_; |
71 |
ok( $date = C4::Dates->new(), "$pre Date Creation : new()" ); |
101 |
ok( $date = C4::Dates->new(), "$pre Date Creation : new()" ); |
Lines 73-86
foreach (@formats) {
Link Here
|
73 |
ok( $format = $date->visual(), "$pre visual() : " . ( $format || 'FAILED' ) ); |
103 |
ok( $format = $date->visual(), "$pre visual() : " . ( $format || 'FAILED' ) ); |
74 |
ok( $today = $date->output(), "$pre output() : " . ( $today || 'FAILED' ) ); |
104 |
ok( $today = $date->output(), "$pre output() : " . ( $today || 'FAILED' ) ); |
75 |
ok( $today = $date->today(), "$pre object->today : " . ( $today || 'FAILED' ) ); |
105 |
ok( $today = $date->today(), "$pre object->today : " . ( $today || 'FAILED' ) ); |
76 |
print "\n"; |
|
|
77 |
} |
106 |
} |
78 |
|
107 |
|
79 |
diag "\nTesting with valid inputs:\n"; |
108 |
#diag "Testing with valid inputs:"; |
80 |
foreach $format (@formats) { |
109 |
foreach $format (@formats) { |
81 |
my $pre = sprintf '(%-6s)', $format; |
110 |
my $pre = sprintf '(%-6s)', $format; |
82 |
foreach my $testval ( @{ $thash{$format} } ) { |
111 |
foreach my $testval ( @{ $thash{$format} } ) { |
83 |
ok( $date = C4::Dates->new( $testval, $format ), "$pre Date Creation : new('$testval','$format')" ); |
112 |
if (isBadDate($testval,$format)==1) { |
|
|
113 |
warning_like { $date = C4::Dates->new( $testval, $format ) } |
114 |
qr/Illegal date/, |
115 |
"$pre Date Creation : new('$testval','$format') -- Warning as expected."; |
116 |
} |
117 |
else { |
118 |
ok( $date = C4::Dates->new( $testval, $format ), "$pre Date Creation : new('$testval','$format')" ); |
119 |
} |
84 |
ok( $re = $date->regexp, "$pre has regexp()" ); |
120 |
ok( $re = $date->regexp, "$pre has regexp()" ); |
85 |
ok( $testval =~ /^$re$/, "$pre has regexp() match $testval" ); |
121 |
ok( $testval =~ /^$re$/, "$pre has regexp() match $testval" ); |
86 |
ok( $val = $date->output(), describe( "$pre output()", $val ) ); |
122 |
ok( $val = $date->output(), describe( "$pre output()", $val ) ); |
Lines 98-108
foreach $format (@formats) {
Link Here
|
98 |
ok( $val = $date->output(), describe( "$pre output()", $val ) ); |
134 |
ok( $val = $date->output(), describe( "$pre output()", $val ) ); |
99 |
|
135 |
|
100 |
# ok($format eq ($format = $date->format()), "$pre format() : $format" ); |
136 |
# ok($format eq ($format = $date->format()), "$pre format() : $format" ); |
101 |
print "\n"; |
|
|
102 |
} |
137 |
} |
103 |
} |
138 |
} |
104 |
|
139 |
|
105 |
diag "\nTesting object independence from class\n"; |
140 |
#diag "Testing object independence from class"; |
106 |
my $in1 = '12/25/1952'; # us |
141 |
my $in1 = '12/25/1952'; # us |
107 |
my $in2 = '13/01/2001'; # metric |
142 |
my $in2 = '13/01/2001'; # metric |
108 |
my $d1 = C4::Dates->new( $in1, 'us' ); |
143 |
my $d1 = C4::Dates->new( $in1, 'us' ); |
Lines 110-113
my $d2 = C4::Dates->new( $in2, 'metric' );
Link Here
|
110 |
my $out1 = $d1->output('iso'); |
145 |
my $out1 = $d1->output('iso'); |
111 |
my $out2 = $d2->output('iso'); |
146 |
my $out2 = $d2->output('iso'); |
112 |
ok( $out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)" ); |
147 |
ok( $out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)" ); |
113 |
diag "done.\n"; |
|
|
114 |
- |