X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=t%2F4_app_00_base.t;h=ef636489f31cc7cced25451be44ecaa4ebd3ee10;hb=6ab8b2e8e8388d1a238148a1ee58e124855f3768;hp=2e3170af67f70edf85affbcb83daa17ae480b0db;hpb=85070b46d0a93ddbeef07341421adb8389a55418;p=chaz%2Fp5-CGI-Ex diff --git a/t/4_app_00_base.t b/t/4_app_00_base.t index 2e3170a..ef63648 100644 --- a/t/4_app_00_base.t +++ b/t/4_app_00_base.t @@ -1,12 +1,1132 @@ # -*- Mode: Perl; -*- -use Test; +=head1 NAME -BEGIN {plan tests => 2}; +4_app_00_base.t - Check for the basic functionality of CGI::Ex::App. -use CGI::Ex::App; -ok(1); +=head1 NOTE -my $obj = CGI::Ex::App->new({ -}); -ok($obj); +These tests are extremely stripped down to test the basic path flow. Normally +unit tests are useful for garnering information about a module. For CGI::Ex::App +it is suggested to stick to live use cases or the CGI::Ex::App perldoc - though +we do try to put it through most paces. + +=cut + +use Test::More tests => 234; +use strict; +use warnings; +use CGI::Ex::Dump qw(debug caller_trace); + +{ + package CGIXFail; + use vars qw($AUTOLOAD); + sub new { bless {}, __PACKAGE__ } + sub DESTROY {} + sub AUTOLOAD { + my $self = shift; + my $meth = ($AUTOLOAD =~ /::(\w+$)/) ? $1 : die "Invalid method $AUTOLOAD"; + die "Not calling CGI::Ex method $meth while testing App"; + } +} +{ + package Foo; + + use base qw(CGI::Ex::App); + use vars qw($test_stdout); + use CGI::Ex::Dump qw(debug caller_trace); + + sub cgix { shift->{'cgix'} ||= CGIXFail->new } # for our tests try not to access external + + sub form { shift->{'form'} ||= {} } + + sub cookies { shift->{'cookies'} ||= {} } + + sub init { $test_stdout = '' } + + sub print_out { + my $self = shift; + my $step = shift; + my $str = shift; + $test_stdout = ref($str) ? $$str : $str; + } + + sub swap_template { + my ($self, $step, $file, $swap) = @_; + die "No filenames allowed during test mode" if ! ref($file); + return $self->SUPER::swap_template($step, $file, $swap); + } + + sub auth_args { {login_template => \q{Login Form}, key_user => 'user', key_pass => 'pass', key_cookie => 'user', set_cookie => sub {}} } + + sub get_pass_by_user { '123qwe' } + + ###----------------------------------------------------------------### + + sub main_info_complete { 0 } + + sub main_file_print { return \ "Main Content [%~ extra %]" } + + sub main_path_info_map { shift->{'main_path_info_map'} } + + sub step2_hash_validation { return {wow => {required => 1, required_error => 'wow is required'}} } + + sub step2_path_info_map { [[qr{^/step2/(\w+)$}x, 'wow']] } + + sub step2_file_print { return \ "Some step2 content ([% foo %], [% one %]) [% wow_error %]" } + + sub step2_hash_swap { return {foo => 'bar', one => 'two'} } + + sub step2_hash_fill { return {wow => 'wee'} } + + sub step2_finalize { shift->append_path('step3') } + + sub step3_info_complete { 0 } + + sub step3_file_print { return \ "All good [%~ extra %]" } + + sub step4_file_val { return {wow => {required => 1, required_error => 'wow is required'}} } + + sub step4_path_info_map { [[qr{^/step4/(\w+)$}x, 'wow']] } + + sub step4_file_print { return \ "Some step4 content ([% foo %], [% one %])
[% wow_error %]
[% js_validation %]" } + + sub step4_hash_swap { return {foo => 'bar', one => 'two'} } + + sub step4_hash_fill { return {wow => 'wee'} } + + sub step4_finalize { shift->append_path('step3') } + + sub step5__part_a_file_print { return \ "Step 5 Nested ([% step %])" } + + sub step5__part_a_info_complete { 0 } + +} + +###----------------------------------------------------------------### +###----------------------------------------------------------------### +print "#-----------------------------------------\n"; +print "### Test some basic returns ###\n"; + +ok(! eval { CGI::Ex::App::new() }, "Invalid new"); +ok(! eval { CGI::Ex::App::new(0) }, "Invalid new"); + +my $app = CGI::Ex::App->new({script_name => '/cgi-bin/foo_bar'}); +ok($app->script_name eq '/cgi-bin/foo_bar', "Can pass in script_name"); +ok($app->name_module eq 'foo_bar', "Can pass in script_name"); + +$app = CGI::Ex::App->new({script_name => '/cgi-bin/foo_bar.pl'}); +ok($app->script_name eq '/cgi-bin/foo_bar.pl', "Can pass in script_name"); +ok($app->name_module eq 'foo_bar', "Can pass in script_name"); + +ok(Foo->new(name_module => 'foo')->name_module eq 'foo', "Got the name_module"); +ok(! eval { Foo->new(script_name => '%####$')->name_module } && $@, "Bad script_name"); +ok(! eval { Foo->new(script_name => '%####$')->name_module('foo') } && $@, "Bad script_name"); + +ok(! eval { $app->morph_package } && $@, "Can't get a good morph_package"); +ok($app->morph_package('foo') eq 'CGI::Ex::App::Foo', "Got a good morph_package"); +ok($app->morph_package('foo_bar') eq 'CGI::Ex::App::FooBar', "Got a good morph_package"); + +ok(ref($app->path), "Got a good path"); +ok(@{ $app->path } == 0, "Got a good path"); +is($app->default_step, 'main', "Got a good default_step"); +is($app->login_step, '__login', "Got a good login_step"); +is($app->error_step, '__error', "Got a good error_step"); +is($app->forbidden_step, '__forbidden', "Got a good forbidden_step"); +is($app->js_step, 'js', "Got a good js_step"); + +# check for different step types +is($app->run_hook('file_print', '__leading_underbars'), 'foo_bar/__leading_underbars.html', 'file_print - __ is preserved at beginning of step'); +is($app->run_hook('file_print', 'central__underbars'), 'foo_bar/central/underbars.html', 'file_print - __ is used in middle of step'); +my $ref = ref($app); +is($app->run_hook('morph_package', '__leading_underbars'), "${ref}::LeadingUnderbars", 'morph_package - __ is works at beginning of step'); +is($app->run_hook('morph_package', 'central__underbars'), "${ref}::Central::Underbars", 'morph_package - __ is used in middle of step'); + +###----------------------------------------------------------------### +###----------------------------------------------------------------### +print "#-----------------------------------------\n"; +print "### Test basic step selection/form input/validation/filling/template swapping methods ###\n"; + +#$ENV{'REQUEST_METHOD'} = 'GET'; +#$ENV{'QUERY_STRING'} = ''; + +Foo->new({ + form => {}, +})->navigate; +ok($Foo::test_stdout eq "Main Content", "Got the right output for Foo"); + +{ + package Foo2; + our @ISA = qw(Foo); + sub form { {} } +} +Foo2->navigate; +ok($Foo::test_stdout eq "Main Content", "Got the right output for Foo2"); + +###----------------------------------------------------------------### + +{ + package Foo2_1; + our @ISA = qw(Foo); + sub pre_navigate { 1 } +} +Foo2_1->navigate; +ok($Foo::test_stdout eq "", "Got the right output for Foo2_1"); + +Foo2_1->new({_no_pre_navigate => 1})->navigate; +ok($Foo::test_stdout eq "Main Content", "Got the right output for Foo2_1"); + +{ + package Foo2_2; + our @ISA = qw(Foo); + sub pre_loop { 1 } +} +Foo2_2->navigate; +ok($Foo::test_stdout eq "", "Got the right output for Foo2_2"); + +{ + package Foo2_3; + our @ISA = qw(Foo); + sub post_loop { 1 } +} +Foo2_3->navigate; +ok($Foo::test_stdout eq "", "Got the right output for Foo2_3"); + +{ + package Foo2_4; + our @ISA = qw(Foo); + sub post_navigate { $Foo::test_stdout .= " post"; 1 } +} +Foo2_4->navigate; +ok($Foo::test_stdout eq "Main Content post", "Got the right output for Foo2_4"); + +Foo2_4->new({_no_post_navigate => 1})->navigate; +ok($Foo::test_stdout eq "Main Content", "Got the right output for Foo2_4"); + +my $f; + +###----------------------------------------------------------------### + +local $ENV{'REQUEST_METHOD'} = 'POST'; +#$ENV{'QUERY_STRING'} = 'step=step2'; + +Foo->new({ + form => {step => 'step2'}, +})->navigate; +ok($Foo::test_stdout eq "Some step2 content (bar, two) wow is required", "Got the right output for Foo"); + +Foo->new({ + form => {step => 'step4'}, +})->navigate; +ok($Foo::test_stdout =~ /Some step4 content.*wow is required.*