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
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
BEGIN {
$] >= 5.026000 or plan skip_all => "No parse_subsignature()";
}
use feature 'signatures';
no warnings 'experimental';
use Sublike::Extended;
# basic sub
{
extended sub t1 { return "named" }
my $t2 = extended sub { return "anon" };
is( t1(), "named",
'extended sub can make basic named' );
is( $t2->(), "anon",
'extended sub can make basic anon' );
}
# sub with named params
{
extended sub t3 (:$x, :$y) { return "x=$x y=$y"; }
my $t4 = extended sub ( :$i, :$j ) { return "i=$i j=$j"; };
is( t3( x => 10, y => 20 ), "x=10 y=20",
'extended sub can make named param subs' );
is( $t4->( j => 40, i => 30 ), "i=30 j=40",
'extended sub can make named param anon subs' );
}
use lib "t";
use testcase "t::func";
BEGIN { $^H{"t::func/Attribute"}++ }
our @ATTRIBUTE_APPLIED;
{
extended sub t5 ($x :Attribute, $y :Attribute(Value)) { }
my @t5_attributes;
BEGIN { @t5_attributes = @ATTRIBUTE_APPLIED; @ATTRIBUTE_APPLIED = (); }
# No need to call it just to see these
is( \@t5_attributes,
[ '$x' => undef, '$y' => "Value" ],
':Attribute applied to subroutine parameters' );
}
{
use Sublike::Extended 'sub';
sub t6 ( $z :Attribute, :$alpha :Attribute(Value) ) { }
my @t6_attributes;
BEGIN { @t6_attributes = @ATTRIBUTE_APPLIED; @ATTRIBUTE_APPLIED = (); }
is( \@t6_attributes,
[ '$z' => undef, ':$alpha' => "Value" ],
':Attribute applied to subroutine parameters of `sub` directly' );
}
done_testing;
|