X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Funit_core_component_loading.t;h=d27b9b7078dd20b65d1514c520b38ac05782c809;hb=3932d8818a3c5112315a8757b6db7a3678755f7e;hp=f0204ec2ebb3b7f015bc43c60e6094b622950cb1;hpb=2d486591cee0779dbfa2afff5e35d957f46ec79c;p=catagits%2FCatalyst-Runtime.git diff --git a/t/unit_core_component_loading.t b/t/unit_core_component_loading.t index f0204ec..d27b9b7 100644 --- a/t/unit_core_component_loading.t +++ b/t/unit_core_component_loading.t @@ -1,7 +1,7 @@ # 2 initial tests, and 6 per component in the loop below # (do not forget to update the number of components in test 3 as well) -# 4 extra tests for the loading options -use Test::More tests => 2 + 6 * 24 + 4; +# 5 extra tests for the loading options +use Test::More tests => 2 + 6 * 24 + 5; use strict; use warnings; @@ -9,6 +9,8 @@ use warnings; use File::Spec; use File::Path; +use Test::MockObject; + my $libdir = 'test_trash'; unshift(@INC, $libdir); @@ -40,6 +42,18 @@ my @components = ( { type => 'View', prefix => 'View', name => 'Foo' }, ); +sub write_component_file { + my ($dir_list, $module_name, $content) = @_; + + my $dir = File::Spec->catdir(@$dir_list); + my $file = File::Spec->catfile($dir, $module_name . '.pm'); + + mkpath(join(q{/}, @$dir_list) ); + open(my $fh, '>', $file) or die "Could not open file $file for writing: $!"; + print $fh $content; + close $fh; +} + sub make_component_file { my ($type, $prefix, $name) = @_; @@ -48,17 +62,13 @@ sub make_component_file { my @namedirs = split(/::/, $name); my $name_final = pop(@namedirs); my @dir_list = ($libdir, $appclass, $prefix, @namedirs); - my $dir_ux = join(q{/}, @dir_list); - my $dir = File::Spec->catdir(@dir_list); - my $file = File::Spec->catfile($dir, $name_final . '.pm'); - mkpath($dir_ux); # mkpath wants unix '/' seperators :p - open(my $fh, '>', $file) or die "Could not open file $file for writing: $!"; - print $fh <NEXT::COMPONENT(\@_); + my \$self = shift->next::method(\@_); no strict 'refs'; *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; }; \$self; @@ -66,8 +76,6 @@ sub COMPONENT { 1; EOF - - close($fh); } foreach my $component (@components) { @@ -76,7 +84,19 @@ foreach my $component (@components) { $component->{name}); } -eval "package $appclass; use Catalyst; __PACKAGE__->setup"; +my $shut_up_deprecated_warnings = q{ + use Test::MockObject; + my $old_logger = __PACKAGE__->log; + my $logger = Test::MockObject->new; + $logger->mock('warn', sub { + my $self = shift; + return if $_[0] =~ /deprecated/; + $old_logger->warn(@_); + }); + __PACKAGE__->log($logger); +}; + +eval "package $appclass; use Catalyst; $shut_up_deprecated_warnings __PACKAGE__->setup"; can_ok( $appclass, 'components'); @@ -135,6 +155,7 @@ foreach my $component (@components) { eval qq( package $appclass; use Catalyst; +$shut_up_deprecated_warnings __PACKAGE__->config->{ setup_components } = { search_extra => [ '::Extra' ], except => [ "${appclass}::Controller::Foo" ] @@ -151,4 +172,41 @@ is(scalar keys %$complist, 24+1, "Correct number of components loaded"); ok( !exists $complist->{ "${appclass}::Controller::Foo" }, 'Controller::Foo was skipped' ); ok( exists $complist->{ "${appclass}::Extra::Foo" }, 'Extra::Foo was loaded' ); -rmtree($libdir); \ No newline at end of file +rmtree($libdir); + +$appclass = "ComponentOnce"; + +write_component_file([$libdir, $appclass, 'Model'], 'TopLevel', <next::method(\@_); + no strict 'refs'; + *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; }; + \$self; +} + +package ${appclass}::Model::TopLevel::Nested; + +sub COMPONENT { die "COMPONENT called in the wrong order!"; } + +1; + +EOF + +write_component_file([$libdir, $appclass, 'Model', 'TopLevel'], 'Nested', <next::method(\@_); } +1; + +EOF + +eval "package $appclass; use Catalyst; __PACKAGE__->setup"; + +is($@, '', "Didn't load component twice"); + +rmtree($libdir);