File: 45sub-signatures.t

package info (click to toggle)
libfuture-asyncawait-perl 0.70-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 528 kB
  • sloc: perl: 2,647; ansic: 118; pascal: 34; makefile: 3
file content (85 lines) | stat: -rw-r--r-- 1,815 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
#!/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 Future::AsyncAwait;

{
   async sub add ( $x, $y )
   {
      return $x + $y;
   }

   my $f = add( 2, 3 );
   is( $f->get, 5, 'add(2,3)' );
}

# return in argument default still Future-wraps
{
   async sub identity ( $x, $y = return $x ) { }

   my $f = identity( 123 );
   isa_ok( $f, [ "Future" ], '$f' );
   is( $f->get, 123, '$f->get on return in arg default' );
}

# argcount exceptions are thrown synchronously
{
   async sub one_arg ( $x ) { return $x; }

   like( dies { my $f = one_arg() },
      qr/^Too few arguments for subroutine 'main::one_arg'/,
      'argcheck failure happens synchronously' );

   like( dies { my $f = one_arg( 1, 2 ) },
      qr/^Too many arguments for subroutine 'main::one_arg'/,
      'argcheck failure happens synchronously' );
}

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

   my $f = sum( 10, 20, 30 );
   is( $f->get, 60, 'parsed slurpy parameter' );

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

   $f = firstandthird(qw( a b c ));
   is( $f->get, "ac", 'parsed unnamed parameter' );

   async sub withdefault ( $one = 1, $two = 2 ) {
      return $one + $two;
   }

   $f = withdefault();
   is( $f->get, 3, 'parsed parameters with default expr' );
}

# RT131571
{
   ok( defined eval q{
      use experimental 'signatures';
      async sub func :method ( $self, @args ) { }
      1;
   }, 'signatures do not leak into attributes (RT131571)' ) or
      diag( "Error was $@" );
}

done_testing;