X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F040_type_constraints%2F021_maybe_type_constraint.t;h=233a17fab36f94b55e1ffb4bf9b4c3b2b972abc7;hb=6cbf4a2359409fde77b74b2c131764611e6ea5ed;hp=c4f62d78bec4a1a4a81068ba0d0ca9838145d1c7;hpb=620db0454d31341c981eb9061132d4f3a08a7310;p=gitmo%2FMoose.git diff --git a/t/040_type_constraints/021_maybe_type_constraint.t b/t/040_type_constraints/021_maybe_type_constraint.t index c4f62d7..233a17f 100644 --- a/t/040_type_constraints/021_maybe_type_constraint.t +++ b/t/040_type_constraints/021_maybe_type_constraint.t @@ -3,13 +3,10 @@ use strict; use warnings; -use Test::More tests => 19; +use Test::More tests => 36; use Test::Exception; -BEGIN { - use_ok('Moose'); - use_ok('Moose::Util::TypeConstraints'); -} +use Moose::Util::TypeConstraints; my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]'); isa_ok($type, 'Moose::Meta::TypeConstraint'); @@ -29,25 +26,108 @@ ok(!$type->check('Hello World'), '... checked type correctly (fail)'); ok(!$type->check([]), '... checked type correctly (fail)'); { + package Bar; + use Moose; + package Foo; use Moose; - - has 'bar' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1); + use Moose::Util::TypeConstraints; + + has 'arr' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1); + has 'bar' => (is => 'rw', isa => class_type('Bar')); + has 'maybe_bar' => (is => 'rw', isa => maybe_type(class_type('Bar'))); } lives_ok { - Foo->new(bar => []); + Foo->new(arr => [], bar => Bar->new); +} '... Bar->new isa Bar'; + +dies_ok { + Foo->new(arr => [], bar => undef); +} '... undef isnta Bar'; + +lives_ok { + Foo->new(arr => [], maybe_bar => Bar->new); +} '... Bar->new isa maybe(Bar)'; + +lives_ok { + Foo->new(arr => [], maybe_bar => undef); +} '... undef isa maybe(Bar)'; + +dies_ok { + Foo->new(arr => [], maybe_bar => 1); +} '... 1 isnta maybe(Bar)'; + +lives_ok { + Foo->new(arr => []); } '... it worked!'; lives_ok { - Foo->new(bar => undef); + Foo->new(arr => undef); } '... it worked!'; dies_ok { - Foo->new(bar => 100); + Foo->new(arr => 100); } '... failed the type check'; dies_ok { - Foo->new(bar => 'hello world'); + Foo->new(arr => 'hello world'); } '... failed the type check'; + +{ + package Test::MooseX::Types::Maybe; + use Moose; + + has 'Maybe_Int' => (is=>'rw', isa=>'Maybe[Int]'); + has 'Maybe_ArrayRef' => (is=>'rw', isa=>'Maybe[ArrayRef]'); + has 'Maybe_HashRef' => (is=>'rw', isa=>'Maybe[HashRef]'); + has 'Maybe_ArrayRefInt' => (is=>'rw', isa=>'Maybe[ArrayRef[Int]]'); + has 'Maybe_HashRefInt' => (is=>'rw', isa=>'Maybe[HashRef[Int]]'); +} + +ok my $obj = Test::MooseX::Types::Maybe->new + => 'Create good test object'; + +## Maybe[Int] + +ok my $Maybe_Int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]') + => 'made TC Maybe[Int]'; + +ok $Maybe_Int->check(1) + => 'passed (1)'; + +ok $obj->Maybe_Int(1) + => 'assigned (1)'; + +ok $Maybe_Int->check() + => 'passed ()'; + +ok $obj->Maybe_Int() + => 'assigned ()'; + +ok $Maybe_Int->check(0) + => 'passed (0)'; + +ok defined $obj->Maybe_Int(0) + => 'assigned (0)'; + +ok $Maybe_Int->check(undef) + => 'passed (undef)'; + +ok sub {$obj->Maybe_Int(undef); 1}->() + => 'assigned (undef)'; + +ok !$Maybe_Int->check("") + => 'failed ("")'; + +throws_ok sub { $obj->Maybe_Int("") }, + qr/Attribute \(Maybe_Int\) does not pass the type constraint/ + => 'failed assigned ("")'; + +ok !$Maybe_Int->check("a") + => 'failed ("a")'; + +throws_ok sub { $obj->Maybe_Int("a") }, + qr/Attribute \(Maybe_Int\) does not pass the type constraint/ + => 'failed assigned ("a")';