Skip to content

Commit

Permalink
Improving print and request tests and code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Parry committed Mar 27, 2012
1 parent 0ca5fdc commit beefc45
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
23 changes: 16 additions & 7 deletions lib/Apache/Sling/Print.pm
Expand Up @@ -89,11 +89,24 @@ sub print_result {

#}}}

#{{{sub date_time
#{{{sub date_string

sub date_time {
sub date_string {
my ( $day_of_week, $month, $year_offset, $day_of_month, $hour, $minute, $sec ) = @_;
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @week_days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
if ( $sec =~ /^[0-9]$/msx ) { $sec = "0$sec"; }
if ( $minute =~ /^[0-9]$/msx ) { $minute = "0$minute"; }
my $year = 1900 + $year_offset;
return
"$week_days[$day_of_week] $months[$month] $day_of_month $hour:$minute:$sec";
}

#}}}

#{{{sub date_time

sub date_time {
(
my $sec,
my $minute,
Expand All @@ -105,11 +118,7 @@ sub date_time {
my $day_of_year,
my $daylight_savings
) = localtime;
if ( $sec =~ /^[0-9]$/msx ) { $sec = "0$sec"; }
if ( $minute =~ /^[0-9]$/msx ) { $minute = "0$minute"; }
my $year = 1900 + $year_offset;
return
"$week_days[$day_of_week] $months[$month] $day_of_month $hour:$minute:$sec";
return date_string($day_of_week, $month, $year_offset, $day_of_month, $hour, $minute, $sec );
}

#}}}
Expand Down
9 changes: 8 additions & 1 deletion t/Local/Apache-Sling-Print.t
Expand Up @@ -3,17 +3,24 @@
use strict;
use warnings;

use Test::More tests => 9;
use Test::More tests => 13;
use Test::Exception;
use File::Temp;
BEGIN { use_ok( 'Apache::Sling' ); }
BEGIN { use_ok( 'Apache::Sling::Print' ); }

ok( Apache::Sling::Print::print_lock('Check print_lock function'), 'Check print_lock function' );
ok( Apache::Sling::Print::print_with_lock('Check print_with_lock function'), 'Check print_with_lock function' );
my ( $tmp_print_file_handle, $tmp_print_file_name ) = File::Temp::tempfile();
ok( Apache::Sling::Print::print_with_lock('Check print_with_lock function',$tmp_print_file_name), 'Check print_with_lock function to file' );
ok( Apache::Sling::Print::print_file_lock('Check print_file_lock function',$tmp_print_file_name), 'Check print_file_lock function' );
my $file;
throws_ok{ Apache::Sling::Print::print_with_lock('Check print_with_lock function',\$file); } qr%%, 'Check print_with_lock function to in memory file fails';
close $tmp_print_file_handle;
unlink($tmp_print_file_name);
ok( Apache::Sling::Print::date_time('Check date_time function'), 'Check date_time function' );
ok( Apache::Sling::Print::date_string(1, 1, 100, 1, 1, 1, 1), 'Check date_string function single figure units' );
ok( Apache::Sling::Print::date_string(1, 1, 100, 1, 1, 30, 30), 'Check date_string function double figure units' );

# sling object:
my $sling = Apache::Sling->new();
Expand Down
8 changes: 7 additions & 1 deletion t/Local/Apache-Sling-Request.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 13;
use Test::More tests => 18;
use Test::Exception;
BEGIN { use_ok( 'Apache::Sling' ); }
BEGIN { use_ok( 'Apache::Sling::Request' ); }
Expand All @@ -12,11 +12,13 @@ BEGIN { use_ok( 'Apache::Sling::Content' ); }
my $sling = Apache::Sling->new();
my $authn = new Apache::Sling::Authn(\$sling);
my $content = new Apache::Sling::Content(\$authn,'1','log.txt');
throws_ok { Apache::Sling::Request::string_to_request() } qr/No string defined to turn into request!/, 'Checking string_to_request function string undefined';
throws_ok { Apache::Sling::Request::string_to_request('',\$authn) } qr/Error generating request for blank target!/, 'Checking string_to_request function blank string';
ok ( Apache::Sling::Request::string_to_request("post http://localhost:8080 \$post_variables = ['a','b']",\$authn), 'Checking string_to_request function for post action' );
ok ( Apache::Sling::Request::string_to_request("data http://localhost:8080 \$post_variables = ['a','b']",\$authn), 'Checking string_to_request function for data action' );
my ( $tmp_print_file_handle, $tmp_print_file_name ) = File::Temp::tempfile();
ok ( Apache::Sling::Request::string_to_request("fileupload http://localhost:8080 filename $tmp_print_file_name \$post_variables = []",\$authn), 'Checking string_to_request function for file upload action' );
throws_ok { Apache::Sling::Request::string_to_request("fileupload http://localhost:8080 filename $tmp_print_file_name \$post_variables = ['a',",\$authn) } qr//, 'Checking string_to_request function for fileupload action fails with broken post variables';
unlink($tmp_print_file_name);
$authn->{'Username'} = 'user';
$authn->{'Password'} = 'pass';
Expand All @@ -27,3 +29,7 @@ ok ( Apache::Sling::Request::string_to_request("get http://localhost:8080",\$aut
throws_ok { Apache::Sling::Request::request(\$content,'') } qr/Error generating request for blank target!/, 'Checking request function blank string';
throws_ok { Apache::Sling::Request::request() } qr/No reference to a suitable object supplied!/, 'Check request function croaks without object';
throws_ok { Apache::Sling::Request::request(\$content) } qr/No string defined to turn into request!/, 'Check request function croaks without string';
throws_ok { Apache::Sling::Request::string_to_request("post http://localhost:8080 \$post_variables = ['a',",\$authn) } qr//, 'Checking string_to_request function for post action fails with broken post variables';
throws_ok { Apache::Sling::Request::string_to_request("data http://localhost:8080 \$post_variables = ['a',",\$authn) } qr//, 'Checking string_to_request function for data action fails with broken post variables';
$authn->{'LWP'} = undef;
throws_ok { Apache::Sling::Request::string_to_request("post http://localhost:8080 \$post_variables = ['a','b']",\$authn) } qr//, 'Checking string_to_request function for post action fails without LWP defined';

0 comments on commit beefc45

Please sign in to comment.