X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F01-basic.t;h=071f231f7c5d01e62db5a650b8383c062f9a479d;hb=b86402a09c4f8f81b5f685e340c96d2361c742de;hp=90e31ff55260b5dc51fc73712eef813a9f1df6f5;hpb=bfef1b305fffdc098e6732fe88c1e6b4e3e006ee;p=gitmo%2FMooseX-Types-Structured.git diff --git a/t/01-basic.t b/t/01-basic.t old mode 100755 new mode 100644 index 90e31ff..071f231 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -1,47 +1,43 @@ -BEGIN { - use strict; - use warnings; - use Test::More tests=>4; - use Test::Exception; - - use_ok 'Moose::Util::TypeConstraints'; - use_ok 'MooseX::Meta::TypeConstraint::Structured::Positionable'; -} - -ok my $REGISTRY = Moose::Meta::TypeConstraint::Registry->new - => 'Got a registry'; - -my $tuple = MooseX::Meta::TypeConstraint::Structured::Positionable->new( - name => 'Tuple', - package_defined_in => __PACKAGE__, - parent => find_type_constraint('Ref'), - ); - - -type('Tuple', $tuple); - - - - -use Data::Dump qw/dump/; -#warn dump sort {$a cmp $b} Moose::Util::TypeConstraints::list_all_type_constraints; - - -{ - package Test::MooseX::Types::Structured::Positionable; - use Moose; - - has 'attr' => (is=>'rw', isa=>'Tuple[Int,Str,Int]'); - -} - -ok my $positioned_obj = Test::MooseX::Types::Structured::Positionable->new, - => 'Got a good object'; - -## should be good -$positioned_obj->attr([1,'hello',3]); - -## should all fail -$positioned_obj->attr([1,2,'world']); -$positioned_obj->attr(['hello',2,3]); -$positioned_obj->attr(['hello',2,'world']); \ No newline at end of file +use strict; +use warnings; + +use Test::More tests=>14; + +use_ok 'MooseX::Meta::TypeConstraint::Structured'; +use_ok 'Moose::Util::TypeConstraints'; + +ok my $int = find_type_constraint('Int') => 'Got Int'; +ok my $str = find_type_constraint('Str') => 'Got Str'; +ok my $arrayref = find_type_constraint('ArrayRef') => 'Got ArrayRef'; + +my $list_tc = MooseX::Meta::TypeConstraint::Structured->new( + name => 'list_tc', + parent => $arrayref, + type_constraints => [$int, $str], + constraint_generator=> sub { + my @type_constraints = @{shift @_}; + my @values = @{shift @_}; + + while(my $type_constraint = shift @type_constraints) { + my $value = shift @values || return; + $type_constraint->check($value) || return; + } + if(@values) { + return; + } else { + return 1; + } + } +); + +isa_ok $list_tc, 'MooseX::Meta::TypeConstraint::Structured'; + +ok !$arrayref->check() => 'Parent undef fails'; +ok !$list_tc->check() => 'undef fails'; +ok !$list_tc->check(1) => '1 fails'; +ok !$list_tc->check([]) => '[] fails'; +ok !$list_tc->check([1]) => '[1] fails'; +ok !$list_tc->check([1,2,3]) => '[1,2,3] fails'; +ok !$list_tc->check(['a','b']) => '["a","b"] fails'; + +ok $list_tc->check([1,'a']) => '[1,"a"] passes';