no windows linefeeds
[gitmo/MooseX-Types-Structured.git] / t / 01-basic.t
CommitLineData
a30fa891 1use strict;
2use warnings;
3
4use Test::More tests=>14;
5
6use_ok 'MooseX::Meta::TypeConstraint::Structured';
7use_ok 'Moose::Util::TypeConstraints';
8
9ok my $int = find_type_constraint('Int') => 'Got Int';
10ok my $str = find_type_constraint('Str') => 'Got Str';
11ok my $arrayref = find_type_constraint('ArrayRef') => 'Got ArrayRef';
12
13my $list_tc = MooseX::Meta::TypeConstraint::Structured->new(
14 name => 'list_tc',
15 parent => $arrayref,
16 type_constraints => [$int, $str],
17 constraint_generator=> sub {
c5d806b4 18 my @type_constraints = @{shift @_};
a30fa891 19 my @values = @{shift @_};
20
21 while(my $type_constraint = shift @type_constraints) {
22 my $value = shift @values || return;
23 $type_constraint->check($value) || return;
24 }
25 if(@values) {
26 return;
27 } else {
28 return 1;
29 }
30 }
31);
32
33isa_ok $list_tc, 'MooseX::Meta::TypeConstraint::Structured';
34
35ok !$arrayref->check() => 'Parent undef fails';
36ok !$list_tc->check() => 'undef fails';
37ok !$list_tc->check(1) => '1 fails';
38ok !$list_tc->check([]) => '[] fails';
39ok !$list_tc->check([1]) => '[1] fails';
40ok !$list_tc->check([1,2,3]) => '[1,2,3] fails';
41ok !$list_tc->check(['a','b']) => '["a","b"] fails';
42
43ok $list_tc->check([1,'a']) => '[1,"a"] passes';