X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fdefaults.t;h=bd0a6ada601d423baff1b63527993941d47d86f7;hb=581c4a1866ff70f8b5a29918bf773a882c6f8e61;hp=b43d01262bce5982f4d3e70baea0f2993ccb6165;hpb=36bf5c4d40113630e8c3aabed77262a62a7d8428;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/t/defaults.t b/t/defaults.t index b43d012..bd0a6ad 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,61 @@ use MooseX::UndefTolerant::Attribute (); package main; -note 'Default behaviour'; -note ''; - -note 'Testing class with a single UndefTolerant attribute'; +sub do_tests { - 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'); -} + note 'Testing class with a single UndefTolerant attribute'; + do_tests_with_class('Foo'); -{ - 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'); + note ''; + note 'Testing class with the entire class being UndefTolerant'; + do_tests_with_class('Bar'); } +sub do_tests_with_class { - 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'); + my $class = shift; + + { + my $obj = $class->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 = $class->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 = $class->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; +