File: 02func-signatures.t

package info (click to toggle)
libxs-parse-sublike-perl 0.37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 516 kB
  • sloc: ansic: 944; perl: 930; sh: 6; makefile: 3
file content (89 lines) | stat: -rw-r--r-- 2,112 bytes parent folder | download
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
#!/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 lib "t";
use testcase "t::func";

BEGIN { $^H{"t::func/func"}++ }

# basic sig
{
   func withparam($x) { return $x + 1 }

   is( withparam( 4 ), 5, 'func with param' );
}

# The following are additional tests that our pre-5.31.3 backported
# parse_subsignature() works correctly
{
   func sum(@x) {
      my $ret = 0;
      $ret += $_ for @x;
      return $ret;
   }

   is( sum( 10, 20, 30 ), 60, 'func with slurpy parameter' );

   func firstandthird($x, $, $z) {
      return $x . $z;
   }

   is( firstandthird(qw( a b c )), "ac", 'func with unnamed parameter' );

   func withoptparam($one = 1) { return $one + 2 }

   is( withoptparam,      3, 'func with optional param missing' );
   is( withoptparam( 2 ), 4, 'func with optional param present' );

   func has_whitespace (
      $x
   ) {
      return $x;
   }

   is( has_whitespace( "value" ), "value", 'func with whitespace in signature' );

   # RT132284
   func noparams() { return "constant" }

   is( noparams, "constant", 'func with no params' );
   like( dies { noparams( 1, 2, 3 ) },
      # message was extended somewhere in perl 5.33
      qr/^Too many arguments for subroutine 'main::noparams' (\(.*\) )?at /,
      'Exception thrown from empty signature validation failure' );
}

# RT131571
{
   func withattr :method ($self, @args) { }

   ok( scalar( grep { m/^method$/ } attributes::get( \&withattr ) ),
      'func with attr and signture does not collide' );
}

# RT155630
{
   my $var = "outside";
   func defaultfromoutside($var = $var) { return $var; }

   is( defaultfromoutside(), "outside",
      'variable in defaulting expression is not shadowed by its own parameter' );

   # Not directly related to this bug but we might as well test this too
   func defaultfromfirst($x, $y = $x) { return $y; }

   is( defaultfromfirst( "first" ), "first",
      'variable in later defaulting expression can see earlier params' );
}

done_testing;