Debian Codegraph @

t/05func-sig-attributes.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/afunc"}++ }
18 BEGIN { $^H{"t::func/Attribute"}++ }
19
20 our @ATTRIBUTE_APPLIED;
21 our @ATTRIBUTE_SAW_OPTREES;
22 our $ATTRIBUTE_INVOKED;
23
24 sub NZ { !Test2::Tools::Compare::number(0); }
25
26 {
27 afunc withattr( $x :Attribute, $y :Attribute(Value) = 123 ) { }
28
29 is( \@ATTRIBUTE_APPLIED,
30 [ '$x' => undef, '$y' => "Value" ],
31 ':Attribute applied to subroutine parameters' );
32
33 is( \@ATTRIBUTE_SAW_OPTREES,
34 [
35 { op => NZ(), varop => NZ(), defop => 0 },
36 { op => NZ(), varop => NZ(), defop => NZ() },
37 ],
38 ':Attribute saw some optrees' );
39
40 withattr( "X", "Y" );
41
42 is( $ATTRIBUTE_INVOKED, 2,
43 ':Attribute modified optree got invoked' );
44 }
45
46 if( $^V ge v5.26 ) {
47 undef @ATTRIBUTE_APPLIED;
48 undef @ATTRIBUTE_SAW_OPTREES;
49 undef $ATTRIBUTE_INVOKED;
50
51 BEGIN { $^H{"t::func/nafunc"}++ }
52 BEGIN { $^H{"t::func/Attribute"}++ }
53
54 defined eval <<'EOF'
55 nafunc withattrnamed( :$alpha :Attribute, :$beta :Attribute(NamedValue) = 456 ) { }
56 1;
57 EOF
58 or die "Cannot compile - $@";
59
60 is( \@ATTRIBUTE_APPLIED,
61 [ ':$alpha' => undef, ':$beta' => "NamedValue" ],
62 ':Attribute applied to named subroutine parameters' );
63
64 # Named params don't get a varop
65 # They don't get an op *at all* if there's no default
66 is( \@ATTRIBUTE_SAW_OPTREES,
67 [
68 { op => 0, varop => 0, defop => 0 },
69 { op => NZ(), varop => 0, defop => NZ() },
70 ],
71 ':Attribute saw some optrees' );
72
73 withattrnamed( alpha => "A", beta => "B" );
74
75 is( $ATTRIBUTE_INVOKED, 2,
76 ':Attribute modified optree got invoked for named' );
77 }
78
79 # RT168812
80 # In this file purely to ensure we use our version of parse_subsignature()
81 {
82 is(
83 eval( 'afunc f01( $ = undef ) { }; 1' ) ? undef : $@,
84 undef,
85 'Function with anonymous parameter default expression as undef is permitted' );
86
87 like(
88 eval( 'afunc f02( $ = 123 ) { }' ) ? undef : $@,
89 qr/^Unnamed positional parameters cannot have defaulting expressions /,
90 'Function with anonymous parameter defaulting expression fails to parse' );
91
92 is(
93 eval( 'afunc f03( $x = ) { }; 1' ) ? undef : $@,
94 undef,
95 'Function with missing parameter default expression implies undef' );
96
97 is(
98 eval( 'afunc f04( $ = ) { }; 1' ) ? undef : $@,
99 undef,
100 'Function with missing unnamed parameter default expression implies undef' );
101
102 is(
103 eval( 'afunc f05( $= ) { }; 1' ) ? undef : $@,
104 undef,
105 'Function with missing unnamed parameter default expression implies undef' );
106 }
107
108 done_testing;