X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F050_metaclasses%2F013_metaclass_traits.t;h=782323757e3a1231137fcbda91144f4f953be7b9;hb=be0ed15704fdad5f2d8517380a6f24687432c1dd;hp=950665cf60ef4afa0426a08ab5476c9d52b7f1ef;hpb=d9a57e33ac3d162111ff769e64057e238cb7b493;p=gitmo%2FMoose.git diff --git a/t/050_metaclasses/013_metaclass_traits.t b/t/050_metaclasses/013_metaclass_traits.t index 950665c..7823237 100644 --- a/t/050_metaclasses/013_metaclass_traits.t +++ b/t/050_metaclasses/013_metaclass_traits.t @@ -3,8 +3,10 @@ use strict; use warnings; -use Test::More tests => 28; -use Test::Exception; +use lib 't/lib', 'lib'; + +use Test::More; +use Test::Fatal; { package My::SimpleTrait; @@ -165,9 +167,10 @@ is( Role::Foo->meta()->simple(), 5, { require Moose::Util::TypeConstraints; - dies_ok( sub { Moose::Util::TypeConstraints->import( -traits => 'My::SimpleTrait' ) }, - 'cannot provide -traits to an exporting module that does not init_meta' ); - like( $@, qr/does not have an init_meta/, + my $error; + ok($error = exception { Moose::Util::TypeConstraints->import( -traits => 'My::SimpleTrait' ) }, + 'cannot provide -traits to an exporting module that does not init_meta'); + like( $error, qr/does not have an init_meta/, '... and error provides a useful explanation' ); } @@ -188,3 +191,35 @@ can_ok( Foo::Subclass->meta(), 'attr2' ); is( Foo::Subclass->meta()->attr2(), 'something', 'Foo::Subclass->meta()->attr2() returns expected value' ); +{ + + package Class::WithAlreadyPresentTrait; + use Moose -traits => 'My::SimpleTrait'; + + has an_attr => ( is => 'ro' ); +} + +ok ! exception { + my $instance = Class::WithAlreadyPresentTrait->new( an_attr => 'value' ); + is( $instance->an_attr, 'value', 'Can get value' ); +}, +'Can create instance and access attributes'; + +{ + + package Class::WhichLoadsATraitFromDisk; + + # Any role you like here, the only important bit is that it gets + # loaded from disk and has not already been defined. + use Moose -traits => 'Role::Parent'; + + has an_attr => ( is => 'ro' ); +} + +ok ! exception { + my $instance = Class::WhichLoadsATraitFromDisk->new( an_attr => 'value' ); + is( $instance->an_attr, 'value', 'Can get value' ); +}, +'Can create instance and access attributes'; + +done_testing;