actually create the optional TC, and a bunch fo changes to make sure the api test...
[gitmo/MooseX-Types-Structured.git] / t / 09-optional.t
CommitLineData
fbb43338 1use strict;
2use warnings;
3
e327145a 4use Test::More tests=>26;
fbb43338 5use Moose::Util::TypeConstraints;
e327145a 6use MooseX::Types::Structured qw(Optional);
7
8## Setup Stuff
9ok my $Optional = Moose::Util::TypeConstraints::find_or_parse_type_constraint('MooseX::Types::Structured::Optional')
10 => 'Got Optional';
11
12isa_ok $Optional
13 => 'Moose::Meta::TypeConstraint::Parameterizable';
fbb43338 14
1e60caec 15ok my $int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Int')
16 => 'Got Int';
fbb43338 17
1e60caec 18ok my $arrayref = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Int]')
19 => 'Got ArrayRef[Int]';
fbb43338 20
e327145a 21BASIC: {
22 ok my $Optional_Int = $Optional->parameterize($int), 'Parameterized Int';
23 ok my $Optional_ArrayRef = $Optional->parameterize($arrayref), 'Parameterized ArrayRef';
24
25 ok $Optional_Int->check() => 'Optional is allowed to not exist';
26
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"';
30
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"]';
36}
37
38SUBREF: {
39 ok my $Optional_Int = Optional->parameterize($int),'Parameterized Int';
40 ok my $Optional_ArrayRef = Optional->parameterize($arrayref), 'Parameterized ArrayRef';
41
42 ok $Optional_Int->check() => 'Optional is allowed to not exist';
43
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"';
47
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"]';
53}
54
55## Test via the subref Optional()
fbb43338 56