From: Karen Etheridge Date: Wed, 3 Nov 2010 22:16:15 +0000 (-0700) Subject: new tests for default behaviour, using classes in first test file X-Git-Tag: 0.09~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=36bf5c4d40113630e8c3aabed77262a62a7d8428;p=gitmo%2FMooseX-UndefTolerant.git new tests for default behaviour, using classes in first test file --- diff --git a/t/attribute.t b/t/attribute.t index 43441cb..96b27a2 100644 --- a/t/attribute.t +++ b/t/attribute.t @@ -1,4 +1,4 @@ -use Test::More tests => 12; +use Test::More tests => 14; use Test::Fatal; # TODO: this test should be renamed constructor.t, since all it tests is @@ -12,13 +12,13 @@ use Test::Fatal; traits => [ qw(MooseX::UndefTolerant::Attribute)], is => 'ro', isa => 'Num', - predicate => 'has_attr1' + predicate => 'has_attr1', ); has 'attr2' => ( is => 'ro', isa => 'Num', - predicate => 'has_attr2' + predicate => 'has_attr2', ); } @@ -30,17 +30,20 @@ use Test::Fatal; has 'attr1' => ( is => 'ro', isa => 'Num', - predicate => 'has_attr1' + predicate => 'has_attr1', ); has 'attr2' => ( is => 'ro', isa => 'Num', - predicate => 'has_attr2' + predicate => 'has_attr2', ); } package main; +note 'Constructor behaviour'; +note ''; + note 'Testing class with a single UndefTolerant attribute'; { my $obj = Foo->new; @@ -56,9 +59,11 @@ note 'Testing class with a single UndefTolerant attribute'; } { - my $obj = Foo->new(attr1 => 1234); + 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'); } 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;