X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fconstructor.t;fp=t%2Fconstructor.t;h=e97766ef0ec0aaa87dacb38122ac748006842098;hb=3991a5d2d6058c15d85b028dae0c548922c38dbe;hp=ca5131566739d00f8a2f990d7edd5fb33ba4e08e;hpb=015b91679983f142397cd7172262a5e56f3cd850;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/t/constructor.t b/t/constructor.t index ca51315..e97766e 100644 --- a/t/constructor.t +++ b/t/constructor.t @@ -1,4 +1,5 @@ -use Test::More; +#use Test::More; +use Test::Most 'die'; use Test::Fatal; { @@ -11,12 +12,16 @@ use Test::Fatal; isa => 'Num', predicate => 'has_attr1', ); - has 'attr2' => ( is => 'ro', isa => 'Num', predicate => 'has_attr2', ); + has 'attr3' => ( + is => 'ro', + isa => 'Maybe[Num]', + predicate => 'has_attr3', + ); } { @@ -34,6 +39,11 @@ use Test::Fatal; isa => 'Num', predicate => 'has_attr2', ); + has 'attr3' => ( + is => 'ro', + isa => 'Maybe[Num]', + predicate => 'has_attr3', + ); } package main; @@ -45,21 +55,30 @@ sub do_tests my $obj = Foo->new; ok(!$obj->has_attr1, 'attr1 has no value before it is assigned'); ok(!$obj->has_attr2, 'attr2 has no value before it is assigned'); + ok(!$obj->has_attr3, 'attr3 has no value before it is assigned'); } { my $obj = Foo->new(attr1 => undef); ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor'); - is (exception { $obj = Foo->new(attr2 => undef) }, undef, + isnt (exception { $obj = Foo->new(attr2 => undef) }, undef, 'But assigning undef to attr2 generates a type constraint error'); + + is (exception { $obj = Foo->new(attr3 => undef) }, undef, + 'assigning undef to attr3 is acceptable'); + ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor'); } { - my $obj = Foo->new(attr1 => 1234, attr2 => 5678); + my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012); is($obj->attr1, 1234, 'assigning a defined value during construction works as normal'); ok($obj->has_attr1, '...and the predicate returns true as normal'); + is($obj->attr2, 5678, 'assigning a defined value during construction works as normal'); ok($obj->has_attr2, '...and the predicate returns true as normal'); + + is($obj->attr3, 9012, 'assigning a defined value during construction works as normal'); + ok($obj->has_attr3, '...and the predicate returns true as normal'); } @@ -68,20 +87,33 @@ sub do_tests { my $obj = Bar->new; ok(!$obj->has_attr1, 'attr1 has no value before it is assigned'); + ok(!$obj->has_attr2, 'attr2 has no value before it is assigned'); + ok(!$obj->has_attr3, 'attr3 has no value before it is assigned'); } { my $obj = Bar->new(attr1 => undef); ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor'); + # note this test differs from the Foo case above is (exception { $obj = Bar->new(attr2 => undef) }, undef, 'assigning undef to attr2 does not produce an error'); ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor'); + + is( exception { $obj = Foo->new(attr3 => undef) }, undef, + 'assigning undef to attr3 is acceptable'); + ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor'); } { - my $obj = Bar->new(attr1 => 1234); + my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012); is($obj->attr1, 1234, 'assigning a defined value during construction works as normal'); ok($obj->has_attr1, '...and the predicate returns true as normal'); + + is($obj->attr2, 5678, 'assigning a defined value during construction works as normal'); + ok($obj->has_attr2, '...and the predicate returns true as normal'); + + is($obj->attr3, 9012, 'assigning a defined value during construction works as normal'); + ok($obj->has_attr3, '...and the predicate returns true as normal'); } }