From: Shawn M Moore Date: Wed, 26 Nov 2008 02:25:52 +0000 (+0000) Subject: Fix the error message when we're composing role A with a role B which is excluded... X-Git-Tag: 0.62~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=88cfb4cb41715de3ac007f658c8063f92f847be9;p=gitmo%2FMoose.git Fix the error message when we're composing role A with a role B which is excluded by role A. Before it just used to say "role B excludes role B". --- diff --git a/Changes b/Changes index eeae12e..ba2caec 100644 --- a/Changes +++ b/Changes @@ -7,6 +7,9 @@ Revision history for Perl extension Moose same methods for a single role did not work right (worked just fine with multiple roles) - added test for this + * Moose::Meta::Role::Application::RoleSummation + - fixed the error message when trying to compose + a role with a role it excludes (Sartak) * Moose::Exporter - Catch another case where recursion caused the value of $CALLER to be stamped on (t0m) diff --git a/lib/Moose/Meta/Role/Application/RoleSummation.pm b/lib/Moose/Meta/Role/Application/RoleSummation.pm index 0cfcd9b..c443e3d 100644 --- a/lib/Moose/Meta/Role/Application/RoleSummation.pm +++ b/lib/Moose/Meta/Role/Application/RoleSummation.pm @@ -65,18 +65,25 @@ my $uniq = sub { my %h; map { $h{$_}++ == 0 ? $_ : () } @_ }; sub check_role_exclusions { my ($self, $c) = @_; - my @all_excluded_roles = $uniq->(map { - $_->get_excluded_roles_list - } @{$c->get_roles}); + my %excluded_roles; + for my $role (@{ $c->get_roles }) { + my $name = $role->name; + + for my $excluded ($role->get_excluded_roles_list) { + push @{ $excluded_roles{$excluded} }, $name; + } + } foreach my $role (@{$c->get_roles}) { - foreach my $excluded (@all_excluded_roles) { - Moose->throw_error("Conflict detected: " . $role->name . " excludes role '" . $excluded . "'") - if $role->does_role($excluded); + foreach my $excluded (keys %excluded_roles) { + next unless $role->does_role($excluded); + + my @excluding = @{ $excluded_roles{$excluded} }; + Moose->throw_error(sprintf 'Conflict detected: Role%s %s exclude%s role "%s"', (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded); } } - $c->add_excluded_roles(@all_excluded_roles); + $c->add_excluded_roles(keys %excluded_roles); } sub check_required_methods { diff --git a/t/030_roles/006_role_exclusion.t b/t/030_roles/006_role_exclusion.t index 578ba82..30c2d0e 100644 --- a/t/030_roles/006_role_exclusion.t +++ b/t/030_roles/006_role_exclusion.t @@ -62,7 +62,7 @@ the roles into the same class ::throws_ok { with 'Molecule::Organic', 'Molecule::Inorganic'; - } qr/Conflict detected: .+ excludes role \'Molecule::Inorganic\'/, + } qr/Conflict detected: Role Molecule::Organic excludes role "Molecule::Inorganic"/, '... adding the role w/ excluded role conflict dies okay'; package My::Test3;