Implementation of TypeConstraint::Union->includes_type, and use it in Attribute.
[gitmo/Moose.git] / t / 030_roles / 017_extending_role_attrs.t
index 4e81d3d..55d1410 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 10;
+use Test::More tests => 29;
 use Test::Exception;
 
 BEGIN {
@@ -73,3 +73,105 @@ 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 /;
 is($bar->foo, 100, "... still has the old Int value");
 
+{
+    package Baz::Role;
+    use Moose::Role;
+
+    has 'baz' => (
+        is      => 'rw',
+        isa     => 'Str | Int | ArrayRef',
+    );
+
+    package Baz;
+    use Moose;
+
+    with 'Baz::Role';
+
+    ::lives_ok {
+        has '+baz' => (
+            isa => 'Int | ArrayRef',
+        )
+    } "... narrowed the role's type constraint successfully";
+}
+
+
+my $baz = Baz->new(baz => 99);
+isa_ok($baz, 'Baz');
+is($baz->baz, 99, '... got the extended attribute');
+$baz->baz(100);
+is($baz->baz, 100, "... can change the attribute's value to an Int");
+$baz->baz(["hi"]);
+is_deeply($baz->baz, ["hi"], "... can change the attribute's value to an ArrayRef");
+
+throws_ok { $baz->baz("quux") } qr/^Attribute \(baz\) does not pass the type constraint because: Validation failed for 'Int \| ArrayRef' failed with value quux at /;
+is_deeply($baz->baz, ["hi"], "... still has the old ArrayRef value");
+
+{
+    package Quux::Role;
+    use Moose::Role;
+
+    has 'quux' => (
+        is      => 'rw',
+        isa     => 'Str | Int | Ref',
+    );
+
+    package Quux;
+    use Moose;
+    use Moose::Util::TypeConstraints;
+
+    with 'Quux::Role';
+
+    subtype 'Positive'
+        => as 'Int'
+        => where { $_ > 0 };
+
+    ::lives_ok {
+        has '+quux' => (
+            isa => 'Positive | ArrayRef',
+        )
+    } "... narrowed the role's type constraint successfully";
+}
+
+
+my $quux = Quux->new(quux => 99);
+isa_ok($quux, 'Quux');
+is($quux->quux, 99, '... got the extended attribute');
+$quux->quux(100);
+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 /;
+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 /;
+is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value");
+
+{
+    package Err::Role;
+    use Moose::Role;
+
+    has "err" => (
+        isa => 'Str | Int',
+    );
+
+    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/;
+
+    ::throws_ok {
+        has '+err' => (isa => 'Defined');
+    } $error, "must get more specific, not less specific";
+
+    ::throws_ok {
+        has '+err' => (isa => 'Bool');
+    } $error, "the type has to be a part of the union";
+
+    ::throws_ok {
+        has '+err' => (isa => 'Str | ArrayRef');
+    } $error, "can't add new types to the union";
+}
+