X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F13_typedecorator.t;h=e22aca592615858373f1892162d2e85732c8a5f9;hb=af976fb2fcd78e7728ea17669c5304bd33f6a61f;hp=585dce244ab3c907853526a3bc0e483705f660b7;hpb=cf1a8bfa50cb6cab796582ddae0a5b05dfcd8759;p=gitmo%2FMooseX-Types.git diff --git a/t/13_typedecorator.t b/t/13_typedecorator.t index 585dce2..e22aca5 100644 --- a/t/13_typedecorator.t +++ b/t/13_typedecorator.t @@ -2,7 +2,7 @@ use warnings; use strict; -use Test::More tests => 29; +use Test::More tests => 52; use Test::Exception; use FindBin; use lib "$FindBin::Bin/lib"; @@ -12,10 +12,11 @@ use lib "$FindBin::Bin/lib"; use Moose; use MooseX::Types::Moose qw( - Int + Int Str ArrayRef HashRef Object ); use DecoratorLibrary qw( MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef + AtLeastOneInt Jobs SubOfMyArrayRefInt01 ); has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1); @@ -23,6 +24,12 @@ use lib "$FindBin::Bin/lib"; has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1); has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]); has 'StrOrArrayRef' => (is=>'rw', isa=>StrOrArrayRef); + has 'AtLeastOneInt' => (is=>'rw', isa=>AtLeastOneInt); + has 'pipeoverloading' => (is=>'rw', isa=>Int|Str); + has 'deep' => (is=>'rw', isa=>ArrayRef[ArrayRef[HashRef[Int]]] ); + has 'deep2' => (is=>'rw', isa=>ArrayRef[Int|ArrayRef[HashRef[Int|Object]]] ); + has 'enum' => (is=>'rw', isa=>Jobs); + has 'SubOfMyArrayRefInt01_attr' => (is=>'rw', isa=>SubOfMyArrayRefInt01); } ## Make sure we have a 'create object sanity check' @@ -35,10 +42,10 @@ isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator' ## test arrayrefbase normal and coercion -ok $type->arrayrefbase([qw(a b c)]) - => 'Assigned arrayrefbase qw(a b c)'; +ok $type->arrayrefbase([qw(a b c d e)]) + => 'Assigned arrayrefbase qw(a b c d e)'; -is_deeply $type->arrayrefbase, [qw(a b c)], +is_deeply $type->arrayrefbase, [qw(a b c d e)], => 'Assignment is correct'; ok $type->arrayrefbase('d,e,f') @@ -125,4 +132,97 @@ ok $type->StrOrArrayRef([1,2,3]) throws_ok sub { $type->StrOrArrayRef({a=>111}); -}, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref'; \ No newline at end of file +}, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref'; + +# Test AtLeastOneInt + +ok $type->AtLeastOneInt([1,2]), + => 'Good assignment'; + +is_deeply $type->AtLeastOneInt, [1,2] + => "Got expected values."; + +throws_ok sub { + $type->AtLeastOneInt([]); +}, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails to assign as []'; + +throws_ok sub { + $type->AtLeastOneInt(['a','b']); +}, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings'; + +## Test pipeoverloading + +ok $type->pipeoverloading(1) + => 'Integer for union test accepted'; + +ok $type->pipeoverloading('a') + => 'String for union test accepted'; + +throws_ok sub { + $type->pipeoverloading({a=>1,b=>2}); +}, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef'; + +## test deep (ArrayRef[ArrayRef[HashRef[Int]]]) + +ok $type->deep([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]) + => 'Assigned deep to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]'; + +is_deeply $type->deep, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]], + => 'Assignment is correct'; + +throws_ok sub { + $type->deep({a=>1,b=>2}); +}, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail'; + +# test deep2 (ArrayRef[Int|ArrayRef[HashRef[Int|Object]]]) + +ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]) + => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]'; + +is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]], + => 'Assignment is correct'; + +throws_ok sub { + $type->deep2({a=>1,b=>2}); +}, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail'; + +throws_ok sub { + $type->deep2([[{a=>1,b=>2},{c=>3,d=>'noway'}],[{e=>5}]]); +}, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail'; + + +ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]]) + => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]]'; + + +is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]], + => 'Assignment is correct'; + +ok $type->deep2([1,2,3]) + => 'Assigned deep2 to [1,2,3]'; + + +is_deeply $type->deep2, [1,2,3], + => 'Assignment is correct'; + +## Test jobs + +ok $type->enum('Programming') + => 'Good Assignment of Programming to Enum'; + + +throws_ok sub { + $type->enum('ddddd'); +}, qr/Attribute \(enum\) does not pass the type constraint/ => 'Enum properly fails'; + +## Test SubOfMyArrayRefInt01_attr + +ok $type->SubOfMyArrayRefInt01_attr([15,20,25]) + => 'Assigned SubOfMyArrayRefInt01_attr to [15,20,25]'; + +is_deeply $type->SubOfMyArrayRefInt01_attr, [15,20,25], + => 'Assignment is correct'; + +throws_ok sub { + $type->SubOfMyArrayRefInt01_attr([15,5,20]); +}, qr/Attribute \(SubOfMyArrayRefInt01_attr\) does not pass the type constraint/ => 'SubOfMyArrayRefInt01 Constraints properly fail'; \ No newline at end of file