We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / type_constraints / maybe_type_constraint.t
CommitLineData
7e4e1ad4 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
7e4e1ad4 8
7ff56534 9use Moose::Util::TypeConstraints;
7e4e1ad4 10
620db045 11my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]');
7e4e1ad4 12isa_ok($type, 'Moose::Meta::TypeConstraint');
13isa_ok($type, 'Moose::Meta::TypeConstraint::Parameterized');
14
dabed765 15ok( $type->equals($type), "equals self" );
16ok( !$type->equals($type->parent), "not equal to parent" );
17ok( !$type->equals(find_type_constraint("Maybe")), "not equal to Maybe" );
18ok( $type->parent->equals(find_type_constraint("Maybe")), "parent is Maybe" );
19ok( $type->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "__ANON__", parent => find_type_constraint("Maybe"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" );
20ok( !$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" );
620db045 21ok( !$type->equals( Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Str]') ), "not equal to declarative version of diff param" );
dabed765 22
7e4e1ad4 23ok($type->check(10), '... checked type correctly (pass)');
24ok($type->check(undef), '... checked type correctly (pass)');
25ok(!$type->check('Hello World'), '... checked type correctly (fail)');
26ok(!$type->check([]), '... checked type correctly (fail)');
27
28{
1b2c9bda 29 package Bar;
30 use Moose;
31
7e4e1ad4 32 package Foo;
33 use Moose;
1b2c9bda 34 use Moose::Util::TypeConstraints;
d03bd989 35
36 has 'arr' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);
1b2c9bda 37 has 'bar' => (is => 'rw', isa => class_type('Bar'));
38 has 'maybe_bar' => (is => 'rw', isa => maybe_type(class_type('Bar')));
7e4e1ad4 39}
40
b10dde3a 41is( exception {
1b2c9bda 42 Foo->new(arr => [], bar => Bar->new);
b10dde3a 43}, undef, '... Bar->new isa Bar' );
1b2c9bda 44
b10dde3a 45isnt( exception {
1b2c9bda 46 Foo->new(arr => [], bar => undef);
b10dde3a 47}, undef, '... undef isnta Bar' );
1b2c9bda 48
b10dde3a 49is( exception {
1b2c9bda 50 Foo->new(arr => [], maybe_bar => Bar->new);
b10dde3a 51}, undef, '... Bar->new isa maybe(Bar)' );
1b2c9bda 52
b10dde3a 53is( exception {
1b2c9bda 54 Foo->new(arr => [], maybe_bar => undef);
b10dde3a 55}, undef, '... undef isa maybe(Bar)' );
1b2c9bda 56
b10dde3a 57isnt( exception {
1b2c9bda 58 Foo->new(arr => [], maybe_bar => 1);
b10dde3a 59}, undef, '... 1 isnta maybe(Bar)' );
1b2c9bda 60
b10dde3a 61is( exception {
1b2c9bda 62 Foo->new(arr => []);
b10dde3a 63}, undef, '... it worked!' );
7e4e1ad4 64
b10dde3a 65is( exception {
1b2c9bda 66 Foo->new(arr => undef);
b10dde3a 67}, undef, '... it worked!' );
7e4e1ad4 68
b10dde3a 69isnt( exception {
1b2c9bda 70 Foo->new(arr => 100);
b10dde3a 71}, undef, '... failed the type check' );
7e4e1ad4 72
b10dde3a 73isnt( exception {
1b2c9bda 74 Foo->new(arr => 'hello world');
b10dde3a 75}, undef, '... failed the type check' );
7e4e1ad4 76
f805496a 77
78{
79 package Test::MooseX::Types::Maybe;
80 use Moose;
81
82 has 'Maybe_Int' => (is=>'rw', isa=>'Maybe[Int]');
d03bd989 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]]');
f805496a 87}
88
89ok my $obj = Test::MooseX::Types::Maybe->new
90 => 'Create good test object';
91
92## Maybe[Int]
93
94ok my $Maybe_Int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]')
95 => 'made TC Maybe[Int]';
d03bd989 96
f805496a 97ok $Maybe_Int->check(1)
98 => 'passed (1)';
d03bd989 99
f805496a 100ok $obj->Maybe_Int(1)
101 => 'assigned (1)';
d03bd989 102
f805496a 103ok $Maybe_Int->check()
104 => 'passed ()';
105
106ok $obj->Maybe_Int()
107 => 'assigned ()';
108
109ok $Maybe_Int->check(0)
110 => 'passed (0)';
111
112ok defined $obj->Maybe_Int(0)
113 => 'assigned (0)';
d03bd989 114
f805496a 115ok $Maybe_Int->check(undef)
116 => 'passed (undef)';
d03bd989 117
f805496a 118ok sub {$obj->Maybe_Int(undef); 1}->()
119 => 'assigned (undef)';
d03bd989 120
f805496a 121ok !$Maybe_Int->check("")
122 => 'failed ("")';
d03bd989 123
b10dde3a 124like( exception { $obj->Maybe_Int("") }, qr/Attribute \(Maybe_Int\) does not pass the type constraint/, 'failed assigned ("")' );
f805496a 125
126ok !$Maybe_Int->check("a")
127 => 'failed ("a")';
128
b10dde3a 129like( exception { $obj->Maybe_Int("a") }, qr/Attribute \(Maybe_Int\) does not pass the type constraint/, 'failed assigned ("a")' );
a28e50e4 130
131done_testing;