| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use v5.14; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test2::V0; |
| 7 |
BEGIN { |
| 8 |
$] >= 5.026000 or plan skip_all => "No parse_subsignature()"; |
| 9 |
} |
| 10 |
|
| 11 |
use feature 'signatures'; |
| 12 |
no warnings 'experimental'; |
| 13 |
|
| 14 |
use Sublike::Extended; |
| 15 |
|
| 16 |
# basic sub |
| 17 |
{ |
| 18 |
extended sub t1 { return "named" } |
| 19 |
my $t2 = extended sub { return "anon" }; |
| 20 |
|
| 21 |
is( t1(), "named", |
| 22 |
'extended sub can make basic named' ); |
| 23 |
is( $t2->(), "anon", |
| 24 |
'extended sub can make basic anon' ); |
| 25 |
} |
| 26 |
|
| 27 |
# sub with named params |
| 28 |
{ |
| 29 |
extended sub t3 (:$x, :$y) { return "x=$x y=$y"; } |
| 30 |
my $t4 = extended sub ( :$i, :$j ) { return "i=$i j=$j"; }; |
| 31 |
|
| 32 |
is( t3( x => 10, y => 20 ), "x=10 y=20", |
| 33 |
'extended sub can make named param subs' ); |
| 34 |
is( $t4->( j => 40, i => 30 ), "i=30 j=40", |
| 35 |
'extended sub can make named param anon subs' ); |
| 36 |
} |
| 37 |
|
| 38 |
use lib "t"; |
| 39 |
use testcase "t::func"; |
| 40 |
|
| 41 |
BEGIN { $^H{"t::func/Attribute"}++ } |
| 42 |
|
| 43 |
our @ATTRIBUTE_APPLIED; |
| 44 |
|
| 45 |
{ |
| 46 |
extended sub t5 ($x :Attribute, $y :Attribute(Value)) { } |
| 47 |
|
| 48 |
my @t5_attributes; |
| 49 |
BEGIN { @t5_attributes = @ATTRIBUTE_APPLIED; @ATTRIBUTE_APPLIED = (); } |
| 50 |
|
| 51 |
# No need to call it just to see these |
| 52 |
|
| 53 |
is( \@t5_attributes, |
| 54 |
[ '$x' => undef, '$y' => "Value" ], |
| 55 |
':Attribute applied to subroutine parameters' ); |
| 56 |
} |
| 57 |
|
| 58 |
{ |
| 59 |
use Sublike::Extended 'sub'; |
| 60 |
|
| 61 |
sub t6 ( $z :Attribute, :$alpha :Attribute(Value) ) { } |
| 62 |
|
| 63 |
my @t6_attributes; |
| 64 |
BEGIN { @t6_attributes = @ATTRIBUTE_APPLIED; @ATTRIBUTE_APPLIED = (); } |
| 65 |
|
| 66 |
is( \@t6_attributes, |
| 67 |
[ '$z' => undef, ':$alpha' => "Value" ], |
| 68 |
':Attribute applied to subroutine parameters of `sub` directly' ); |
| 69 |
} |
| 70 |
|
| 71 |
done_testing; |