merge trunk to pluggable errors
[gitmo/Moose.git] / t / 040_type_constraints / 021_maybe_type_constraint.t
CommitLineData
7e4e1ad4 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
e606ae5f 6use Test::More tests => 17;
7e4e1ad4 7use Test::Exception;
8
e606ae5f 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{
29 package Foo;
30 use Moose;
31
32 has 'bar' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);
33}
34
35lives_ok {
36 Foo->new(bar => []);
37} '... it worked!';
38
39lives_ok {
40 Foo->new(bar => undef);
41} '... it worked!';
42
43dies_ok {
44 Foo->new(bar => 100);
45} '... failed the type check';
46
47dies_ok {
48 Foo->new(bar => 'hello world');
49} '... failed the type check';
50