X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fconstructor.t;h=22d4ce216c526026316da15b5bef2606f75574a4;hb=0dd9c65b0cd3941fe1901f40045e5fa6732e7692;hp=96b27a2fbd47ef7074ede86aaf19f998c66d1c85;hpb=5fa797153fd062a0b9ea4d57bf2baf78b5510e88;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/t/constructor.t b/t/constructor.t index 96b27a2..22d4ce2 100644 --- a/t/constructor.t +++ b/t/constructor.t @@ -1,9 +1,6 @@ -use Test::More tests => 14; +use Test::More; use Test::Fatal; -# TODO: this test should be renamed constructor.t, since all it tests is -# UT behaviour during construction. - { package Foo; use Moose; @@ -14,12 +11,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', + ); } { @@ -37,55 +38,101 @@ use Test::Fatal; isa => 'Num', predicate => 'has_attr2', ); + has 'attr3' => ( + is => 'ro', + isa => 'Maybe[Num]', + predicate => 'has_attr3', + ); } 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'); -} - +sub do_tests { - 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'); -} - -{ - 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'); + 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'); + 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, 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'); + } + + + 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'); + 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, 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'); + } } +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'); -} +do_tests; -{ - 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'); -} - -{ - 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;