From: Shawn M Moore Date: Thu, 27 Mar 2008 03:25:11 +0000 (+0000) Subject: Test for narrowing "Str | Int | ArrayRef" down to "Int | ArrayRef" too X-Git-Tag: 0_55~255 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=68e9fbfcdb6a582394fb92b40b73a4c4f7202d8b;p=gitmo%2FMoose.git Test for narrowing "Str | Int | ArrayRef" down to "Int | ArrayRef" too --- diff --git a/t/030_roles/017_extending_role_attrs.t b/t/030_roles/017_extending_role_attrs.t index 4e81d3d..5d09f70 100644 --- a/t/030_roles/017_extending_role_attrs.t +++ b/t/030_roles/017_extending_role_attrs.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 17; use Test::Exception; BEGIN { @@ -73,3 +73,36 @@ 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"); +