X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fconstructor.t;h=ca5131566739d00f8a2f990d7edd5fb33ba4e08e;hb=015b91679983f142397cd7172262a5e56f3cd850;hp=62e8d696f3b52a952bd68eb853a9ac0b89808301;hpb=4635347295081d0588159329d164b78260c6838d;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/t/constructor.t b/t/constructor.t index 62e8d69..ca51315 100644 --- a/t/constructor.t +++ b/t/constructor.t @@ -1,4 +1,4 @@ -use Test::More tests => 14; +use Test::More; use Test::Fatal; { @@ -38,51 +38,70 @@ use Test::Fatal; package main; -note 'Constructor behaviour'; -note ''; - -note 'Testing class with a single UndefTolerant attribute'; -{ - 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'); -} - -{ - my $obj = Foo->new(attr1 => undef); - ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor'); - ok (exception { $obj = Foo->new(attr2 => undef) }, - 'But assigning undef to attr2 generates a type constraint error'); -} - +sub do_tests { - my $obj = Foo->new(attr1 => 1234, attr2 => 5678); - 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'); + note 'Testing class with a single UndefTolerant attribute'; + { + 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'); + } + + { + 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, + 'But assigning undef to attr2 generates a type constraint error'); + } + + { + my $obj = Foo->new(attr1 => 1234, attr2 => 5678); + 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'); + } + + + note ''; + note 'Testing class with the entire class being UndefTolerant'; + { + my $obj = Bar->new; + ok(!$obj->has_attr1, 'attr1 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'); + 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'); + } + + { + my $obj = Bar->new(attr1 => 1234); + is($obj->attr1, 1234, 'assigning a defined value during construction works as normal'); + ok($obj->has_attr1, '...and the predicate returns true as normal'); + } } +note 'Constructor behaviour: mutable classes'; note ''; -note 'Testing class with the entire class being UndefTolerant'; -{ - my $obj = Bar->new; - ok(!$obj->has_attr1, 'attr1 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'); - ok (!exception { $obj = Bar->new(attr2 => undef) }, - 'assigning undef to attr2 does not produce an error'); - ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor'); -} +do_tests; -{ - my $obj = Bar->new(attr1 => 1234); - is($obj->attr1, 1234, 'assigning a defined value during construction works as normal'); - ok($obj->has_attr1, '...and the predicate returns true as normal'); +note ''; +note 'Constructor behaviour: immutable classes'; +note ''; +Foo->meta->make_immutable; +Bar->meta->make_immutable; +TODO: { + local $TODO = 'some immutable cases are not handled yet'; + # for now, catch errors + is(exception { do_tests }, undef, 'tests do not die'); + + is(Test::More->builder->current_test, 28, 'if we got here, we can declare victory!'); } +done_testing;