fix and test equals for various TC classes, and introduce the Enum TC class
[gitmo/Moose.git] / t / 040_type_constraints / 021_maybe_type_constraint.t
CommitLineData
7e4e1ad4 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
dabed765 6use Test::More tests => 19;
7e4e1ad4 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
dabed765 18ok( $type->equals($type), "equals self" );
19ok( !$type->equals($type->parent), "not equal to parent" );
20ok( !$type->equals(find_type_constraint("Maybe")), "not equal to Maybe" );
21ok( $type->parent->equals(find_type_constraint("Maybe")), "parent is Maybe" );
22ok( $type->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "__ANON__", parent => find_type_constraint("Maybe"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" );
23ok( !$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" );
24ok( !$type->equals( Moose::Util::TypeConstraints::find_or_create_type_constraint('Maybe[Str]') ), "not equal to declarative version of diff param" );
25
7e4e1ad4 26ok($type->check(10), '... checked type correctly (pass)');
27ok($type->check(undef), '... checked type correctly (pass)');
28ok(!$type->check('Hello World'), '... checked type correctly (fail)');
29ok(!$type->check([]), '... checked type correctly (fail)');
30
31{
32 package Foo;
33 use Moose;
34
35 has 'bar' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);
36}
37
38lives_ok {
39 Foo->new(bar => []);
40} '... it worked!';
41
42lives_ok {
43 Foo->new(bar => undef);
44} '... it worked!';
45
46dies_ok {
47 Foo->new(bar => 100);
48} '... failed the type check';
49
50dies_ok {
51 Foo->new(bar => 'hello world');
52} '... failed the type check';
53