Fix bug with -traits to Moose
[gitmo/Moose.git] / t / 050_metaclasses / 013_metaclass_traits.t
index f6519f3..9211f33 100644 (file)
@@ -3,7 +3,10 @@
 use strict;
 use warnings;
 
-use Test::More 'no_plan';
+use lib 't/lib', 'lib';
+
+use Test::More tests => 32;
+use Test::Exception;
 
 {
     package My::SimpleTrait;
@@ -24,6 +27,16 @@ is( Foo->meta()->simple(), 5,
     'Foo->meta()->simple() returns expected value' );
 
 {
+    package Bar;
+
+    use Moose -traits => 'My::SimpleTrait';
+}
+
+can_ok( Bar->meta(), 'simple' );
+is( Bar->meta()->simple(), 5,
+    'Foo->meta()->simple() returns expected value' );
+
+{
     package My::SimpleTrait2;
 
     use Moose::Role;
@@ -151,3 +164,59 @@ ok( RanOutOfNames->meta()->meta()->has_method('whatever'),
 can_ok( Role::Foo->meta(), 'simple' );
 is( Role::Foo->meta()->simple(), 5,
     'Role::Foo->meta()->simple() returns expected value' );
+
+{
+    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/,
+          '... and error provides a useful explanation' );
+}
+
+{
+    package Foo::Subclass;
+
+    use Moose -traits => [ 'My::SimpleTrait3' ];
+
+    extends 'Foo';
+}
+
+can_ok( Foo::Subclass->meta(), 'simple' );
+is( Foo::Subclass->meta()->simple(), 5,
+    'Foo::Subclass->meta()->simple() returns expected value' );
+is( Foo::Subclass->meta()->simple2(), 55,
+    'Foo::Subclass->meta()->simple2() returns expected value' );
+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' );
+}
+
+lives_ok {
+    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' );
+}
+
+lives_ok {
+    my $instance = Class::WhichLoadsATraitFromDisk->new( an_attr => 'value' );
+    is( $instance->an_attr, 'value', 'Can get value' );
+}
+'Can create instance and access attributes';