3 # Regression tests for attributes.pm and the C< : attrs> syntax.
17 print "1..".NTESTS."\n";
19 $SIG{__WARN__} = sub { die @_ };
23 if (!$@ ne !$_[0] || $_[0] && $@ !~ $_[0]) {
30 print "# Got unexpected success\n";
33 print "# Expected: $_[0]\n";
36 print "# Expected success\n";
41 elsif (@_ == 3 && $_[1] ne $_[2]) {
42 print "# Got: $_[1]\n";
43 print "# Expected: $_[2]\n";
47 print $bad."ok ".++$test."\n";
50 eval 'sub t1 ($) : locked { $_[0]++ }';
54 eval 'sub t2 : locked { $_[0]++ }';
58 eval 'sub t3 ($) : locked ;';
62 eval 'sub t4 : locked ;';
67 eval '$anon1 = sub ($) : locked:method { $_[0]++ }';
72 eval '$anon2 = sub : locked : method { $_[0]++ }';
77 eval '$anon3 = sub : method { $_[0]->[1] }';
81 eval 'sub e1 ($) : plugh ;';
82 mytest qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
85 eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
86 mytest qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
89 eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
90 mytest qr/Unterminated attribute parameter in attribute list at/;
93 eval 'sub e4 ($) : plugh + xyzzy ;';
94 mytest qr/Invalid separator character '[+]' in attribute list at/;
97 eval 'my main $x : = 0;';
109 eval 'my ($x) : = 0;';
121 eval 'my ($x,$y) : = 0;';
129 eval 'my ($x,$y) : ;';
133 eval 'my ($x,$y) : plugh;';
134 mytest qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
137 sub A::MODIFY_SCALAR_ATTRIBUTES { return }
138 eval 'my A $x : plugh;';
139 mytest qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
142 eval 'my A $x : plugh plover;';
143 mytest qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
146 eval 'package Cat; my Cat @socks;';
147 mytest qr/^Can't declare class for non-scalar \@socks in "my"/;
150 sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
153 *Y::bar = \&X::foo; # second time for -w
154 eval 'package Z; sub Y::bar : foo';
158 eval 'package Z; sub Y::baz : locked {}';
159 my @attrs = eval 'attributes::get \&Y::baz';
160 mytest '', "@attrs", "locked";
163 @attrs = eval 'attributes::get $anon1';
164 mytest '', "@attrs", "locked method";
168 sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
169 my $thunk = eval 'bless +sub : method locked { 1 }, "Z"';
170 mytest '', ref($thunk), "Z";
173 @attrs = eval 'attributes::get $thunk';
174 mytest '', "@attrs", "locked method Z";
177 # Test ability to modify existing sub's (or XSUB's) attributes.
178 eval 'package A; sub X { $_[0] } sub X : lvalue';
179 @attrs = eval 'attributes::get \&A::X';
180 mytest '', "@attrs", "lvalue";
183 # Above not with just 'pure' built-in attributes.
184 sub Z::MODIFY_CODE_ATTRIBUTES { (); }
185 eval 'package Z; sub L { $_[0] } sub L : Z lvalue';
186 @attrs = eval 'attributes::get \&Z::L';
187 mytest '', "@attrs", "lvalue Z";
191 # Begin testing attributes that tie
196 sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
197 sub FETCH { ${$_[0]} }
199 #print "# In Ttie::STORE\n";
204 sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
210 my $x : TieLoop = $i;
211 $x != $i*2 and ::mytest "", $x, $i*2;
217 # Other tests should be added above this line
219 sub NTESTS () { $ntests }