X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fconstructor.t;h=810f3f91f4e5768dc5260c9a43aff5ac435505cb;hb=0833a4a7f2ffbf6290fd90fdfb14c38da47211a5;hp=96b27a2fbd47ef7074ede86aaf19f998c66d1c85;hpb=5fa797153fd062a0b9ea4d57bf2baf78b5510e88;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/t/constructor.t b/t/constructor.t index 96b27a2..810f3f9 100644 --- a/t/constructor.t +++ b/t/constructor.t @@ -1,91 +1,15 @@ -use Test::More tests => 14; -use Test::Fatal; +use strict; +use warnings; -# TODO: this test should be renamed constructor.t, since all it tests is -# UT behaviour during construction. +use Test::More; +use Test::Moose; -{ - package Foo; - use Moose; +use lib 't/lib'; +use ConstructorTests; - has 'attr1' => ( - traits => [ qw(MooseX::UndefTolerant::Attribute)], - is => 'ro', - isa => 'Num', - predicate => 'has_attr1', - ); +with_immutable { ConstructorTests::do_tests() } qw(Foo Bar); - 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; - -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'); -} - -{ - 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 'Ran ', Test::More->builder->current_test, ' tests - should have run 56'; +done_testing;