X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F02-constraints.t;h=86c4e6ce682b699762f67fabb086ce27815e8821;hb=78f559467710da345f5d08c2fea40da4d75ed8ee;hp=ba8cf30227430f928eed46d64f07e316990dd77a;hpb=d06d64c329d25f3dcba58bf98e677a60e37be60d;p=gitmo%2FMooseX-Types-Structured.git diff --git a/t/02-constraints.t b/t/02-constraints.t index ba8cf30..86c4e6c 100644 --- a/t/02-constraints.t +++ b/t/02-constraints.t @@ -1,7 +1,7 @@ BEGIN { use strict; use warnings; - use Test::More tests=>42; + use Test::More tests=>47; use Test::Exception; } @@ -9,45 +9,42 @@ BEGIN { package Test::MooseX::Meta::TypeConstraint::Structured; use Moose; - use MooseX::Types::Structured qw(Tuple Dict); + use MooseX::Types::Structured qw(Tuple Dict Optional); + use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); + use MooseX::Types -declare => [qw(MyString)]; use Moose::Util::TypeConstraints; - subtype 'MyString', + subtype MyString, as 'Str', where { $_=~m/abc/}; - has 'tuple' => (is=>'rw', isa=>Tuple['Int', 'Str', 'MyString']); - has 'dict' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Int']); - has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Maybe[Int]']); - has 'tuple_with_param' => (is=>'rw', isa=>Tuple['Int', 'Str', 'ArrayRef[Int]']); - has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple['Int', 'Str', 'Maybe[Int]']); - has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>'Str', key2=>Tuple['Int','Str']]); - has 'optional_tuple' => (is=>'rw', isa=>Tuple['Int', 'Int'],['Int'] ); - has 'optional_dict' => (is=>'rw', isa=>Dict[key1=>'Int'],[key2=>'Int'] ); - + has 'tuple' => (is=>'rw', isa=>Tuple[Int, Str, MyString]); + has 'dict' => (is=>'rw', isa=>Dict[name=>Str, age=>Int]); + has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>Str, age=>Maybe[Int]]); + has 'tuple_with_param' => (is=>'rw', isa=>Tuple[Int, Str, ArrayRef[Int]]); + has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int]]); + has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>Str, key2=>Tuple[Int,Str]]); + has 'optional_tuple' => (is=>'rw', isa=>Tuple[Int, Int, Optional[Int]] ); + has 'optional_dict' => (is=>'rw', isa=>Dict[key1=>Int, Optional[key2=>Int]] ); + has 'dict_with_tuple_with_union' => (is=>'rw', isa=>Dict[key1=>Str|Object, key2=>Tuple[Int,Str|Object]] ); + has 'crazy' => ( is=>'rw', isa=>Tuple ## First ArrayRef Arg is the required type constraints for the top ## level Tuple. [ - 'Int', - 'MyString', + Int, + MyString, ## The third required element is a Dict type constraint, which ## itself has two required keys and a third optional key. - Dict - [name=>'Str',age=>'Int'], - [visits=>'Int'] - ], - ## Second ArrayRef Arg defines the optional constraints for the top - ## level Tuple. - [ - 'Int', - ## This Tuple has one required type constraint and two optional. - Tuple - ['Int'], - ['Int','HashRef'], - ], + Dict[name=>Str,age=>Int, Optional[visits=>Int]], + Optional[ + Int, + ## This Tuple has one required type constraint and two optional. + Tuple[Int, Optional[Int,HashRef]], + ], + ], ); } @@ -235,4 +232,28 @@ throws_ok sub { $record->optional_dict({key1=>1,key2=>'bad'}); }, qr/Validation failed for 'Int'/ => 'Properly failed for bad value in optional bit'; - \ No newline at end of file + + +## Test dict_with_tuple_with_union: Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']] + +lives_ok sub { + $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,'World']}); +} => 'Set tuple attribute without error'; + +throws_ok sub { + $record->dict_with_tuple_with_union({key1=>'Hello', key2=>['World',2]}); +}, qr/Validation failed for 'Int'/ + => 'Threw error on bad constraint'; + +lives_ok sub { + $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']}); +} => 'Set tuple attribute without error'; + +lives_ok sub { + $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,$record]}); +} => 'Set tuple attribute without error'; + +throws_ok sub { + $record->dict_with_tuple_with_union({key1=>1, key2=>['World',2]}); +}, qr/Validation failed for 'Int'/ + => 'Threw error on bad constraint';