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