Commit | Line | Data |
7e4e1ad4 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
b10dde3a |
7 | use Test::Fatal; |
7e4e1ad4 |
8 | |
7ff56534 |
9 | use Moose::Util::TypeConstraints; |
7e4e1ad4 |
10 | |
620db045 |
11 | my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]'); |
7e4e1ad4 |
12 | isa_ok($type, 'Moose::Meta::TypeConstraint'); |
13 | isa_ok($type, 'Moose::Meta::TypeConstraint::Parameterized'); |
14 | |
dabed765 |
15 | ok( $type->equals($type), "equals self" ); |
16 | ok( !$type->equals($type->parent), "not equal to parent" ); |
17 | ok( !$type->equals(find_type_constraint("Maybe")), "not equal to Maybe" ); |
18 | ok( $type->parent->equals(find_type_constraint("Maybe")), "parent is Maybe" ); |
19 | ok( $type->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "__ANON__", parent => find_type_constraint("Maybe"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" ); |
20 | ok( !$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 |
21 | ok( !$type->equals( Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Str]') ), "not equal to declarative version of diff param" ); |
dabed765 |
22 | |
7e4e1ad4 |
23 | ok($type->check(10), '... checked type correctly (pass)'); |
24 | ok($type->check(undef), '... checked type correctly (pass)'); |
25 | ok(!$type->check('Hello World'), '... checked type correctly (fail)'); |
26 | ok(!$type->check([]), '... checked type correctly (fail)'); |
27 | |
28 | { |
1b2c9bda |
29 | package Bar; |
30 | use Moose; |
31 | |
7e4e1ad4 |
32 | package Foo; |
33 | use Moose; |
1b2c9bda |
34 | use Moose::Util::TypeConstraints; |
d03bd989 |
35 | |
36 | has 'arr' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1); |
1b2c9bda |
37 | has 'bar' => (is => 'rw', isa => class_type('Bar')); |
38 | has 'maybe_bar' => (is => 'rw', isa => maybe_type(class_type('Bar'))); |
7e4e1ad4 |
39 | } |
40 | |
b10dde3a |
41 | is( exception { |
1b2c9bda |
42 | Foo->new(arr => [], bar => Bar->new); |
b10dde3a |
43 | }, undef, '... Bar->new isa Bar' ); |
1b2c9bda |
44 | |
b10dde3a |
45 | isnt( exception { |
1b2c9bda |
46 | Foo->new(arr => [], bar => undef); |
b10dde3a |
47 | }, undef, '... undef isnta Bar' ); |
1b2c9bda |
48 | |
b10dde3a |
49 | is( exception { |
1b2c9bda |
50 | Foo->new(arr => [], maybe_bar => Bar->new); |
b10dde3a |
51 | }, undef, '... Bar->new isa maybe(Bar)' ); |
1b2c9bda |
52 | |
b10dde3a |
53 | is( exception { |
1b2c9bda |
54 | Foo->new(arr => [], maybe_bar => undef); |
b10dde3a |
55 | }, undef, '... undef isa maybe(Bar)' ); |
1b2c9bda |
56 | |
b10dde3a |
57 | isnt( exception { |
1b2c9bda |
58 | Foo->new(arr => [], maybe_bar => 1); |
b10dde3a |
59 | }, undef, '... 1 isnta maybe(Bar)' ); |
1b2c9bda |
60 | |
b10dde3a |
61 | is( exception { |
1b2c9bda |
62 | Foo->new(arr => []); |
b10dde3a |
63 | }, undef, '... it worked!' ); |
7e4e1ad4 |
64 | |
b10dde3a |
65 | is( exception { |
1b2c9bda |
66 | Foo->new(arr => undef); |
b10dde3a |
67 | }, undef, '... it worked!' ); |
7e4e1ad4 |
68 | |
b10dde3a |
69 | isnt( exception { |
1b2c9bda |
70 | Foo->new(arr => 100); |
b10dde3a |
71 | }, undef, '... failed the type check' ); |
7e4e1ad4 |
72 | |
b10dde3a |
73 | isnt( exception { |
1b2c9bda |
74 | Foo->new(arr => 'hello world'); |
b10dde3a |
75 | }, undef, '... failed the type check' ); |
7e4e1ad4 |
76 | |
f805496a |
77 | |
78 | { |
79 | package Test::MooseX::Types::Maybe; |
80 | use Moose; |
81 | |
82 | has 'Maybe_Int' => (is=>'rw', isa=>'Maybe[Int]'); |
d03bd989 |
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]]'); |
f805496a |
87 | } |
88 | |
89 | ok my $obj = Test::MooseX::Types::Maybe->new |
90 | => 'Create good test object'; |
91 | |
92 | ## Maybe[Int] |
93 | |
94 | ok my $Maybe_Int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]') |
95 | => 'made TC Maybe[Int]'; |
d03bd989 |
96 | |
f805496a |
97 | ok $Maybe_Int->check(1) |
98 | => 'passed (1)'; |
d03bd989 |
99 | |
f805496a |
100 | ok $obj->Maybe_Int(1) |
101 | => 'assigned (1)'; |
d03bd989 |
102 | |
f805496a |
103 | ok $Maybe_Int->check() |
104 | => 'passed ()'; |
105 | |
106 | ok $obj->Maybe_Int() |
107 | => 'assigned ()'; |
108 | |
109 | ok $Maybe_Int->check(0) |
110 | => 'passed (0)'; |
111 | |
112 | ok defined $obj->Maybe_Int(0) |
113 | => 'assigned (0)'; |
d03bd989 |
114 | |
f805496a |
115 | ok $Maybe_Int->check(undef) |
116 | => 'passed (undef)'; |
d03bd989 |
117 | |
f805496a |
118 | ok sub {$obj->Maybe_Int(undef); 1}->() |
119 | => 'assigned (undef)'; |
d03bd989 |
120 | |
f805496a |
121 | ok !$Maybe_Int->check("") |
122 | => 'failed ("")'; |
d03bd989 |
123 | |
b10dde3a |
124 | like( exception { $obj->Maybe_Int("") }, qr/Attribute \(Maybe_Int\) does not pass the type constraint/, 'failed assigned ("")' ); |
f805496a |
125 | |
126 | ok !$Maybe_Int->check("a") |
127 | => 'failed ("a")'; |
128 | |
b10dde3a |
129 | like( exception { $obj->Maybe_Int("a") }, qr/Attribute \(Maybe_Int\) does not pass the type constraint/, 'failed assigned ("a")' ); |
a28e50e4 |
130 | |
131 | done_testing; |