Debian Codegraph @

t/02func-signatures.t

raw
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 lib "t";
15 use testcase "t::func";
16
17 BEGIN { $^H{"t::func/func"}++ }
18
19 # basic sig
20 {
21 func withparam($x) { return $x + 1 }
22
23 is( withparam( 4 ), 5, 'func with param' );
24 }
25
26 # The following are additional tests that our pre-5.31.3 backported
27 # parse_subsignature() works correctly
28 {
29 func sum(@x) {
30 my $ret = 0;
31 $ret += $_ for @x;
32 return $ret;
33 }
34
35 is( sum( 10, 20, 30 ), 60, 'func with slurpy parameter' );
36
37 func firstandthird($x, $, $z) {
38 return $x . $z;
39 }
40
41 is( firstandthird(qw( a b c )), "ac", 'func with unnamed parameter' );
42
43 func withoptparam($one = 1) { return $one + 2 }
44
45 is( withoptparam, 3, 'func with optional param missing' );
46 is( withoptparam( 2 ), 4, 'func with optional param present' );
47
48 func has_whitespace (
49 $x
50 ) {
51 return $x;
52 }
53
54 is( has_whitespace( "value" ), "value", 'func with whitespace in signature' );
55
56 # RT132284
57 func noparams() { return "constant" }
58
59 is( noparams, "constant", 'func with no params' );
60 like( dies { noparams( 1, 2, 3 ) },
61 # message was extended somewhere in perl 5.33
62 qr/^Too many arguments for subroutine 'main::noparams' (\(.*\) )?at /,
63 'Exception thrown from empty signature validation failure' );
64 }
65
66 # RT131571
67 {
68 func withattr :method ($self, @args) { }
69
70 ok( scalar( grep { m/^method$/ } attributes::get( \&withattr ) ),
71 'func with attr and signture does not collide' );
72 }
73
74 # RT155630
75 {
76 my $var = "outside";
77 func defaultfromoutside($var = $var) { return $var; }
78
79 is( defaultfromoutside(), "outside",
80 'variable in defaulting expression is not shadowed by its own parameter' );
81
82 # Not directly related to this bug but we might as well test this too
83 func defaultfromfirst($x, $y = $x) { return $y; }
84
85 is( defaultfromfirst( "first" ), "first",
86 'variable in later defaulting expression can see earlier params' );
87 }
88
89 done_testing;