3 # Regression tests for attributes.pm and the C< : attrs> syntax.
15 $SIG{__WARN__} = sub { die @_ };
22 eval_ok 'sub t1 ($) : locked { $_[0]++ }';
23 eval_ok 'sub t2 : locked { $_[0]++ }';
24 eval_ok 'sub t3 ($) : locked ;';
25 eval_ok 'sub t4 : locked ;';
26 our $anon1; eval_ok '$anon1 = sub ($) : locked:method { $_[0]++ }';
27 our $anon2; eval_ok '$anon2 = sub : locked : method { $_[0]++ }';
28 our $anon3; eval_ok '$anon3 = sub : method { $_[0]->[1] }';
30 eval 'sub e1 ($) : plugh ;';
31 like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
33 eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
34 like $@, qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
36 eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
37 like $@, qr/Unterminated attribute parameter in attribute list at/;
39 eval 'sub e4 ($) : plugh + xyzzy ;';
40 like $@, qr/Invalid separator character '[+]' in attribute list at/;
42 eval_ok 'my main $x : = 0;';
43 eval_ok 'my $x : = 0;';
45 eval_ok 'my ($x) : = 0;';
47 eval_ok 'my ($x) : ;';
48 eval_ok 'my ($x,$y) : = 0;';
49 eval_ok 'my ($x,$y) ;';
50 eval_ok 'my ($x,$y) : ;';
52 eval 'my ($x,$y) : plugh;';
53 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
56 eval '{my $x : plugh}';
57 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
58 eval '{my ($x,$y) : plugh(})}';
59 like $@, qr/^Invalid SCALAR attribute: ["']?plugh\(}\)["']? at/;
61 # More syntax tests from the attributes manpage
62 eval 'my $x : switch(10,foo(7,3)) : expensive;';
63 like $@, qr/^Invalid SCALAR attributes: ["']?switch\(10,foo\(7,3\)\) : expensive["']? at/;
64 eval q/my $x : Ugly('\(") :Bad;/;
65 like $@, qr/^Invalid SCALAR attributes: ["']?Ugly\('\\\("\) : Bad["']? at/;
67 like $@, qr/^Invalid SCALAR attribute: ["']?_5x5["']? at/;
68 eval 'my $x : locked method;';
69 like $@, qr/^Invalid SCALAR attributes: ["']?locked : method["']? at/;
70 eval 'my $x : switch(10,foo();';
71 like $@, qr/^Unterminated attribute parameter in attribute list at/;
72 eval q/my $x : Ugly('(');/;
73 like $@, qr/^Unterminated attribute parameter in attribute list at/;
76 eval 'my $x : Y2::north;';
77 like $@, qr/Invalid separator character ':' in attribute list at/;
79 sub A::MODIFY_SCALAR_ATTRIBUTES { return }
80 eval 'my A $x : plugh;';
81 like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
83 eval 'my A $x : plugh plover;';
84 like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
86 no warnings 'reserved';
87 eval 'my A $x : plugh;';
90 eval 'package Cat; my Cat @socks;';
91 like $@, qr/^Can't declare class for non-scalar \@socks in "my"/;
93 sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
96 *Y::bar = \&X::foo; # second time for -w
97 eval 'package Z; sub Y::bar : foo';
100 eval 'package Z; sub Y::baz : locked {}';
101 my @attrs = eval 'attributes::get \&Y::baz';
102 is "@attrs", "locked";
104 @attrs = eval 'attributes::get $anon1';
105 is "@attrs", "locked method";
108 sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
109 my $thunk = eval 'bless +sub : method locked { 1 }, "Z"';
112 @attrs = eval 'attributes::get $thunk';
113 is "@attrs", "locked method Z";
115 # Test attributes on predeclared subroutines:
116 eval 'package A; sub PS : lvalue';
117 @attrs = eval 'attributes::get \&A::PS';
118 is "@attrs", "lvalue";
120 # Test ability to modify existing sub's (or XSUB's) attributes.
121 eval 'package A; sub X { $_[0] } sub X : lvalue';
122 @attrs = eval 'attributes::get \&A::X';
123 is "@attrs", "lvalue";
125 # Above not with just 'pure' built-in attributes.
126 sub Z::MODIFY_CODE_ATTRIBUTES { (); }
127 eval 'package Z; sub L { $_[0] } sub L : Z lvalue';
128 @attrs = eval 'attributes::get \&Z::L';
129 is "@attrs", "lvalue Z";
131 # Begin testing attributes that tie
136 sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
137 sub FETCH { ${$_[0]} }
143 sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
149 my $x : TieLoop = $i;
150 $x != $i*2 and ::is $x, $i*2;
155 eval 'our ${""} : foo = 1';
156 like $@, qr/Can't declare scalar dereference in "our"/;
157 eval 'my $$foo : bar = 1';
158 like $@, qr/Can't declare scalar dereference in "my"/;
161 my @code = qw(lvalue locked method);
162 unshift @code, 'assertion' if $] >= 5.009;
163 my @other = qw(shared unique);
165 $valid{CODE} = {map {$_ => 1} @code};
166 $valid{SCALAR} = {map {$_ => 1} @other};
167 $valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
169 our ($scalar, @array, %hash);
170 foreach my $value (\&foo, \$scalar, \@array, \%hash) {
171 my $type = ref $value;
172 foreach my $negate ('', '-') {
173 foreach my $attr (@code, @other) {
174 my $attribute = $negate . $attr;
175 eval "use attributes __PACKAGE__, \$value, '$attribute'";
176 if ($valid{$type}{$attr}) {
177 if ($attribute eq '-shared') {
178 like $@, qr/^A variable may not be unshared/;
180 is( $@, '', "$type attribute $attribute");
183 like $@, qr/^Invalid $type attribute: $attribute/,
184 "Bogus $type attribute $attribute should fail";