X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F13_typedecorator.t;h=1bf93298efe66d606bd5b7898658345f70efe498;hb=a344ca961b0a3507cfb5a4fc142c3d89c0bea1c7;hp=da2c5cdb822dcf38de77e62757cc03cb6d8b7f80;hpb=377378b80ae2487e28b7ce1ce72736f215c5ebb9;p=gitmo%2FMooseX-Types.git diff --git a/t/13_typedecorator.t b/t/13_typedecorator.t index da2c5cd..1bf9329 100644 --- a/t/13_typedecorator.t +++ b/t/13_typedecorator.t @@ -2,8 +2,8 @@ use warnings; use strict; -use Test::More tests => 39; -use Test::Exception; +use Test::More; +use Test::Fatal; use FindBin; use lib "$FindBin::Bin/lib"; @@ -12,21 +12,27 @@ use lib "$FindBin::Bin/lib"; use Moose; use MooseX::Types::Moose qw( - Int Str ArrayRef HashRef + Int Str ArrayRef HashRef Object ); use DecoratorLibrary qw( MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef - AtLeastOneInt + AtLeastOneInt Jobs SubOfMyArrayRefInt01 WierdIntergersArrayRef1 + WierdIntergersArrayRef2 ); has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1); has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1); 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 'StrOrArrayRef_attr' => (is=>'rw', isa=>StrOrArrayRef); + has 'AtLeastOneInt_attr' => (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); + has 'WierdIntergersArrayRef1_attr' => (is=>'rw', isa=>WierdIntergersArrayRef1); + has 'WierdIntergersArrayRef2_attr' => (is=>'rw', isa=>WierdIntergersArrayRef2); } ## Make sure we have a 'create object sanity check' @@ -39,10 +45,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') @@ -71,7 +77,7 @@ ok $type->arrayrefint01({a=>7,b=>8}) is_deeply $type->arrayrefint01, [qw(7 8)], => 'Assignment and coercion is correct'; -throws_ok sub { +like exception { $type->arrayrefint01([qw(a b c)]) }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings'; @@ -115,37 +121,37 @@ ok $type->arrayrefint03([qw(11 12 13)]) is_deeply $type->arrayrefint03, [qw(11 12 13)], => 'Assignment is correct'; -throws_ok sub { +like exception { $type->arrayrefint03([qw(a b c)]) }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings'; # TEST StrOrArrayRef -ok $type->StrOrArrayRef('string') +ok $type->StrOrArrayRef_attr('string') => 'String part of union is good'; -ok $type->StrOrArrayRef([1,2,3]) +ok $type->StrOrArrayRef_attr([1,2,3]) => 'arrayref part of union is good'; -throws_ok sub { - $type->StrOrArrayRef({a=>111}); -}, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref'; +like exception { + $type->StrOrArrayRef_attr({a=>111}); +}, qr/Attribute \(StrOrArrayRef_attr\) does not pass the type constraint/ => 'Correctly failed to use a hashref'; # Test AtLeastOneInt -ok $type->AtLeastOneInt([1,2]), +ok $type->AtLeastOneInt_attr([1,2]), => 'Good assignment'; -is_deeply $type->AtLeastOneInt, [1,2] +is_deeply $type->AtLeastOneInt_attr, [1,2] => "Got expected values."; -throws_ok sub { - $type->AtLeastOneInt([]); -}, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails'; +like exception { + $type->AtLeastOneInt_attr([]); +}, qr/Attribute \(AtLeastOneInt_attr\) 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'; +like exception { + $type->AtLeastOneInt_attr(['a','b']); +}, qr/Attribute \(AtLeastOneInt_attr\) does not pass the type constraint/ => 'properly fails arrayref of strings'; ## Test pipeoverloading @@ -155,11 +161,11 @@ ok $type->pipeoverloading(1) ok $type->pipeoverloading('a') => 'String for union test accepted'; -throws_ok sub { +like exception { $type->pipeoverloading({a=>1,b=>2}); -}, qr/Validation failed for 'Int | Str'/ => 'Union test corrected fails a HashRef'; +}, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef'; -## test deep +## 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}]]'; @@ -167,6 +173,108 @@ ok $type->deep([[{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 { +like exception { $type->deep({a=>1,b=>2}); -}, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail'; \ No newline at end of file +}, 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'; + +like exception { + $type->deep2({a=>1,b=>2}); +}, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail'; + +like exception { + $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'; + + +like exception { + $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'; + +like exception { + $type->SubOfMyArrayRefInt01_attr([15,5,20]); +}, qr/Attribute \(SubOfMyArrayRefInt01_attr\) does not pass the type constraint/ + => 'SubOfMyArrayRefInt01 Constraints properly fail'; + +## test WierdIntergersArrayRef1 + +ok $type->WierdIntergersArrayRef1_attr([5,10,1000]) + => 'Assigned deep2 to [5,10,1000]'; + +is_deeply $type->WierdIntergersArrayRef1_attr, [5,10,1000], + => 'Assignment is correct'; + +like exception { + $type->WierdIntergersArrayRef1_attr({a=>1,b=>2}); +}, qr/Attribute \(WierdIntergersArrayRef1_attr\) does not pass the type constraint/ + => 'Constraints properly fail'; + +like exception { + $type->WierdIntergersArrayRef1_attr([5,10,1]); +}, qr/Attribute \(WierdIntergersArrayRef1_attr\) does not pass the type constraint/ + => 'Constraints properly fail'; + +like exception { + $type->WierdIntergersArrayRef1_attr([1]); +}, qr/Attribute \(WierdIntergersArrayRef1_attr\) does not pass the type constraint/ + => 'Constraints properly fail'; + +## test WierdIntergersArrayRef2 + +ok $type->WierdIntergersArrayRef2_attr([5,10,$type]) + => 'Assigned deep2 to [5,10,$type]'; + +is_deeply $type->WierdIntergersArrayRef2_attr, [5,10,$type], + => 'Assignment is correct'; + +like exception { + $type->WierdIntergersArrayRef2_attr({a=>1,b=>2}); +}, qr/Attribute \(WierdIntergersArrayRef2_attr\) does not pass the type constraint/ + => 'Constraints properly fail'; + +like exception { + $type->WierdIntergersArrayRef2_attr([5,10,1]); +}, qr/Attribute \(WierdIntergersArrayRef2_attr\) does not pass the type constraint/ + => 'Constraints properly fail'; + +like exception { + $type->WierdIntergersArrayRef2_attr([1]); +}, qr/Attribute \(WierdIntergersArrayRef2_attr\) does not pass the type constraint/ + => 'Constraints properly fail'; + +done_testing();