6 use Test::More tests => 31;
9 use Moose::Util::TypeConstraints;
11 my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]');
12 isa_ok($type, 'Moose::Meta::TypeConstraint');
13 isa_ok($type, 'Moose::Meta::TypeConstraint::Parameterized');
15 ok( $type->equals($type), "equals self" );
16 ok( !$type->equals($type->parent), "not equal to parent" );
17 ok( !$type->equals(find_type_constraint("Maybe")), "not equal to Maybe" );
18 ok( $type->parent->equals(find_type_constraint("Maybe")), "parent is Maybe" );
19 ok( $type->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "__ANON__", parent => find_type_constraint("Maybe"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" );
20 ok( !$type->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "__ANON__", parent => find_type_constraint("Maybe"), type_parameter => find_type_constraint("Str") ) ), "not equal to clone with diff param" );
21 ok( !$type->equals( Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Str]') ), "not equal to declarative version of diff param" );
23 ok($type->check(10), '... checked type correctly (pass)');
24 ok($type->check(undef), '... checked type correctly (pass)');
25 ok(!$type->check('Hello World'), '... checked type correctly (fail)');
26 ok(!$type->check([]), '... checked type correctly (fail)');
32 has 'bar' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);
40 Foo->new(bar => undef);
45 } '... failed the type check';
48 Foo->new(bar => 'hello world');
49 } '... failed the type check';
53 package Test::MooseX::Types::Maybe;
56 has 'Maybe_Int' => (is=>'rw', isa=>'Maybe[Int]');
57 has 'Maybe_ArrayRef' => (is=>'rw', isa=>'Maybe[ArrayRef]');
58 has 'Maybe_HashRef' => (is=>'rw', isa=>'Maybe[HashRef]');
59 has 'Maybe_ArrayRefInt' => (is=>'rw', isa=>'Maybe[ArrayRef[Int]]');
60 has 'Maybe_HashRefInt' => (is=>'rw', isa=>'Maybe[HashRef[Int]]');
63 ok my $obj = Test::MooseX::Types::Maybe->new
64 => 'Create good test object';
68 ok my $Maybe_Int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]')
69 => 'made TC Maybe[Int]';
71 ok $Maybe_Int->check(1)
77 ok $Maybe_Int->check()
83 ok $Maybe_Int->check(0)
86 ok defined $obj->Maybe_Int(0)
89 ok $Maybe_Int->check(undef)
92 ok sub {$obj->Maybe_Int(undef); 1}->()
93 => 'assigned (undef)';
95 ok !$Maybe_Int->check("")
98 throws_ok sub { $obj->Maybe_Int("") },
99 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
100 => 'failed assigned ("")';
102 ok !$Maybe_Int->check("a")
105 throws_ok sub { $obj->Maybe_Int("a") },
106 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
107 => 'failed assigned ("a")';