From: Karen Etheridge Date: Wed, 3 Nov 2010 22:36:51 +0000 (-0700) Subject: also test immutable classes using existing constructor and default classes -- X-Git-Tag: 0.09~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8055a64173c89e5129f8ec136acf49d6facab65d;p=gitmo%2FMooseX-UndefTolerant.git also test immutable classes using existing constructor and default classes -- these currently fail. the check for if all tests were succesfully run can be removed when TODO label comes off. --- diff --git a/t/constructor.t b/t/constructor.t index 62e8d69..a638cbf 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'); + 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 ''; + 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'); + } + + { + 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 + ok(! exception { do_tests }, 'tests do not die'); + + is(Test::More->builder->current_test, 28, 'if we got here, we can declare victory!'); } +done_testing; diff --git a/t/defaults.t b/t/defaults.t index b43d012..d599f20 100644 --- a/t/defaults.t +++ b/t/defaults.t @@ -1,4 +1,5 @@ -use Test::More tests => 22; +use Test::More; +use Test::Fatal; use MooseX::UndefTolerant::Attribute (); @@ -43,56 +44,76 @@ use MooseX::UndefTolerant::Attribute (); package main; -note 'Default behaviour'; -note ''; - -note 'Testing class with a single UndefTolerant attribute'; -{ - my $obj = Foo->new; - ok($obj->has_attr1, 'attr1 has a value'); - ok($obj->has_attr2, 'attr2 has a value'); - is($obj->attr1, 1, 'attr1\'s value is its default'); - is($obj->attr2, 2, 'attr2\'s value is its default'); -} - -{ - my $obj = Foo->new(attr1 => undef); - ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor'); - is($obj->attr1, 1, 'attr1\'s value is its default'); - is($obj->attr2, 2, 'attr2\'s value is its default'); -} - +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 a value'); + ok($obj->has_attr2, 'attr2 has a value'); + is($obj->attr1, 1, 'attr1\'s value is its default'); + is($obj->attr2, 2, 'attr2\'s value is its default'); + } + + { + my $obj = Foo->new(attr1 => undef); + ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor'); + is($obj->attr1, 1, 'attr1\'s value is its default'); + is($obj->attr2, 2, 'attr2\'s value is its default'); + } + + { + 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 a value'); + ok($obj->has_attr2, 'attr2 has a value'); + is($obj->attr1, 1, 'attr1\'s value is its default'); + is($obj->attr2, 2, 'attr2\'s value is its default'); + } + + { + my $obj = Bar->new(attr1 => undef); + ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor'); + is($obj->attr1, 1, 'attr1\'s value is its default'); + is($obj->attr2, 2, 'attr2\'s value is its default'); + } + + { + my $obj = Bar->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 'Default behaviour: mutable classes'; +note ''; +do_tests; note ''; -note 'Testing class with the entire class being UndefTolerant'; -{ - my $obj = Bar->new; - ok($obj->has_attr1, 'attr1 has a value'); - ok($obj->has_attr2, 'attr2 has a value'); - is($obj->attr1, 1, 'attr1\'s value is its default'); - is($obj->attr2, 2, 'attr2\'s value is its default'); -} +note 'Default behaviour: immutable classes'; +note ''; +Foo->meta->make_immutable; +Bar->meta->make_immutable; -{ - my $obj = Bar->new(attr1 => undef); - ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor'); - is($obj->attr1, 1, 'attr1\'s value is its default'); - is($obj->attr2, 2, 'attr2\'s value is its default'); -} +TODO: { + local $TODO = 'some immutable cases are not handled yet'; + # for now, catch errors + ok(! exception { do_tests }, 'tests do not die'); -{ - my $obj = Bar->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'); + is(Test::More->builder->current_test, 44, 'if we got here, we can declare victory!'); } +done_testing; +