X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F030_roles%2F013_method_aliasing_in_composition.t;h=c6146a732cbbca0c70f73e1d449a24ad323039ad;hb=143fb6b0b23c6b2491726f598d658a71c647764f;hp=787b2bb3366e28699cc7f90a7ef697fbfaba3ee9;hpb=353d4b3b3ee3b009faf6125c6a1b48abc966739f;p=gitmo%2FMoose.git diff --git a/t/030_roles/013_method_aliasing_in_composition.t b/t/030_roles/013_method_aliasing_in_composition.t index 787b2bb..c6146a7 100644 --- a/t/030_roles/013_method_aliasing_in_composition.t +++ b/t/030_roles/013_method_aliasing_in_composition.t @@ -3,9 +3,8 @@ use strict; use warnings; -use Test::More tests => 35; -use Test::Exception; - +use Test::More; +use Test::Fatal; { @@ -21,16 +20,16 @@ use Test::Exception; package My::Class; use Moose; - ::lives_ok { - with 'My::Role' => { alias => { bar => 'role_bar' } }; - } '... this succeeds'; + ::is( ::exception { + with 'My::Role' => { -alias => { bar => 'role_bar' } }; + }, undef, '... this succeeds' ); package My::Class::Failure; use Moose; - ::throws_ok { - with 'My::Role' => { alias => { bar => 'role_bar' } }; - } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds'; + ::like( ::exception { + with 'My::Role' => { -alias => { bar => 'role_bar' } }; + }, qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds' ); sub role_bar { 'FAIL' } } @@ -41,37 +40,37 @@ ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar ro package My::OtherRole; use Moose::Role; - ::lives_ok { - with 'My::Role' => { alias => { bar => 'role_bar' } }; - } '... this succeeds'; + ::is( ::exception { + with 'My::Role' => { -alias => { bar => 'role_bar' } }; + }, undef, '... this succeeds' ); sub bar { 'My::OtherRole::bar' } package My::OtherRole::Failure; use Moose::Role; - ::throws_ok { - with 'My::Role' => { alias => { bar => 'role_bar' } }; - } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds'; + ::like( ::exception { + with 'My::Role' => { -alias => { bar => 'role_bar' } }; + }, qr/Cannot create a method alias if a local method of the same name exists/, '... cannot alias to a name that exists' ); sub role_bar { 'FAIL' } } 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'); { package My::AliasingRole; use Moose::Role; - ::lives_ok { - with 'My::Role' => { alias => { bar => 'role_bar' } }; - } '... this succeeds'; + ::is( ::exception { + with 'My::Role' => { -alias => { bar => 'role_bar' } }; + }, undef, '... this succeeds' ); } 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; @@ -92,21 +91,20 @@ ok(My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is r package My::Foo::Class; use Moose; - ::lives_ok { - with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, - 'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' }, + ::is( ::exception { + with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' }, + 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' }, 'Baz::Role'; - } '... composed our roles correctly'; + }, undef, '... composed our roles correctly' ); package My::Foo::Class::Broken; use Moose; - ::throws_ok { - with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, - 'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, + ::like( ::exception { + with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' }, + 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' }, 'Baz::Role'; - } qr/Due to a method name conflict in roles 'Bar::Role' and 'Foo::Role', the method 'foo_foo' must be implemented or excluded by 'My::Foo::Class::Broken'/, - '... composed our roles correctly'; + }, qr/Due to a method name conflict in roles 'Bar::Role' and 'Foo::Role', the method 'foo_foo' must be implemented or excluded by 'My::Foo::Class::Broken'/, '... composed our roles correctly' ); } { @@ -122,11 +120,11 @@ ok(My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is r package My::Foo::Role; use Moose::Role; - ::lives_ok { - with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, - 'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' }, + ::is( ::exception { + with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' }, + 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' }, 'Baz::Role'; - } '... composed our roles correctly'; + }, undef, '... composed our roles correctly' ); } ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;; @@ -137,13 +135,74 @@ ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not package My::Foo::Role::Other; use Moose::Role; - ::lives_ok { - with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, - 'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, + ::is( ::exception { + with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' }, + 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' }, 'Baz::Role'; - } '... composed our roles correctly'; + }, undef, '... composed our roles correctly' ); } ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method"); ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required'); +{ + package My::Foo::AliasOnly; + use Moose; + + ::is( ::exception { + with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' } }, + }, undef, '... composed our roles correctly' ); +} + +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; + + ::is( ::exception { + with 'Role::Foo' => { + -alias => { x1 => 'foo_x1' }, + -excludes => ['y1'], + }; + }, undef, '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; + + ::is( ::exception { + with 'Role::Foo' => { + -alias => { x1 => 'foo_x1' }, + -excludes => ['y1'], + }; + }, undef, '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' ); +} + +done_testing;