refactoring the parameterized type constraints
[gitmo/Moose.git] / t / 040_type_constraints / 021_maybe_type_constraint.t
CommitLineData
7e4e1ad4 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 12;
7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose');
11 use_ok('Moose::Util::TypeConstraints');
12}
13
14my $type = Moose::Util::TypeConstraints::find_or_create_type_constraint('Maybe[Int]');
15isa_ok($type, 'Moose::Meta::TypeConstraint');
16isa_ok($type, 'Moose::Meta::TypeConstraint::Parameterized');
17
18ok($type->check(10), '... checked type correctly (pass)');
19ok($type->check(undef), '... checked type correctly (pass)');
20ok(!$type->check('Hello World'), '... checked type correctly (fail)');
21ok(!$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
30lives_ok {
31 Foo->new(bar => []);
32} '... it worked!';
33
34lives_ok {
35 Foo->new(bar => undef);
36} '... it worked!';
37
38dies_ok {
39 Foo->new(bar => 100);
40} '... failed the type check';
41
42dies_ok {
43 Foo->new(bar => 'hello world');
44} '... failed the type check';
45