test array delegation edge cases
[gitmo/Moose.git] / t / 030_roles / 017_extending_role_attrs.t
index 35138b6..16bc2e7 100644 (file)
@@ -3,16 +3,13 @@
 use strict;
 use warnings;
 
-use Test::More tests => 28;
+use Test::More;
 use Test::Exception;
 
-BEGIN {
-    use_ok('Moose');
-}
 
 =pod
 
-This basically just makes sure that using +name 
+This basically just makes sure that using +name
 on role attributes works right.
 
 =cut
@@ -20,21 +17,21 @@ on role attributes works right.
 {
     package Foo::Role;
     use Moose::Role;
-    
+
     has 'bar' => (
         is      => 'rw',
-        isa     => 'Int',   
+        isa     => 'Int',
         default => sub { 10 },
     );
-    
+
     package Foo;
     use Moose;
-    
+
     with 'Foo::Role';
-    
+
     ::lives_ok {
         has '+bar' => (default => sub { 100 });
-    } '... extended the attribute successfully';  
+    } '... extended the attribute successfully';
 }
 
 my $foo = Foo->new;
@@ -70,7 +67,7 @@ is($bar->foo, 42, '... got the extended attribute');
 $bar->foo(100);
 is($bar->foo, 100, "... can change the attribute's value to an Int");
 
-throws_ok { $bar->foo("baz") } qr/^Attribute \(foo\) does not pass the type constraint because: Validation failed for 'Int' failed with value baz at /;
+throws_ok { $bar->foo("baz") } qr/^Attribute \(foo\) does not pass the type constraint because: Validation failed for 'Int' with value baz at /;
 is($bar->foo, 100, "... still has the old Int value");
 
 
@@ -101,7 +98,7 @@ is($baz->baz, 99, '... got the extended attribute');
 $baz->baz('Foo');
 is($baz->baz, 'Foo', "... can change the attribute's value to a ClassName");
 
-throws_ok { $baz->baz("zonk") } qr/^Attribute \(baz\) does not pass the type constraint because: Validation failed for 'Int \| ClassName' failed with value zonk at /;
+throws_ok { $baz->baz("zonk") } qr/^Attribute \(baz\) does not pass the type constraint because: Validation failed for 'ClassName\|Int' with value zonk at /;
 is_deeply($baz->baz, 'Foo', "... still has the old ClassName value");
 
 
@@ -139,10 +136,10 @@ is($quux->quux, 100, "... can change the attribute's value to an Int");
 $quux->quux(["hi"]);
 is_deeply($quux->quux, ["hi"], "... can change the attribute's value to an ArrayRef");
 
-throws_ok { $quux->quux("quux") } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'Positive \| ArrayRef' failed with value quux at /;
+throws_ok { $quux->quux("quux") } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'ArrayRef\|Positive' with value quux at /;
 is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value");
 
-throws_ok { $quux->quux({a => 1}) } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'Positive \| ArrayRef' failed with value HASH\(\w+\) at /;
+throws_ok { $quux->quux({a => 1}) } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'ArrayRef\|Positive' with value HASH\(\w+\) at /;
 is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value");
 
 
@@ -150,27 +147,41 @@ is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value");
     package Err::Role;
     use Moose::Role;
 
-    has "err" => (
-        isa => 'Str | Int',
-    );
+    for (1..3) {
+        has "err$_" => (
+            isa => 'Str | Int',
+            is => 'bare',
+        );
+    }
 
     package Err;
     use Moose;
 
     with 'Err::Role';
 
-    my $error = qr/New type constraint setting must be a subtype of inherited one, or included in the inherited constraint/;
+    ::lives_ok {
+        has '+err1' => (isa => 'Defined');
+    } "can get less specific in the subclass";
 
-    ::throws_ok {
-        has '+err' => (isa => 'Defined');
-    } $error, "must get more specific, not less specific";
+    ::lives_ok {
+        has '+err2' => (isa => 'Bool');
+    } "or change the type completely";
 
-    ::throws_ok {
-        has '+err' => (isa => 'Bool');
-    } $error, "the type has to be a part of the union";
+    ::lives_ok {
+        has '+err3' => (isa => 'Str | ArrayRef');
+    } "or add new types to the union";
+}
+
+{
+    package Role::With::PlusAttr;
+    use Moose::Role;
+
+    with 'Foo::Role';
 
     ::throws_ok {
-        has '+err' => (isa => 'Str | ArrayRef');
-    } $error, "can't add new types to the union";
+        has '+bar' => ( is => 'ro' );
+    } qr/has '\+attr' is not supported in roles/,
+        "Test has '+attr' in roles explodes";
 }
 
+done_testing;