From: Karen Etheridge Date: Wed, 3 Nov 2010 22:04:58 +0000 (-0700) Subject: more robust testing of construction behaviour; labelled and counted tests X-Git-Tag: 0.09~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-UndefTolerant.git;a=commitdiff_plain;h=5cc8d5b3da39c491fb32c3b65c2cd6da17b8926d more robust testing of construction behaviour; labelled and counted tests --- diff --git a/t/attribute.t b/t/attribute.t index 7458ff6..43441cb 100644 --- a/t/attribute.t +++ b/t/attribute.t @@ -1,46 +1,86 @@ -use Test::More; +use Test::More tests => 12; +use Test::Fatal; -package Foo; -use Moose; +# TODO: this test should be renamed constructor.t, since all it tests is +# UT behaviour during construction. -has 'bar' => ( - traits => [ qw(MooseX::UndefTolerant::Attribute)], - is => 'ro', - isa => 'Num', - predicate => 'has_bar' -); +{ + package Foo; + use Moose; + + has 'attr1' => ( + traits => [ qw(MooseX::UndefTolerant::Attribute)], + is => 'ro', + isa => 'Num', + predicate => 'has_attr1' + ); -package Foo2; -use Moose; -use MooseX::UndefTolerant; + has 'attr2' => ( + is => 'ro', + isa => 'Num', + predicate => 'has_attr2' + ); +} -has 'bar' => ( - is => 'ro', - isa => 'Num', - predicate => 'has_bar' -); +{ + 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; +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 $foo = Foo->new; - ok(!$foo->has_bar); + 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 $foo = Foo->new(bar => undef); - ok(!$foo->has_bar); + my $obj = Foo->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 'Testing class with the entire class being UndefTolerant'; { - my $foo = Foo2->new(bar => undef); - ok(!$foo->has_bar); + my $obj = Bar->new; + ok(!$obj->has_attr1, 'attr1 has no value before it is assigned'); } { - my $foo = Foo2->new(bar => 1234); - cmp_ok($foo->bar, 'eq', 1234); - ok($foo->has_bar); + 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'); } -done_testing; +{ + 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'); +} + +