From: Matt S Trout Date: Tue, 19 Jun 2012 13:55:42 +0000 (+0000) Subject: modifiers and composition fix X-Git-Tag: v1.001003~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=06b06c8e8a98b4aca1168e5336a946bffba4431e;p=gitmo%2FRole-Tiny.git modifiers and composition fix --- diff --git a/Changes b/Changes index 7c33e19..0a5e1a5 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ + - correctly apply modifiers with role composition - check for conflicts during role-to-object application (test from mmcleric) - add an explicit return to all exported subs so people don't accidentally rely on the return value diff --git a/lib/Role/Tiny.pm b/lib/Role/Tiny.pm index 04a44d1..d19f3a5 100644 --- a/lib/Role/Tiny.pm +++ b/lib/Role/Tiny.pm @@ -188,7 +188,14 @@ sub apply_roles_to_package { } keys %conflicts; die $fail; } - delete $INFO{$to}{methods}; # reset since we're about to add methods + + # the if guard here is essential since otherwise we accidentally create + # a $INFO for something that isn't a Role::Tiny (or Moo::Role) because + # autovivification hates us and wants us to die() + if ($INFO{$to}) { + delete $INFO{$to}{methods}; # reset since we're about to add methods + } + $me->apply_role_to_package($to, $_) for @roles; $APPLIED_TO{$to}{join('|',@roles)} = 1; } diff --git a/t/modifiers.t b/t/modifiers.t index a557d50..8a3e1f1 100644 --- a/t/modifiers.t +++ b/t/modifiers.t @@ -17,12 +17,28 @@ BEGIN { } BEGIN { + package ExtraRole; + + use Role::Tiny; +} + +BEGIN { package MyClass; sub foo { 'class foo' } } BEGIN { + package ExtraClass; + + use Role::Tiny::With; + + with qw(MyRole ExtraRole); + + sub foo { 'class foo' } +} + +BEGIN { package BrokenRole; use Role::Tiny; @@ -36,6 +52,7 @@ sub try_apply_to { is(try_apply_to('MyClass'), undef, 'role applies cleanly'); is(MyClass->foo, 'role foo class foo', 'method modifier'); +is(ExtraClass->foo, 'role foo class foo', 'method modifier with composition'); ok(exception { my $new_class = Role::Tiny->create_class_with_roles('MyClass', 'BrokenRole');