A name-only subtype now inherits inlining from its parent type
[gitmo/Moose.git] / t / type_constraints / inlining.t
CommitLineData
5c40cf37 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::Fatal;
7use Test::More;
8
9use Moose::Util::TypeConstraints;
10
11#<<<
12subtype 'Inlinable',
13 as 'Str',
14 where { $_ !~ /Q/ },
15 inline_as { "defined $_[1] && ! ref $_[1] && $_[1] !~ /Q/" };
16
17subtype 'NotInlinable',
18 as 'Str',
19 where { $_ !~ /Q/ };
20#>>>
21
22my $inlinable = find_type_constraint('Inlinable');
23my $not_inlinable = find_type_constraint('NotInlinable');
24
25{
26 ok(
27 $inlinable->has_inlined_type_constraint,
28 'Inlinable returns true for has_inlined_type_constraint'
29 );
30
31 is(
32 $inlinable->_inline_check('$foo'),
33 'defined $foo && ! ref $foo && $foo !~ /Q/',
34 'got expected inline code for Inlinable constraint'
35 );
36
37 ok(
38 !$not_inlinable->has_inlined_type_constraint,
39 'NotInlinable returns false for has_inlined_type_constraint'
40 );
41
42 like(
43 exception { $not_inlinable->_inline_check('$foo') },
44 qr/Cannot inline a type constraint check for NotInlinable/,
45 'threw an exception when asking for inlinable code from type which cannot be inlined'
46 );
47}
48
49{
50 my $aofi = Moose::Util::TypeConstraints::find_or_create_type_constraint(
51 'ArrayRef[Inlinable]');
52
53 ok(
54 $aofi->has_inlined_type_constraint,
55 'ArrayRef[Inlinable] returns true for has_inlined_type_constraint'
56 );
57
58 is(
59 $aofi->_inline_check('$foo'),
43837b8a 60 q{ref $foo eq 'ARRAY' && &List::MoreUtils::all( sub { defined $_ && ! ref $_ && $_ !~ /Q/ }, @{$foo} )},
5c40cf37 61 'got expected inline code for ArrayRef[Inlinable] constraint'
62 );
63
64 my $aofni = Moose::Util::TypeConstraints::find_or_create_type_constraint(
65 'ArrayRef[NotInlinable]');
66
67 ok(
68 !$aofni->has_inlined_type_constraint,
69 'ArrayRef[NotInlinable] returns false for has_inlined_type_constraint'
70 );
71}
72
73subtype 'ArrayOfInlinable',
74 as 'ArrayRef[Inlinable]';
75
76subtype 'ArrayOfNotInlinable',
77 as 'ArrayRef[NotInlinable]';
5c40cf37 78{
5c40cf37 79 my $aofi = Moose::Util::TypeConstraints::find_or_create_type_constraint(
43837b8a 80 'ArrayOfInlinable');
5c40cf37 81
82 ok(
83 $aofi->has_inlined_type_constraint,
84 'ArrayOfInlinable returns true for has_inlined_type_constraint'
85 );
7487e61c 86
87 is(
88 $aofi->_inline_check('$foo'),
89 q{ref $foo eq 'ARRAY' && &List::MoreUtils::all( sub { defined $_ && ! ref $_ && $_ !~ /Q/ }, @{$foo} )},
90 'got expected inline code for ArrayOfInlinable constraint'
91 );
92
93 my $aofni = Moose::Util::TypeConstraints::find_or_create_type_constraint(
94 'ArrayOfNotInlinable');
95
96 ok(
97 !$aofni->has_inlined_type_constraint,
98 'ArrayOfNotInlinable returns false for has_inlined_type_constraint'
99 );
5c40cf37 100}
101
102{
103 my $hoaofi = Moose::Util::TypeConstraints::find_or_create_type_constraint(
104 'HashRef[ArrayRef[Inlinable]]');
105
106 ok(
107 $hoaofi->has_inlined_type_constraint,
108 'HashRef[ArrayRef[Inlinable]] returns true for has_inlined_type_constraint'
109 );
110
111 is(
112 $hoaofi->_inline_check('$foo'),
43837b8a 113 q{ref $foo eq 'HASH' && &List::MoreUtils::all( sub { ref $_ eq 'ARRAY' && &List::MoreUtils::all( sub { defined $_ && ! ref $_ && $_ !~ /Q/ }, @{$_} ) }, values %{$foo} )},
5c40cf37 114 'got expected inline code for HashRef[ArrayRef[Inlinable]] constraint'
115 );
116
117 my $hoaofni = Moose::Util::TypeConstraints::find_or_create_type_constraint(
118 'HashRef[ArrayRef[NotInlinable]]');
119
120 ok(
121 !$hoaofni->has_inlined_type_constraint,
122 'HashRef[ArrayRef[NotInlinable]] returns false for has_inlined_type_constraint'
123 );
124}
125
126{
127 my $iunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
128 'Inlinable | Object');
129
130 ok(
131 $iunion->has_inlined_type_constraint,
132 'Inlinable | Object returns true for has_inlined_type_constraint'
133 );
134
135 is(
136 $iunion->_inline_check('$foo'),
137 '(defined $foo && ! ref $foo && $foo !~ /Q/) || (Scalar::Util::blessed( $foo ))',
138 'got expected inline code for Inlinable | Object constraint'
139 );
140
141 my $niunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
142 'NotInlinable | Object');
143
144 ok(
145 !$niunion->has_inlined_type_constraint,
146 'NotInlinable | Object returns false for has_inlined_type_constraint'
147 );
148}
149
150{
151 my $iunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
152 'Object | Inlinable');
153
154 ok(
155 $iunion->has_inlined_type_constraint,
156 'Object | Inlinable returns true for has_inlined_type_constraint'
157 );
158
159 is(
160 $iunion->_inline_check('$foo'),
161 '(Scalar::Util::blessed( $foo )) || (defined $foo && ! ref $foo && $foo !~ /Q/)',
162 'got expected inline code for Object | Inlinable constraint'
163 );
164
165 my $niunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
166 'Object | NotInlinable');
167
168 ok(
169 !$niunion->has_inlined_type_constraint,
170 'Object | NotInlinable returns false for has_inlined_type_constraint'
171 );
172}
173
174{
175 my $iunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
176 'Object | Inlinable | CodeRef');
177
178 ok(
179 $iunion->has_inlined_type_constraint,
180 'Object | Inlinable | CodeRef returns true for has_inlined_type_constraint'
181 );
182
183 is(
184 $iunion->_inline_check('$foo'),
185 q{(Scalar::Util::blessed( $foo )) || (defined $foo && ! ref $foo && $foo !~ /Q/) || (ref $foo eq 'CODE')},
186 'got expected inline code for Object | Inlinable | CodeRef constraint'
187 );
188
189 my $niunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
190 'Object | NotInlinable | CodeRef');
191
192 ok(
193 !$niunion->has_inlined_type_constraint,
194 'Object | NotInlinable | CodeRef returns false for has_inlined_type_constraint'
195 );
196}
197
5c40cf37 198done_testing;