moving stuff around a bit
[gitmo/Moose.git] / t / 040_type_constraints / 021_maybe_type_constraint.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 12;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');
11     use_ok('Moose::Util::TypeConstraints');
12 }
13
14 my $type = Moose::Util::TypeConstraints::find_or_create_type_constraint('Maybe[Int]');
15 isa_ok($type, 'Moose::Meta::TypeConstraint');
16 isa_ok($type, 'Moose::Meta::TypeConstraint::Parameterized');
17
18 ok($type->check(10), '... checked type correctly (pass)');
19 ok($type->check(undef), '... checked type correctly (pass)');
20 ok(!$type->check('Hello World'), '... checked type correctly (fail)');
21 ok(!$type->check([]), '... checked type correctly (fail)');
22
23 {
24     package Foo;
25     use Moose;
26     
27     has 'bar' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);    
28 }
29
30 lives_ok {
31     Foo->new(bar => []);
32 } '... it worked!';
33
34 lives_ok {
35     Foo->new(bar => undef);
36 } '... it worked!';
37
38 dies_ok {
39     Foo->new(bar => 100);
40 } '... failed the type check';
41
42 dies_ok {
43     Foo->new(bar => 'hello world');
44 } '... failed the type check';
45