From da82cb539a47e4b70dcaf5c24ef0972d1d6e73d0 Mon Sep 17 00:00:00 2001 From: Andrew Moore Date: Mon, 7 Jul 2008 12:54:51 -0500 Subject: [PATCH] bug 2295 [1/4]: moving C4::Dates tests into database dependent tests the t/Dates.t tests were actually databasase dependent. This patch replaces t/Dates.t with t/lib/KohaTest/Dates/Usage.pm that relies on a database. Signed-off-by: Galen Charlton Signed-off-by: Joshua Ferraro --- t/Dates.t | 78 ---------------------------- t/lib/KohaTest/Dates/Usage.pm | 96 +++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 78 deletions(-) delete mode 100755 t/Dates.t create mode 100644 t/lib/KohaTest/Dates/Usage.pm diff --git a/t/Dates.t b/t/Dates.t deleted file mode 100755 index aeec309a4e..0000000000 --- a/t/Dates.t +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -use Test::More tests => 126; -BEGIN { - use FindBin; - use lib $FindBin::Bin; - use_ok('C4::Dates', qw(format_date format_date_in_iso)); -} - -sub describe ($$) { - my $front = sprintf("%-25s", shift); - my $tail = shift || 'FAILED'; - return "$front : $tail"; -} - -my %thash = ( - iso => ['2001-01-01','1989-09-21','1952-01-00'], - metric => ["01-01-2001",'21-09-1989','00-01-1952'], - us => ["01-01-2001",'09-21-1989','01-00-1952'], - sql => ['20010101 010101', - '19890921 143907', - '19520100 000000' ], -); - -my ($date, $format, $today, $today0, $val, $re, $syspref); -my @formats = sort keys %thash; -diag "\n Testing Legacy Functions: format_date and format_date_in_iso"; -ok($syspref = C4::Dates->new->format(), "Your system preference is: $syspref"); -print "\n"; -foreach (@{$thash{'iso'}}) { - ok($val = format_date($_), "format_date('$_'): $val" ); -} -foreach (@{$thash{$syspref}}) { - ok($val = format_date_in_iso($_), "format_date_in_iso('$_'): $val" ); -} -ok($today0 = C4::Dates->today(), "(default) CLASS ->today : $today0" ); -diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n"; -print "\n"; -foreach (@formats) { - my $pre = sprintf '(%-6s)', $_; - ok($date = C4::Dates->new(), "$pre Date Creation : new()"); - ok($_ eq ($format = $date->format($_)), "$pre format($_) : " . ($format|| 'FAILED') ); - ok($format = $date->visual(), "$pre visual() : " . ($format|| 'FAILED') ); - ok($today = $date->output(), "$pre output() : " . ($today || 'FAILED') ); - ok($today = $date->today(), "$pre object->today : " . ($today || 'FAILED') ); - print "\n"; -} - -diag "\nTesting with valid inputs:\n"; -foreach $format (@formats) { - my $pre = sprintf '(%-6s)', $format; - foreach my $testval (@{$thash{ $format }}) { - ok($date = C4::Dates->new($testval,$format), "$pre Date Creation : new('$testval','$format')"); - ok($re = $date->regexp, "$pre has regexp()" ); - ok($val = $date->output(), describe("$pre output()", $val) ); - foreach (grep {!/$format/} @formats) { - ok($today = $date->output($_), describe(sprintf("$pre output(%8s)","'$_'"), $today) ); - } - ok($today = $date->today(), describe("$pre object->today", $today) ); - # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today : $today" ); - ok($val = $date->output(), describe("$pre output()", $val) ); - # ok($format eq ($format = $date->format()), "$pre format() : $format" ); - print "\n"; - } -} - -diag "\nTesting object independence from class\n"; -my $in1 = '12/25/1952'; # us -my $in2 = '13/01/2001'; # metric -my $d1 = C4::Dates->new($in1, 'us'); -my $d2 = C4::Dates->new($in2, 'metric'); -my $out1 = $d1->output('iso'); -my $out2 = $d2->output('iso'); -ok($out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)"); -diag "done.\n"; diff --git a/t/lib/KohaTest/Dates/Usage.pm b/t/lib/KohaTest/Dates/Usage.pm new file mode 100644 index 0000000000..6f5941833c --- /dev/null +++ b/t/lib/KohaTest/Dates/Usage.pm @@ -0,0 +1,96 @@ +package KohaTest::Dates::Usage; +use base qw( KohaTest::Dates ); + +use strict; +use warnings; + +use Test::More; + +use C4::Dates qw(format_date format_date_in_iso); + + +my %thash = ( + iso => [ '2001-01-01', '1989-09-21', '1952-01-00' ], + metric => [ "01-01-2001", '21-09-1989', '00-01-1952' ], + us => [ "01-01-2001", '09-21-1989', '01-00-1952' ], + sql => [ '20010101 010101', '19890921 143907', '19520100 000000' ], +); + + +my @formats = sort keys %thash; + +sub check_formats : Test( 10 ) { + my $self = shift; + + my $syspref = C4::Dates->new->format(); + ok( $syspref, "Your system preference is: $syspref" ); + + foreach ( @{ $thash{'iso'} } ) { + ok( format_date($_), "able to format_date() on $_" ); + } + + foreach ( @{ $thash{$syspref} } ) { + ok( format_date_in_iso($_), "able to format_date_in_iso() on $_" ); + } + ok( C4::Dates->today(), "(default) CLASS ->today : " . C4::Dates->today() ); +} + +sub defaults : Test( 24 ) { + my $self = shift; + + foreach (@formats) { + my $pre = sprintf '(%-6s)', $_; + my $date = C4::Dates->new(); + ok( $date, "$pre Date Creation : new()" ); + isa_ok( $date, 'C4::Dates' ); + ok( $_ eq $date->format($_), "$pre format($_) : " ); + ok( $date->visual(), "$pre visual()" ); + ok( $date->output(), "$pre output()" ); + ok( $date->today(), "$pre object->today" ); + + } +} + +sub valid_inputs : Test( 108 ) { + my $self = shift; + + foreach my $format (@formats) { + my $pre = sprintf '(%-6s)', $format; + foreach my $testval ( @{ $thash{$format} } ) { + my ( $val, $today ); + my $date = C4::Dates->new( $testval, $format ); + ok( $date, "$pre Date Creation : new('$testval','$format')" ); + isa_ok( $date, 'C4::Dates' ); + ok( $date->regexp, "$pre has regexp()" ); + ok( $val = $date->output(), describe( "$pre output()", $val ) ); + foreach ( grep { !/$format/ } @formats ) { + ok( $today = $date->output($_), describe( sprintf( "$pre output(%8s)", "'$_'" ), $today ) ); + } + ok( $today = $date->today(), describe( "$pre object->today", $today ) ); + ok( $val = $date->output(), describe( "$pre output()", $val ) ); + } + } +} + +sub independence_from_class : Test( 1 ) { + my $self = shift; + + my $in1 = '12/25/1952'; # us + my $in2 = '13/01/2001'; # metric + my $d1 = C4::Dates->new( $in1, 'us' ); + my $d2 = C4::Dates->new( $in2, 'metric' ); + my $out1 = $d1->output('iso'); + my $out2 = $d2->output('iso'); + ok( $out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)" ); + +} + + + +sub describe { + my $front = sprintf( "%-25s", shift ); + my $tail = shift || 'FAILED'; + return "$front : $tail"; +} + +1; -- 2.39.5