From: Matt S Trout Date: Wed, 11 Apr 2012 17:28:14 +0000 (+0000) Subject: more cleanup X-Git-Tag: v1.000_900~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2c580674e210e2252672aa0bb691887f46c7d846;p=gitmo%2FRole-Tiny.git more cleanup --- diff --git a/lib/Role/Tiny.pm b/lib/Role/Tiny.pm index 4560637..ff742d8 100644 --- a/lib/Role/Tiny.pm +++ b/lib/Role/Tiny.pm @@ -106,6 +106,14 @@ sub create_class_with_roles { die "No roles supplied!" unless @roles; + { + my %seen; + $seen{$_}++ for @roles; + if (my @dupes = grep $seen{$_} > 1, @roles) { + die "Duplicated roles: ".join(', ', @dupes); + } + } + my $new_name = join( '__WITH__', $superclass, my $compose_name = join '__AND__', @roles ); @@ -167,7 +175,7 @@ sub apply_union_of_roles_to_package { sub _union_info_for { my ($me, @roles) = @_; - $UNION_INFO{join('|',@roles)} ||= do { + $UNION_INFO{join('|', sort @roles)} ||= do { _load_module($_) for @roles; my %methods; foreach my $role (@roles) { diff --git a/t/role-basic-00-load.t b/t/role-basic-00-load.t index a93a7cf..bfcb321 100644 --- a/t/role-basic-00-load.t +++ b/t/role-basic-00-load.t @@ -1,7 +1,7 @@ use Test::More tests => 1; BEGIN { - use_ok( 'Role::Tiny::Restricted' ) || BAIL_OUT "Could not load Role::Tiny::Restricted: $!"; + use_ok( 'Role::Tiny' ) || BAIL_OUT "Could not load Role::Tiny: $!"; } -diag( "Testing Role::Tiny::Restricted $Role::Tiny::Restricted::VERSION, Perl $], $^X" ); +diag( "Testing Role::Tiny $Role::Tiny::VERSION, Perl $], $^X" ); diff --git a/t/role-basic-bugs.t b/t/role-basic-bugs.t index 3819c44..baf35f5 100644 --- a/t/role-basic-bugs.t +++ b/t/role-basic-bugs.t @@ -6,23 +6,23 @@ use MyTests; # multiple roles with the same role { package RoleC; - use Role::Tiny::Restricted; + use Role::Tiny; sub baz { 'baz' } package RoleB; - use Role::Tiny::Restricted; + use Role::Tiny; with 'RoleC'; sub bar { 'bar' } package RoleA; - use Role::Tiny::Restricted; + use Role::Tiny; with 'RoleC'; sub foo { 'foo' } package Foo; use strict; use warnings; - use Role::Tiny::Restricted 'with'; + use Role::Tiny 'with'; ::is( ::exception { with 'RoleA', 'RoleB'; }, undef, 'Composing multiple roles which use the same role should not have conflicts' ); @@ -41,7 +41,7 @@ use MyTests; local *UNIVERSAL::can = sub { 1 }; eval <<' END'; package Can::Can; - use Role::Tiny::Restricted 'with'; + use Role::Tiny 'with'; with 'A::NonExistent::Role'; END my $error = $@ || ''; diff --git a/t/role-basic-composition.t b/t/role-basic-composition.t index 9b94a3c..d161427 100644 --- a/t/role-basic-composition.t +++ b/t/role-basic-composition.t @@ -2,12 +2,12 @@ use lib 'lib', 't/role-basic/lib'; use MyTests; -require Role::Tiny::Restricted; +require Role::Tiny; { package My::Does::Basic1; - use Role::Tiny::Restricted; + use Role::Tiny; requires 'turbo_charger'; sub method { @@ -17,7 +17,7 @@ require Role::Tiny::Restricted; { package My::Does::Basic2; - use Role::Tiny::Restricted; + use Role::Tiny; requires 'turbo_charger'; sub method2 { @@ -27,7 +27,7 @@ require Role::Tiny::Restricted; eval <<'END_PACKAGE'; package My::Class1; -use Role::Tiny::Restricted 'with'; +use Role::Tiny 'with'; with qw( My::Does::Basic1 My::Does::Basic2 @@ -40,7 +40,7 @@ ok !$@, 'We should be able to use two roles with the same requirements' { package My::Does::Basic3; - use Role::Tiny::Restricted; + use Role::Tiny; with 'My::Does::Basic2'; sub method3 { @@ -50,7 +50,7 @@ ok !$@, 'We should be able to use two roles with the same requirements' eval <<'END_PACKAGE'; package My::Class2; -use Role::Tiny::Restricted 'with'; +use Role::Tiny 'with'; with qw( My::Does::Basic3 ); @@ -82,12 +82,12 @@ ok !$object->Role::Tiny::does_role('My::Does::Basic1'), { { package Role::Which::Imports; - use Role::Tiny::Restricted allow => 'TestMethods'; + use Role::Tiny allow => 'TestMethods'; use TestMethods qw(this that); } { package Class::With::ImportingRole; - use Role::Tiny::Restricted 'with'; + use Role::Tiny 'with'; with 'Role::Which::Imports'; sub new { bless {} => shift } } @@ -103,12 +103,12 @@ ok !$object->Role::Tiny::does_role('My::Does::Basic1'), { { package Role::WithImportsOnceRemoved; - use Role::Tiny::Restricted; + use Role::Tiny; with 'Role::Which::Imports'; } { package Class::With::ImportingRole2; - use Role::Tiny::Restricted 'with'; + use Role::Tiny 'with'; $ENV{DEBUG} = 1; with 'Role::WithImportsOnceRemoved'; sub new { bless {} => shift } diff --git a/t/role-basic-exceptions.t b/t/role-basic-exceptions.t index 5a754bb..67af13a 100644 --- a/t/role-basic-exceptions.t +++ b/t/role-basic-exceptions.t @@ -2,12 +2,12 @@ use lib 'lib', 't/role-basic/lib'; use MyTests; -require Role::Tiny::Restricted; +require Role::Tiny; { package My::Does::Basic; - use Role::Tiny::Restricted; + use Role::Tiny; requires 'turbo_charger'; @@ -17,19 +17,8 @@ require Role::Tiny::Restricted; } eval <<'END_PACKAGE'; -package My::Bad::MultipleWith; -use Role::Tiny::Restricted::With; -with 'My::Does::Basic'; -with 'My::Does::Basic'; # can't use with() more than once -sub turbo_charger {} -END_PACKAGE -like $@, - qr/with\(\) may not be called more than once for My::Bad::MultipleWith/, - 'Trying to use with() more than once in a package should fail'; - -eval <<'END_PACKAGE'; package My::Bad::Requirement; -use Role::Tiny::Restricted::With; +use Role::Tiny::With; with 'My::Does::Basic'; # requires turbo_charger END_PACKAGE like $@, @@ -39,12 +28,12 @@ qr/missing turbo_charger/, { { package My::Conflict; - use Role::Tiny::Restricted; + use Role::Tiny; sub conflict {}; } eval <<' END_PACKAGE'; package My::Bad::MethodConflicts; - use Role::Tiny::Restricted::With; + use Role::Tiny::With; with qw(My::Does::Basic My::Conflict); sub turbo_charger {} END_PACKAGE @@ -57,19 +46,19 @@ qr/missing turbo_charger/, { { package Role1; - use Role::Tiny::Restricted; + use Role::Tiny; requires 'missing_method'; sub method1 { 'method1' } } { package Role2; - use Role::Tiny::Restricted; + use Role::Tiny; with 'Role1'; sub method2 { 'method2' } } eval <<" END"; package My::Class::Missing1; - use Role::Tiny::Restricted::With; + use Role::Tiny::With; with 'Role2'; END like $@, @@ -79,12 +68,12 @@ qr/missing turbo_charger/, { { package Role3; - use Role::Tiny::Restricted; + use Role::Tiny; requires qw(this that); } eval <<" END"; package My::Class::Missing2; - use Role::Tiny::Restricted::With; + use Role::Tiny::With; with 'Role3'; END like $@, diff --git a/t/role-duplication.t b/t/role-duplication.t new file mode 100644 index 0000000..f92d5eb --- /dev/null +++ b/t/role-duplication.t @@ -0,0 +1,14 @@ +use strictures 1; +use Test::More; + +{ + package Role; use Role::Tiny; + around foo => sub { my $orig = shift; 1 + $orig->(@_) }; + package Base; sub foo { 1 } +} + +eval { Role::Tiny->create_class_with_roles('Base', qw(Role Role)); }; + +like $@, 'Duplication', 'duplicate role detected'; + +done_testing;