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)');
34 use Moose::Util::TypeConstraints;
36 has 'arr' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);
37 has 'bar' => (is => 'rw', isa => class_type('Bar'));
38 has 'maybe_bar' => (is => 'rw', isa => maybe_type(class_type('Bar')));
42 Foo->new(arr => [], bar => Bar->new);
43 }, '... Bar->new isa Bar';
46 Foo->new(arr => [], bar => undef);
47 }, '... undef isnta Bar';
50 Foo->new(arr => [], maybe_bar => Bar->new);
51 }, '... Bar->new isa maybe(Bar)';
54 Foo->new(arr => [], maybe_bar => undef);
55 }, '... undef isa maybe(Bar)';
58 Foo->new(arr => [], maybe_bar => 1);
59 }, '... 1 isnta maybe(Bar)';
66 Foo->new(arr => undef);
71 }, '... failed the type check';
74 Foo->new(arr => 'hello world');
75 }, '... failed the type check';
79 package Test::MooseX::Types::Maybe;
82 has 'Maybe_Int' => (is=>'rw', isa=>'Maybe[Int]');
83 has 'Maybe_ArrayRef' => (is=>'rw', isa=>'Maybe[ArrayRef]');
84 has 'Maybe_HashRef' => (is=>'rw', isa=>'Maybe[HashRef]');
85 has 'Maybe_ArrayRefInt' => (is=>'rw', isa=>'Maybe[ArrayRef[Int]]');
86 has 'Maybe_HashRefInt' => (is=>'rw', isa=>'Maybe[HashRef[Int]]');
89 ok my $obj = Test::MooseX::Types::Maybe->new
90 => 'Create good test object';
94 ok my $Maybe_Int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]')
95 => 'made TC Maybe[Int]';
97 ok $Maybe_Int->check(1)
100 ok $obj->Maybe_Int(1)
103 ok $Maybe_Int->check()
109 ok $Maybe_Int->check(0)
112 ok defined $obj->Maybe_Int(0)
115 ok $Maybe_Int->check(undef)
118 ok sub {$obj->Maybe_Int(undef); 1}->()
119 => 'assigned (undef)';
121 ok !$Maybe_Int->check("")
124 like exception { $obj->Maybe_Int("") },,
125 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
126 => 'failed assigned ("")';
128 ok !$Maybe_Int->check("a")
131 like exception { $obj->Maybe_Int("a") },,
132 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
133 => 'failed assigned ("a")';