Commit | Line | Data |
fbb43338 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More tests=>15; |
5 | use Moose::Util::TypeConstraints; |
6 | use Moose::Meta::TypeConstraint::Parameterizable; |
7 | use Moose; |
8 | |
9 | |
10 | ok my $Optional = Moose::Meta::TypeConstraint::Parameterizable->new( |
11 | name => 'Optional', |
12 | package_defined_in => __PACKAGE__, |
13 | parent => find_type_constraint('Item'), |
14 | constraint => sub { 1 }, |
15 | constraint_generator => sub { |
16 | my $type_parameter = shift; |
17 | my $check = $type_parameter->_compiled_type_constraint; |
18 | return sub { |
19 | my (@args) = @_; |
20 | if(exists($args[0])) { |
21 | ## If it exists, we need to validate it |
22 | $check->($args[0]); |
23 | } else { |
24 | ## But it's is okay if the value doesn't exists |
25 | return 1; |
26 | } |
27 | } |
28 | } |
29 | ); |
30 | |
31 | Moose::Util::TypeConstraints::add_parameterizable_type($Optional); |
32 | use Data::Dump qw/dump/; |
33 | |
34 | foreach my $type (Moose::Util::TypeConstraints::get_all_parameterizable_types()) { |
35 | if (my $constraint = $type->generate_constraint_for($Optional)) { |
36 | warn 'got this FARRRRRR..........................................'; |
37 | } |
38 | } |
39 | |
40 | |
41 | |
42 | isa_ok $Optional, 'Moose::Meta::TypeConstraint::Parameterizable'; |
43 | |
44 | ok my $int = find_type_constraint('Int') => 'Got Int'; |
45 | ok my $arrayref = find_type_constraint('ArrayRef[Int]') => 'Got ArrayRef[Int]'; |
46 | |
47 | ok my $Optional_Int = $Optional->parameterize($int); |
48 | ok my $Optional_ArrayRef = $Optional->parameterize($arrayref); |
49 | |
50 | |
51 | ok $Optional_Int->check() => 'Optional is allowed to not exist'; |
52 | ok !$Optional_Int->check(undef) => 'Optional is NOT allowed to be undef'; |
53 | ok $Optional_Int->check(199) => 'Correctly validates 199'; |
54 | ok !$Optional_Int->check("a") => 'Correctly fails "a"'; |
55 | |
56 | ok $Optional_ArrayRef->check() => 'Optional is allowed to not exist'; |
57 | ok !$Optional_ArrayRef->check(undef) => 'Optional is NOT allowed to be undef'; |
58 | ok $Optional_ArrayRef->check([1,2,3]) => 'Correctly validates [1,2,3]'; |
59 | ok !$Optional_ArrayRef->check("a") => 'Correctly fails "a"'; |
60 | ok !$Optional_ArrayRef->check(["a","b"]) => 'Correctly fails ["a","b"]'; |