X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fdefaults.t;h=b43d01262bce5982f4d3e70baea0f2993ccb6165;hb=4635347295081d0588159329d164b78260c6838d;hp=aa8680a92db4e113fd61d7931c36465e88c87c25;hpb=779ca481dfc39471fbcbe33772436cf72daac821;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/t/defaults.t b/t/defaults.t index aa8680a..b43d012 100644 --- a/t/defaults.t +++ b/t/defaults.t @@ -1,23 +1,98 @@ -#!/usr/bin/perl +use Test::More tests => 22; -use Test::More; +use MooseX::UndefTolerant::Attribute (); -package Foo; +{ + package Foo; + use Moose; -use Moose; -use MooseX::UndefTolerant::Attribute; + has 'attr1' => ( + traits => [ qw(MooseX::UndefTolerant::Attribute)], + is => 'ro', + isa => 'Num', + predicate => 'has_attr1', + default => 1, + ); + has 'attr2' => ( + is => 'ro', + isa => 'Num', + predicate => 'has_attr2', + default => 2, + ); +} -has bar => ( - is => 'rw', - traits => ['MooseX::UndefTolerant::Attribute'], - default => 'baz' -); +{ + package Bar; + use Moose; + use MooseX::UndefTolerant; + + has 'attr1' => ( + is => 'ro', + isa => 'Num', + predicate => 'has_attr1', + default => 1, + ); + has 'attr2' => ( + is => 'ro', + isa => 'Num', + predicate => 'has_attr2', + default => 2, + ); +} -1; package main; -my $foo = Foo->new( bar => undef ); -is ( $foo->bar, 'baz', 'does the default value get set when passing undef in the constructor' ); +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'); +} + +{ + 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'); +} -done_testing;