X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F03_dynamiccomponent.t;h=33cd87831440adc956586ea6546e4d6cd4da4e0e;hb=500cd4f4ac633b4690c3b5368edeb25b9c693e93;hp=a6f006eedd2e8eb6e05ed9030b218b68319143bf;hpb=7191ab41f164f175523bca641f43f555e81e4f03;p=catagits%2FCatalystX-DynamicComponent.git diff --git a/t/03_dynamiccomponent.t b/t/03_dynamiccomponent.t index a6f006e..33cd878 100644 --- a/t/03_dynamiccomponent.t +++ b/t/03_dynamiccomponent.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 32; use Test::Exception; use Moose (); @@ -34,7 +34,9 @@ throws_ok { generate_testapp(); } my $COMPONENT = sub { return bless {}, 'TestClass' }; my $app_meta = generate_testapp({ name => 'dynamic_component_method', - COMPONENT => $COMPONENT, + methods => { + COMPONENT => $COMPONENT, + }, }); my $app = $app_meta->name; ok $app->can('dynamic_component_method'), 'dynamic component method added'; @@ -46,5 +48,138 @@ throws_ok { generate_testapp(); } isa_ok(bless({}, $name), 'Catalyst::Component', 'Underlying $app::Model::Foo is a C::C'); is($name->can('COMPONENT'), $COMPONENT, 'Supplied COMPONENT method is on $app::Model::Foo'); } +{ + package My::Other::Superclass; + use Moose; + __PACKAGE__->meta->make_immutable; +} +{ + package My::Model; + use Moose; + extends qw/Catalyst::Model/; + __PACKAGE__->meta->make_immutable; +} +{ + package My::Role; + use Moose::Role; + sub _some_method_from_role {} +} +{ + package My::Other::Role; + use Moose::Role; + sub _some_method_from_other_role {} +} +my %generator_config = ( + name => 'dynamic_component_method', + roles => [qw/My::Other::Role/], + superclasses => ['My::Other::Superclass'], + methods => { + my_other_injected_method => sub {}, + }, +); +my $extra_config = { + superclasses => ['My::Model'], + roles => ['My::Role'], + methods => { + my_injected_method => sub { 'quuux' }, + } +}; +{ + # Do not specify any resolve strategies, so get defaults: + # methods - merge + # superclasses - replace + # roles - merge + my $app_meta = generate_testapp({ + %generator_config, + }); + my $app = $app_meta->name; + $app->dynamic_component_method( $app . "::Model::Foo", $extra_config ); + my $model = $app->model('Foo'); + isa_ok($model, 'My::Model', 'Correct superclass'); + ok(!$model->isa('My::Other::Superclass'), + 'superclass in extra config replaces by default'); + + ok $model->can('_some_method_from_role'), 'Has had role applied'; + ok !My::Model->can('_some_method_from_role'), 'Role applied at right place'; + + ok $model->can('_some_method_from_other_role'), + 'Role application merges by default'; + + ok $model->can('my_injected_method'), 'Injected method there as expected'; + is $model->my_injected_method, 'quuux', 'Injected method returns correct val'; + + ok $model->can('my_other_injected_method'), + 'Injected methods merged by default'; +} +{ + # Specify resolve strategies, totally opposite the defaults: + my $app_meta = generate_testapp({ + %generator_config, + roles_resolve_strategy => 'replace', + superclasses_resolve_strategy => 'merge', + methods_resolve_strategy => 'replace', + }); + my $app = $app_meta->name; + $app->dynamic_component_method( $app . "::Model::Foo", $extra_config ); + my $model = $app->model('Foo'); + isa_ok($model, 'My::Model', 'Correct superclass'); + isa_ok($model, 'My::Other::Superclass', + 'superclasses merged'); + + ok $model->can('_some_method_from_role'), 'Has had role applied'; + ok !My::Model->can('_some_method_from_role'), 'Role applied at right place'; + + ok !$model->can('_some_method_from_other_role'), + 'Role application replaces when configured'; + ok $model->can('my_injected_method'), 'Injected method there as expected'; + is $model->my_injected_method, 'quuux', 'Injected method returns correct val'; + + ok !$model->can('my_other_injected_method'), + 'Injected methods replaced'; +} +{ + # Test that replace with blank config doesnt actually replace the config with blank + my $app_meta = generate_testapp({ + %generator_config, + roles_resolve_strategy => 'replace', + superclasses_resolve_strategy => 'replace', + methods_resolve_strategy => 'replace', + }); + + my $app = $app_meta->name; + $app->dynamic_component_method( $app . "::Model::Foo", {} ); + my $model = $app->model('Foo'); + + ok $model->can('my_other_injected_method'), + 'Injected method present'; + + ok($model->isa('My::Other::Superclass'), + 'superclasses not replaced as not set'); + + ok $model->can('_some_method_from_other_role'), + 'Role application does not replace as not set'; +} +{ + # Test that replace with explicit blanks does so. + my $app_meta = generate_testapp({ + %generator_config, + roles_resolve_strategy => 'replace', + superclasses_resolve_strategy => 'replace', + methods_resolve_strategy => 'replace', + }); + + my $app = $app_meta->name; + $app->dynamic_component_method( $app . "::Model::Foo", { superclasses => [], roles => [], methods => {} } ); + my $model = $app->model('Foo'); + + ok !$model->can('my_other_injected_method'), + 'Injected method not present'; + + ok(!$model->isa('My::Other::Superclass'), + 'superclasses not replaced as set to []'); + + ok !$model->can('_some_method_from_other_role'), + 'Role application replaces as set to []'; +}