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 }, undef, '... Bar->new isa Bar' );
46 Foo->new(arr => [], bar => undef);
47 }, undef, '... undef isnta Bar' );
50 Foo->new(arr => [], maybe_bar => Bar->new);
51 }, undef, '... Bar->new isa maybe(Bar)' );
54 Foo->new(arr => [], maybe_bar => undef);
55 }, undef, '... undef isa maybe(Bar)' );
58 Foo->new(arr => [], maybe_bar => 1);
59 }, undef, '... 1 isnta maybe(Bar)' );
63 }, undef, '... it worked!' );
66 Foo->new(arr => undef);
67 }, undef, '... it worked!' );
71 }, undef, '... failed the type check' );
74 Foo->new(arr => 'hello world');
75 }, undef, '... 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("") }, qr/Attribute \(Maybe_Int\) does not pass the type constraint/, 'failed assigned ("")' );
126 ok !$Maybe_Int->check("a")
129 like( exception { $obj->Maybe_Int("a") }, qr/Attribute \(Maybe_Int\) does not pass the type constraint/, 'failed assigned ("a")' );