-#!./perl -w
+#!./perl
# Regression tests for attributes.pm and the C< : attrs> syntax.
+use warnings;
+
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
+ require './test.pl';
}
-sub NTESTS () ;
-
-my ($test, $ntests);
-BEGIN {$ntests=0}
-$test=0;
-my $failed = 0;
-
-print "1..".NTESTS."\n";
+plan 'no_plan';
$SIG{__WARN__} = sub { die @_ };
-sub mytest {
- if (!$@ ne !$_[0] || $_[0] && $@ !~ $_[0]) {
- if ($@) {
- my $x = $@;
- $x =~ s/\n.*\z//s;
- print "# Got: $x\n"
- }
- else {
- print "# Got unexpected success\n";
- }
- if ($_[0]) {
- print "# Expected: $_[0]\n";
- }
- else {
- print "# Expected success\n";
- }
- $failed = 1;
- print "not ";
- }
- elsif (@_ == 3 && $_[1] ne $_[2]) {
- print "# Got: $_[1]\n";
- print "# Expected: $_[2]\n";
- $failed = 1;
- print "not ";
- }
- print "ok ",++$test,"\n";
+sub eval_ok ($;$) {
+ eval shift;
+ is( $@, '', @_);
}
-eval 'sub t1 ($) : locked { $_[0]++ }';
-mytest;
-BEGIN {++$ntests}
-
-eval 'sub t2 : locked { $_[0]++ }';
-mytest;
-BEGIN {++$ntests}
-
-eval 'sub t3 ($) : locked ;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'sub t4 : locked ;';
-mytest;
-BEGIN {++$ntests}
-
-my $anon1;
-eval '$anon1 = sub ($) : locked:method { $_[0]++ }';
-mytest;
-BEGIN {++$ntests}
-
-my $anon2;
-eval '$anon2 = sub : locked : method { $_[0]++ }';
-mytest;
-BEGIN {++$ntests}
-
-my $anon3;
-eval '$anon3 = sub : method { $_[0]->[1] }';
-mytest;
-BEGIN {++$ntests}
+eval_ok 'sub t1 ($) : locked { $_[0]++ }';
+eval_ok 'sub t2 : locked { $_[0]++ }';
+eval_ok 'sub t3 ($) : locked ;';
+eval_ok 'sub t4 : locked ;';
+our $anon1; eval_ok '$anon1 = sub ($) : locked:method { $_[0]++ }';
+our $anon2; eval_ok '$anon2 = sub : locked : method { $_[0]++ }';
+our $anon3; eval_ok '$anon3 = sub : method { $_[0]->[1] }';
eval 'sub e1 ($) : plugh ;';
-mytest qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
-BEGIN {++$ntests}
+like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
-mytest qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
-BEGIN {++$ntests}
+like $@, qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
-mytest qr/Unterminated attribute parameter in attribute list at/;
-BEGIN {++$ntests}
+like $@, qr/Unterminated attribute parameter in attribute list at/;
eval 'sub e4 ($) : plugh + xyzzy ;';
-mytest qr/Invalid separator character '[+]' in attribute list at/;
-BEGIN {++$ntests}
-
-eval 'my main $x : = 0;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'my $x : = 0;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'my $x ;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'my ($x) : = 0;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'my ($x) ;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'my ($x) : ;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'my ($x,$y) : = 0;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'my ($x,$y) ;';
-mytest;
-BEGIN {++$ntests}
-
-eval 'my ($x,$y) : ;';
-mytest;
-BEGIN {++$ntests}
+like $@, qr/Invalid separator character '[+]' in attribute list at/;
+
+eval_ok 'my main $x : = 0;';
+eval_ok 'my $x : = 0;';
+eval_ok 'my $x ;';
+eval_ok 'my ($x) : = 0;';
+eval_ok 'my ($x) ;';
+eval_ok 'my ($x) : ;';
+eval_ok 'my ($x,$y) : = 0;';
+eval_ok 'my ($x,$y) ;';
+eval_ok 'my ($x,$y) : ;';
eval 'my ($x,$y) : plugh;';
-mytest qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
-BEGIN {++$ntests}
+like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
+
+# bug #16080
+eval '{my $x : plugh}';
+like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
+eval '{my ($x,$y) : plugh(})}';
+like $@, qr/^Invalid SCALAR attribute: ["']?plugh\(}\)["']? at/;
+
+# More syntax tests from the attributes manpage
+eval 'my $x : switch(10,foo(7,3)) : expensive;';
+like $@, qr/^Invalid SCALAR attributes: ["']?switch\(10,foo\(7,3\)\) : expensive["']? at/;
+eval q/my $x : Ugly('\(") :Bad;/;
+like $@, qr/^Invalid SCALAR attributes: ["']?Ugly\('\\\("\) : Bad["']? at/;
+eval 'my $x : _5x5;';
+like $@, qr/^Invalid SCALAR attribute: ["']?_5x5["']? at/;
+eval 'my $x : locked method;';
+like $@, qr/^Invalid SCALAR attributes: ["']?locked : method["']? at/;
+eval 'my $x : switch(10,foo();';
+like $@, qr/^Unterminated attribute parameter in attribute list at/;
+eval q/my $x : Ugly('(');/;
+like $@, qr/^Unterminated attribute parameter in attribute list at/;
+eval 'my $x : 5x5;';
+like $@, qr/error/;
+eval 'my $x : Y2::north;';
+like $@, qr/Invalid separator character ':' in attribute list at/;
sub A::MODIFY_SCALAR_ATTRIBUTES { return }
eval 'my A $x : plugh;';
-mytest qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
-BEGIN {++$ntests}
+like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
eval 'my A $x : plugh plover;';
-mytest qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
-BEGIN {++$ntests}
+like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
+
+no warnings 'reserved';
+eval 'my A $x : plugh;';
+is $@, '';
+
+eval 'package Cat; my Cat @socks;';
+like $@, qr/^Can't declare class for non-scalar \@socks in "my"/;
sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
sub X::foo { 1 }
*Y::bar = \&X::foo;
*Y::bar = \&X::foo; # second time for -w
-eval 'package Z; sub Y::bar : locked';
-mytest qr/^X at /;
-BEGIN {++$ntests}
+eval 'package Z; sub Y::bar : foo';
+like $@, qr/^X at /;
-my @attrs = eval 'attributes::get \&Y::bar';
-mytest '', "@attrs", "locked";
-BEGIN {++$ntests}
+eval 'package Z; sub Y::baz : locked {}';
+my @attrs = eval 'attributes::get \&Y::baz';
+is "@attrs", "locked";
@attrs = eval 'attributes::get $anon1';
-mytest '', "@attrs", "locked method";
-BEGIN {++$ntests}
+is "@attrs", "locked method";
sub Z::DESTROY { }
sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
my $thunk = eval 'bless +sub : method locked { 1 }, "Z"';
-mytest '', ref($thunk), "Z";
-BEGIN {++$ntests}
+is ref($thunk), "Z";
@attrs = eval 'attributes::get $thunk';
-mytest '', "@attrs", "locked method Z";
-BEGIN {++$ntests}
-
-
-# Other tests should be added above this line
-
-sub NTESTS () { $ntests }
+is "@attrs", "locked method Z";
+
+# Test attributes on predeclared subroutines:
+eval 'package A; sub PS : lvalue';
+@attrs = eval 'attributes::get \&A::PS';
+is "@attrs", "lvalue";
+
+# Test ability to modify existing sub's (or XSUB's) attributes.
+eval 'package A; sub X { $_[0] } sub X : lvalue';
+@attrs = eval 'attributes::get \&A::X';
+is "@attrs", "lvalue";
+
+# Above not with just 'pure' built-in attributes.
+sub Z::MODIFY_CODE_ATTRIBUTES { (); }
+eval 'package Z; sub L { $_[0] } sub L : Z lvalue';
+@attrs = eval 'attributes::get \&Z::L';
+is "@attrs", "lvalue Z";
+
+# Begin testing attributes that tie
+
+{
+ package Ttie;
+ sub DESTROY {}
+ sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
+ sub FETCH { ${$_[0]} }
+ sub STORE {
+ ::pass;
+ ${$_[0]} = $_[1]*2;
+ }
+ package Tloop;
+ sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
+}
-exit $failed;
+eval_ok '
+ package Tloop;
+ for my $i (0..2) {
+ my $x : TieLoop = $i;
+ $x != $i*2 and ::is $x, $i*2;
+ }
+';
+
+# bug #15898
+eval 'our ${""} : foo = 1';
+like $@, qr/Can't declare scalar dereference in "our"/;
+eval 'my $$foo : bar = 1';
+like $@, qr/Can't declare scalar dereference in "my"/;
+
+
+my @code = qw(lvalue locked method);
+my @other = qw(shared unique);
+my %valid;
+$valid{CODE} = {map {$_ => 1} @code};
+$valid{SCALAR} = {map {$_ => 1} @other};
+$valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
+
+our ($scalar, @array, %hash);
+foreach my $value (\&foo, \$scalar, \@array, \%hash) {
+ my $type = ref $value;
+ foreach my $negate ('', '-') {
+ foreach my $attr (@code, @other) {
+ my $attribute = $negate . $attr;
+ eval "use attributes __PACKAGE__, \$value, '$attribute'";
+ if ($valid{$type}{$attr}) {
+ if ($attribute eq '-shared') {
+ like $@, qr/^A variable may not be unshared/;
+ } else {
+ is( $@, '', "$type attribute $attribute");
+ }
+ } else {
+ like $@, qr/^Invalid $type attribute: $attribute/,
+ "Bogus $type attribute $attribute should fail";
+ }
+ }
+ }
+}