From: Dave Rolsky Date: Fri, 11 Sep 2009 01:37:26 +0000 (-0500) Subject: Fix to-role role application with aliases. X-Git-Tag: 0.89_02~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bd38046ec077bf1dfa1a852f9864d9d7c95dffa1;p=gitmo%2FMoose.git Fix to-role role application with aliases. Aliased methods should be added with both their original and new name. Added some more tests for aliases/excludes with role-to-role application. --- diff --git a/lib/Moose/Meta/Role/Application/ToRole.pm b/lib/Moose/Meta/Role/Application/ToRole.pm index 150def9..31f1473 100644 --- a/lib/Moose/Meta/Role/Application/ToRole.pm +++ b/lib/Moose/Meta/Role/Application/ToRole.pm @@ -74,6 +74,26 @@ sub apply_methods { my ($self, $role1, $role2) = @_; foreach my $method_name ($role1->get_method_list) { + unless ( $self->is_method_excluded($method_name) ) { + if ( $role2->has_method($method_name) + && $role2->get_method($method_name)->body + != $role1->get_method($method_name)->body ) { + + # method conflicts between roles result in the method becoming + # a requirement + $role2->add_conflicting_method( + name => $method_name, + roles => [ $role1->name, $role2->name ], + ); + } + else { + $role2->add_method( + $method_name, + $role1->get_method($method_name) + ); + } + } + if ($self->is_method_aliased($method_name)) { my $aliased_method_name = $self->get_method_aliases->{$method_name}; @@ -93,29 +113,7 @@ sub apply_methods { $role2->add_required_methods($method_name) unless $self->is_method_excluded($method_name); } - - next; - } - - next if $self->is_method_excluded($method_name); - - if ($role2->has_method($method_name) && - $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) { - # method conflicts between roles result - # in the method becoming a requirement - $role2->add_conflicting_method( - name => $method_name, - roles => [$role1->name, $role2->name], - ); - } - else { - $role2->add_method( - $method_name, - $role1->get_method($method_name) - ); - } - } } diff --git a/t/030_roles/013_method_aliasing_in_composition.t b/t/030_roles/013_method_aliasing_in_composition.t index 4c7db16..3f518ee 100644 --- a/t/030_roles/013_method_aliasing_in_composition.t +++ b/t/030_roles/013_method_aliasing_in_composition.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 38; +use Test::More tests => 46; use Test::Exception; @@ -58,7 +58,7 @@ ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar ro } ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar); -ok(!My::OtherRole->meta->requires_method('bar'), '... and the &bar method is not required'); +ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required'); ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required'); { @@ -71,7 +71,7 @@ ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar met } ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar); -ok(My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is required'); +ok(!My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is not required'); { package Foo::Role; @@ -158,3 +158,57 @@ ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo met ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method'); ok(My::Foo::AliasOnly->meta->has_method('foo_foo'), '.. and the aliased foo_foo method'); + +{ + package Role::Foo; + use Moose::Role; + + sub x1 {} + sub y1 {} +} + +{ + package Role::Bar; + use Moose::Role; + + use Test::Exception; + + lives_ok { + with 'Role::Foo' => { + -alias => { x1 => 'foo_x1' }, + -excludes => ['y1'], + }; + } + 'Compose Role::Foo into Role::Bar with alias and exclude'; + + sub x1 {} + sub y1 {} +} + +{ + my $bar = Role::Bar->meta; + ok( $bar->has_method($_), "has $_ method" ) + for qw( x1 y1 foo_x1 ); +} + +{ + package Role::Baz; + use Moose::Role; + + use Test::Exception; + + lives_ok { + with 'Role::Foo' => { + -alias => { x1 => 'foo_x1' }, + -excludes => ['y1'], + }; + } + 'Compose Role::Foo into Role::Baz with alias and exclude'; +} + +{ + my $baz = Role::Baz->meta; + ok( $baz->has_method($_), "has $_ method" ) + for qw( x1 foo_x1 ); + ok( ! $baz->has_method('y1'), 'Role::Baz has no y1 method' ); +}