X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fconstructor.t;h=810f3f91f4e5768dc5260c9a43aff5ac435505cb;hb=0dc46003eb11174f2c434b237f4b679935939a96;hp=ca5131566739d00f8a2f990d7edd5fb33ba4e08e;hpb=015b91679983f142397cd7172262a5e56f3cd850;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/t/constructor.t b/t/constructor.t index ca51315..810f3f9 100644 --- a/t/constructor.t +++ b/t/constructor.t @@ -1,107 +1,15 @@ -use Test::More; -use Test::Fatal; - -{ - package Foo; - use Moose; - - has 'attr1' => ( - traits => [ qw(MooseX::UndefTolerant::Attribute)], - is => 'ro', - isa => 'Num', - predicate => 'has_attr1', - ); - - has 'attr2' => ( - is => 'ro', - isa => 'Num', - predicate => 'has_attr2', - ); -} - -{ - package Bar; - use Moose; - use MooseX::UndefTolerant; - - has 'attr1' => ( - is => 'ro', - isa => 'Num', - predicate => 'has_attr1', - ); - has 'attr2' => ( - is => 'ro', - isa => 'Num', - predicate => 'has_attr2', - ); -} - -package main; - -sub do_tests -{ - 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'); - } -} +use strict; +use warnings; +use Test::More; +use Test::Moose; -note 'Constructor behaviour: mutable classes'; -note ''; -do_tests; +use lib 't/lib'; +use ConstructorTests; -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'); +with_immutable { ConstructorTests::do_tests() } qw(Foo Bar); - is(Test::More->builder->current_test, 28, 'if we got here, we can declare victory!'); -} +note 'Ran ', Test::More->builder->current_test, ' tests - should have run 56'; done_testing;