Debian Codegraph @

t/04func-named-params.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/nfunc"}++ }
18
19 # a signature with experimental named parameter support
20 {
21 my %was_rest;
22 nfunc withx(:$x, %rest) { %was_rest = %rest; return $x }
23
24 is( withx( x => 123 ), 123, 'named param extracts value' );
25 is( \%was_rest, {}, 'named param not visible in %rest' );
26
27 withx( x => 1, y => 2 );
28 is( \%was_rest, { y => 2 }, 'other params still visible in %rest' );
29
30 my $LINE = __LINE__+1;
31 like( dies { withx() },
32 qr/^Missing argument 'x' for subroutine main::withx at \S+ line $LINE\./,
33 'complaint from missing named param' );
34
35 nfunc with2x(:$x1, :$x2) { return "x1=$x1 x2=$x2"; }
36 is( with2x( x1 => 10, x2 => 20 ), "x1=10 x2=20",
37 'supports multiple named params' );
38
39 like( dies { with2x() },
40 # Order here is not reliable
41 qr/^Missing arguments 'x[12]', 'x[21]' for subroutine main::with2x at /,
42 'complaint from missing two named params includes both' );
43 }
44
45 # named params can still have defaults
46 {
47 nfunc withy(:$y = "Y", %rest) { return $y }
48
49 is( withy( y => 456 ), 456, 'named param with default' );
50 is( withy(), "Y", 'named param applies default' );
51 }
52
53 # named param defaulting expressions can still see earlier named params
54 {
55 my $ret_y; my $got_x;
56 sub y_from_x ($x) { $got_x = $x; return $ret_y; }
57
58 nfunc withdefaults(:$x, :$y = y_from_x($x)) { return "$x-$y" }
59
60 $ret_y = "Y_VALUE";
61 is( withdefaults( x => "X_VALUE" ), "X_VALUE-Y_VALUE",
62 'named param defaults can see earlier default params' );
63 is( $got_x, "X_VALUE", 'param default expression was invoked' );
64 }
65
66 # named params can use //= and ||=
67 {
68 nfunc withdefined(:$x //= "default") { return $x }
69
70 is( withdefined( x => "value" ), "value", 'named param with defined-or' );
71 is( withdefined( x => undef ), "default", 'named param with defined-or defaulting' );
72
73 nfunc withtrue(:$x ||= "default") { return $x }
74
75 is( withtrue( x => "value" ), "value", 'named param with true-or' );
76 is( withtrue( x => "" ), "default", 'named param with true-or defaulting' );
77 }
78
79 # named params still work without a slurpy
80 {
81 nfunc withz(:$z) { return $z }
82
83 is( withz( z => 789 ), 789, 'named param without slurpy' );
84
85 my $LINE = __LINE__+1;
86 like( dies { withz( z => 1, w => 1 ); 1 },
87 qr/^Unrecognised argument 'w' for subroutine main::withz at \S+ line $LINE\./,
88 'complaint from unknown param' );
89 }
90
91 # mixed positional+named
92 {
93 nfunc withboth($x, :$y = "def") { return "x=$x y=$y"; }
94
95 is( withboth(1, y => 2), "x=1 y=2",
96 'supports mixed positional + named' );
97 is( withboth(1), "x=1 y=def",
98 'mixed still applies defaults' );
99
100 nfunc withlots($p1, $p2, $p3, :$n1, :$n2, :$n3) {
101 return "($p1, $p2, $p3) + (1=$n1, 2=$n2, 3=$n3)";
102 }
103
104 is( withlots("a", "b", "c", n1 => "d", n3 => "f", n2 => "e"),
105 "(a, b, c) + (1=d, 2=e, 3=f)",
106 'supports multiple positional + named');
107 }
108
109 # named params can support a slurpy array
110 {
111 nfunc withslurpyarray(:$alpha = undef, :$beta = undef, @rest) {
112 return @rest;
113 }
114
115 is( [ withslurpyarray( x => 123, alpha => "no", beta => "no", y => 456, x => 789 ) ],
116 [ x => 123, y => 456, x => 789 ],
117 'supports slurpy array that preserves duplicates/order' );
118
119 is( [ withslurpyarray( 'single' ) ],
120 [ 'single' ],
121 'slurpy array does not gain phantom undef' );
122 }
123
124 # diagnostics on duplicates
125 {
126 sub warnings_from ( $code ) {
127 my $warnings = "";
128 local $SIG{__WARN__} = sub { $warnings .= $_[0] };
129 eval( "$code; 1" ) or die $@;
130 return $warnings;
131 }
132
133 like( warnings_from( 'nfunc diag1($x, :$x) { }' ),
134 qr/^"my" variable \$x masks earlier declaration in same scope at /,
135 'warning from duplicated parameter name' );
136 }
137
138 # RT155654
139 {
140 nfunc unnamedslurpyarray($x, :$y, @) { return "x=$x y=$y"; }
141
142 nfunc unnamedslurpyhash ($x, :$y, %) { return "x=$x y=$y"; }
143
144 pass( 'code with unnamed slurpies compiles OK' );
145
146 is( unnamedslurpyarray( "X", y => "Y", more => "here" ), "x=X y=Y",
147 'result of invoking function with unnamed slurpy array' );
148
149 is( unnamedslurpyhash ( "X", y => "Y", more => "here" ), "x=X y=Y",
150 'result of invoking function with unnamed slurpy hash' );
151 }
152
153 # refalias named parameters are tested in a later test script, along with
154 # other refalias
155
156 done_testing;