4 use Test::More tests=>26;
5 use Moose::Util::TypeConstraints;
6 use MooseX::Types::Structured qw(Optional);
9 ok my $Optional = Moose::Util::TypeConstraints::find_or_parse_type_constraint('MooseX::Types::Structured::Optional')
13 => 'Moose::Meta::TypeConstraint::Parameterizable';
15 ok my $int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Int')
18 ok my $arrayref = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Int]')
19 => 'Got ArrayRef[Int]';
22 ok my $Optional_Int = $Optional->parameterize($int), 'Parameterized Int';
23 ok my $Optional_ArrayRef = $Optional->parameterize($arrayref), 'Parameterized ArrayRef';
25 ok $Optional_Int->check() => 'Optional is allowed to not exist';
27 ok !$Optional_Int->check(undef) => 'Optional is NOT allowed to be undef';
28 ok $Optional_Int->check(199) => 'Correctly validates 199';
29 ok !$Optional_Int->check("a") => 'Correctly fails "a"';
31 ok $Optional_ArrayRef->check() => 'Optional is allowed to not exist';
32 ok !$Optional_ArrayRef->check(undef) => 'Optional is NOT allowed to be undef';
33 ok $Optional_ArrayRef->check([1,2,3]) => 'Correctly validates [1,2,3]';
34 ok !$Optional_ArrayRef->check("a") => 'Correctly fails "a"';
35 ok !$Optional_ArrayRef->check(["a","b"]) => 'Correctly fails ["a","b"]';
39 ok my $Optional_Int = Optional->parameterize($int),'Parameterized Int';
40 ok my $Optional_ArrayRef = Optional->parameterize($arrayref), 'Parameterized ArrayRef';
42 ok $Optional_Int->check() => 'Optional is allowed to not exist';
44 ok !$Optional_Int->check(undef) => 'Optional is NOT allowed to be undef';
45 ok $Optional_Int->check(199) => 'Correctly validates 199';
46 ok !$Optional_Int->check("a") => 'Correctly fails "a"';
48 ok $Optional_ArrayRef->check() => 'Optional is allowed to not exist';
49 ok !$Optional_ArrayRef->check(undef) => 'Optional is NOT allowed to be undef';
50 ok $Optional_ArrayRef->check([1,2,3]) => 'Correctly validates [1,2,3]';
51 ok !$Optional_ArrayRef->check("a") => 'Correctly fails "a"';
52 ok !$Optional_ArrayRef->check(["a","b"]) => 'Correctly fails ["a","b"]';
55 ## Test via the subref Optional()