remove trailing whitespace
[gitmo/Moose.git] / t / 040_type_constraints / 021_maybe_type_constraint.t
CommitLineData
7e4e1ad4 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
1b2c9bda 6use Test::More tests => 36;
7e4e1ad4 7use Test::Exception;
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
41lives_ok {
1b2c9bda 42 Foo->new(arr => [], bar => Bar->new);
43} '... Bar->new isa Bar';
44
45dies_ok {
46 Foo->new(arr => [], bar => undef);
47} '... undef isnta Bar';
48
49lives_ok {
50 Foo->new(arr => [], maybe_bar => Bar->new);
51} '... Bar->new isa maybe(Bar)';
52
53lives_ok {
54 Foo->new(arr => [], maybe_bar => undef);
55} '... undef isa maybe(Bar)';
56
57dies_ok {
58 Foo->new(arr => [], maybe_bar => 1);
59} '... 1 isnta maybe(Bar)';
60
61lives_ok {
62 Foo->new(arr => []);
7e4e1ad4 63} '... it worked!';
64
65lives_ok {
1b2c9bda 66 Foo->new(arr => undef);
7e4e1ad4 67} '... it worked!';
68
69dies_ok {
1b2c9bda 70 Foo->new(arr => 100);
7e4e1ad4 71} '... failed the type check';
72
73dies_ok {
1b2c9bda 74 Foo->new(arr => 'hello world');
7e4e1ad4 75} '... failed the type check';
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
124throws_ok sub { $obj->Maybe_Int("") },
f805496a 125 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
126 => 'failed assigned ("")';
127
128ok !$Maybe_Int->check("a")
129 => 'failed ("a")';
130
d03bd989 131throws_ok sub { $obj->Maybe_Int("a") },
f805496a 132 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
1b2c9bda 133 => 'failed assigned ("a")';