BEGIN {
$ENV
{PERL_TYPE_TINY_XS} = 0; }
sub
code_contains {
s/\s+//msg
for
(
my
(
$code
,
$want
) =
@_
);
index
(
$code
,
$want
) >= 0;
}
subtest
'strictness => CONDITION_STRING'
=>
sub
{
my
$got
= compile(
{
strictness
=>
'$::CHECK_TYPES'
,
want_source
=> 1 },
Int,
ArrayRef,
);
my
$expected
=
<<'EXPECTED';
# Parameter $_[0] (type: Int)
( not $::CHECK_TYPES )
or (do { my $tmp = $_[0]; defined($tmp) and !ref($tmp) and $tmp =~ /\A-?[0-9]+\z/ })
or Type::Tiny::_failed_check( 13, "Int", $_[0], varname => "\$_[0]" );
EXPECTED
ok code_contains(
$got
,
$expected
),
'code contains expected Int check'
or diag(
$got
);
is(
ref
(
eval
$got
),
'CODE'
,
'code compiles'
)
or diag(
$got
);
};
subtest
'strictness => 1'
=>
sub
{
my
$got
= compile(
{
strictness
=> 1,
want_source
=> 1 },
Int,
ArrayRef,
);
my
$expected
=
<<'EXPECTED';
# Parameter $_[0] (type: Int)
(do { my $tmp = $_[0]; defined($tmp) and !ref($tmp) and $tmp =~ /\A-?[0-9]+\z/ })
or Type::Tiny::_failed_check( 13, "Int", $_[0], varname => "\$_[0]" );
EXPECTED
ok code_contains(
$got
,
$expected
),
'code contains expected Int check'
or diag(
$got
);
is(
ref
(
eval
$got
),
'CODE'
,
'code compiles'
)
or diag(
$got
);
};
subtest
'strictness => 0'
=>
sub
{
my
$got
= compile(
{
strictness
=> 0,
want_source
=> 1 },
Int,
ArrayRef,
);
my
$expected
=
<<'EXPECTED';
# Parameter $_[0] (type: Int)
1; # ... nothing to do
EXPECTED
ok code_contains(
$got
,
$expected
),
'code contains expected Int check'
or diag(
$got
);
is(
ref
(
eval
$got
),
'CODE'
,
'code compiles'
)
or diag(
$got
);
};
my
$check
= compile(
{
strictness
=>
'$::CHECK_TYPES'
},
Int,
ArrayRef,
);
{
local
$::CHECK_TYPES = 0;
my
$e
= exception {
my
(
$number
,
$list
) =
$check
->( {}, {} );
my
(
$numbe2
,
$lis2
) =
$check
->();
};
is
$e
,
undef
;
}
{
local
$::CHECK_TYPES = 1;
my
$e
= exception {
my
(
$number
,
$list
) =
$check
->( {}, {} );
};
like
$e
,
qr/did not pass type constraint "Int"/
;
}
my
$check2
= compile(
{
strictness
=>
'$::CHECK_TYPES'
},
Int,
ArrayRef, {
strictness
=> 1 }
);
{
local
$::CHECK_TYPES = 0;
my
$e
= exception {
my
(
$number
,
$list
) =
$check2
->( {}, [] );
};
is
$e
,
undef
;
}
{
local
$::CHECK_TYPES = 0;
my
$e
= exception {
my
(
$number
,
$list
) =
$check2
->( {}, {} );
};
like
$e
,
qr/did not pass type constraint "ArrayRef"/
;
}
done_testing;