Add assert_valid() to Meta::TypeConstraint
[gitmo/Mouse.git] / t / 040_type_constraints / failing / 021_maybe_type_constraint.t
CommitLineData
b2b106d7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 36;
7use Test::Exception;
8
9use Mouse::Util::TypeConstraints;
10
11my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]');
12isa_ok($type, 'Mouse::Meta::TypeConstraint');
13isa_ok($type, 'Mouse::Meta::TypeConstraint::Parameterized');
14
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( Mouse::Meta::TypeConstraint::Parameterized->new( name => "__ANON__", parent => find_type_constraint("Maybe"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" );
20ok( !$type->equals( Mouse::Meta::TypeConstraint::Parameterized->new( name => "__ANON__", parent => find_type_constraint("Maybe"), type_parameter => find_type_constraint("Str") ) ), "not equal to clone with diff param" );
21ok( !$type->equals( Mouse::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Str]') ), "not equal to declarative version of diff param" );
22
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 Bar;
30 use Mouse;
31
32 package Foo;
33 use Mouse;
34 use Mouse::Util::TypeConstraints;
35
36 has 'arr' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);
37 has 'bar' => (is => 'rw', isa => class_type('Bar'));
38 has 'maybe_bar' => (is => 'rw', isa => maybe_type(class_type('Bar')));
39}
40
41lives_ok {
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 => []);
63} '... it worked!';
64
65lives_ok {
66 Foo->new(arr => undef);
67} '... it worked!';
68
69dies_ok {
70 Foo->new(arr => 100);
71} '... failed the type check';
72
73dies_ok {
74 Foo->new(arr => 'hello world');
75} '... failed the type check';
76
77
78{
79 package Test::MouseX::Types::Maybe;
80 use Mouse;
81
82 has 'Maybe_Int' => (is=>'rw', isa=>'Maybe[Int]');
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]]');
87}
88
89ok my $obj = Test::MouseX::Types::Maybe->new
90 => 'Create good test object';
91
92## Maybe[Int]
93
94ok my $Maybe_Int = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]')
95 => 'made TC Maybe[Int]';
96
97ok $Maybe_Int->check(1)
98 => 'passed (1)';
99
100ok $obj->Maybe_Int(1)
101 => 'assigned (1)';
102
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)';
114
115ok $Maybe_Int->check(undef)
116 => 'passed (undef)';
117
118ok sub {$obj->Maybe_Int(undef); 1}->()
119 => 'assigned (undef)';
120
121ok !$Maybe_Int->check("")
122 => 'failed ("")';
123
124throws_ok sub { $obj->Maybe_Int("") },
125 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
126 => 'failed assigned ("")';
127
128ok !$Maybe_Int->check("a")
129 => 'failed ("a")';
130
131throws_ok sub { $obj->Maybe_Int("a") },
132 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
133 => 'failed assigned ("a")';