9 use Moose::Util::TypeConstraints;
15 inline_as { "defined $_[1] && ! ref $_[1] && $_[1] !~ /Q/" };
17 subtype 'NotInlinable',
22 my $inlinable = find_type_constraint('Inlinable');
23 my $not_inlinable = find_type_constraint('NotInlinable');
27 $inlinable->has_inlined_type_constraint,
28 'Inlinable returns true for has_inlined_type_constraint'
32 $inlinable->_inline_check('$foo'),
33 'defined $foo && ! ref $foo && $foo !~ /Q/',
34 'got expected inline code for Inlinable constraint'
38 !$not_inlinable->has_inlined_type_constraint,
39 'NotInlinable returns false for has_inlined_type_constraint'
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'
50 my $aofi = Moose::Util::TypeConstraints::find_or_create_type_constraint(
51 'ArrayRef[Inlinable]');
54 $aofi->has_inlined_type_constraint,
55 'ArrayRef[Inlinable] returns true for has_inlined_type_constraint'
59 $aofi->_inline_check('$foo'),
60 q{ref($foo) eq "ARRAY" && &List::MoreUtils::all(sub { defined $_ && ! ref $_ && $_ !~ /Q/ }, @{$foo})},
61 'got expected inline code for ArrayRef[Inlinable] constraint'
64 my $aofni = Moose::Util::TypeConstraints::find_or_create_type_constraint(
65 'ArrayRef[NotInlinable]');
68 !$aofni->has_inlined_type_constraint,
69 'ArrayRef[NotInlinable] returns false for has_inlined_type_constraint'
73 subtype 'ArrayOfInlinable',
74 as 'ArrayRef[Inlinable]';
76 subtype 'ArrayOfNotInlinable',
77 as 'ArrayRef[NotInlinable]';
79 my $aofi = Moose::Util::TypeConstraints::find_or_create_type_constraint(
83 $aofi->has_inlined_type_constraint,
84 'ArrayOfInlinable returns true for has_inlined_type_constraint'
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'
93 my $aofni = Moose::Util::TypeConstraints::find_or_create_type_constraint(
94 'ArrayOfNotInlinable');
97 !$aofni->has_inlined_type_constraint,
98 'ArrayOfNotInlinable returns false for has_inlined_type_constraint'
103 my $hoaofi = Moose::Util::TypeConstraints::find_or_create_type_constraint(
104 'HashRef[ArrayRef[Inlinable]]');
107 $hoaofi->has_inlined_type_constraint,
108 'HashRef[ArrayRef[Inlinable]] returns true for has_inlined_type_constraint'
112 $hoaofi->_inline_check('$foo'),
113 q{ref($foo) eq "HASH" && &List::MoreUtils::all(sub { ref($_) eq "ARRAY" && &List::MoreUtils::all(sub { defined $_ && ! ref $_ && $_ !~ /Q/ }, @{$_}) }, values %{$foo})},
114 'got expected inline code for HashRef[ArrayRef[Inlinable]] constraint'
117 my $hoaofni = Moose::Util::TypeConstraints::find_or_create_type_constraint(
118 'HashRef[ArrayRef[NotInlinable]]');
121 !$hoaofni->has_inlined_type_constraint,
122 'HashRef[ArrayRef[NotInlinable]] returns false for has_inlined_type_constraint'
127 my $iunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
128 'Inlinable | Object');
131 $iunion->has_inlined_type_constraint,
132 'Inlinable | Object returns true for has_inlined_type_constraint'
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'
141 my $niunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
142 'NotInlinable | Object');
145 !$niunion->has_inlined_type_constraint,
146 'NotInlinable | Object returns false for has_inlined_type_constraint'
151 my $iunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
152 'Object | Inlinable');
155 $iunion->has_inlined_type_constraint,
156 'Object | Inlinable returns true for has_inlined_type_constraint'
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'
165 my $niunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
166 'Object | NotInlinable');
169 !$niunion->has_inlined_type_constraint,
170 'Object | NotInlinable returns false for has_inlined_type_constraint'
175 my $iunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
176 'Object | Inlinable | CodeRef');
179 $iunion->has_inlined_type_constraint,
180 'Object | Inlinable | CodeRef returns true for has_inlined_type_constraint'
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'
189 my $niunion = Moose::Util::TypeConstraints::find_or_create_type_constraint(
190 'Object | NotInlinable | CodeRef');
193 !$niunion->has_inlined_type_constraint,
194 'Object | NotInlinable | CodeRef returns false for has_inlined_type_constraint'