a start at outlining Optional
[gitmo/MooseX-Types-Structured.git] / t / 09-optional.t
CommitLineData
fbb43338 1use strict;
2use warnings;
3
4use Test::More tests=>15;
5use Moose::Util::TypeConstraints;
6use Moose::Meta::TypeConstraint::Parameterizable;
7use Moose;
8
9
10ok 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
31Moose::Util::TypeConstraints::add_parameterizable_type($Optional);
32use 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
42isa_ok $Optional, 'Moose::Meta::TypeConstraint::Parameterizable';
43
44ok my $int = find_type_constraint('Int') => 'Got Int';
45ok my $arrayref = find_type_constraint('ArrayRef[Int]') => 'Got ArrayRef[Int]';
46
47ok my $Optional_Int = $Optional->parameterize($int);
48ok my $Optional_ArrayRef = $Optional->parameterize($arrayref);
49
50
51ok $Optional_Int->check() => 'Optional is allowed to not exist';
52ok !$Optional_Int->check(undef) => 'Optional is NOT allowed to be undef';
53ok $Optional_Int->check(199) => 'Correctly validates 199';
54ok !$Optional_Int->check("a") => 'Correctly fails "a"';
55
56ok $Optional_ArrayRef->check() => 'Optional is allowed to not exist';
57ok !$Optional_ArrayRef->check(undef) => 'Optional is NOT allowed to be undef';
58ok $Optional_ArrayRef->check([1,2,3]) => 'Correctly validates [1,2,3]';
59ok !$Optional_ArrayRef->check("a") => 'Correctly fails "a"';
60ok !$Optional_ArrayRef->check(["a","b"]) => 'Correctly fails ["a","b"]';