From: John Napiorkowski Date: Tue, 30 Sep 2008 21:18:21 +0000 (+0000) Subject: a bunch more tests for the Maybe constraint that I wrote while trying to figure out... X-Git-Tag: 0.59~25 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f805496a4e3753c012def64836f5dc2f37b3242d;p=gitmo%2FMoose.git a bunch more tests for the Maybe constraint that I wrote while trying to figure out the correct way to write the new Optional constraint. Why waste them? --- diff --git a/t/040_type_constraints/021_maybe_type_constraint.t b/t/040_type_constraints/021_maybe_type_constraint.t index 06528d3..e97986c 100644 --- a/t/040_type_constraints/021_maybe_type_constraint.t +++ b/t/040_type_constraints/021_maybe_type_constraint.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 17; +use Test::More tests => 31; use Test::Exception; use Moose::Util::TypeConstraints; @@ -48,3 +48,60 @@ dies_ok { Foo->new(bar => '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")'; \ No newline at end of file