Make C<undef ~~ 0> and C<undef ~~ ""> not match (like in 5.10.0)
[p5sagit/p5-mst-13.2.git] / t / op / attrs.t
index 25abeb2..92b5b9e 100644 (file)
@@ -1,29 +1,29 @@
-#!./perl -w
+#!./perl
 
 # Regression tests for attributes.pm and the C< : attrs> syntax.
 
 BEGIN {
+    if ($ENV{PERL_CORE_MINITEST}) {
+       print "1..0 # skip: miniperl can't load attributes\n";
+       exit 0;
+    }
     chdir 't' if -d 't';
     @INC = '../lib';
     require './test.pl';
 }
 
-plan tests => 47;
+use warnings;
+
+plan 84;
 
 $SIG{__WARN__} = sub { die @_ };
 
-sub eval_ok ($) {
-    eval $_[0];
-    is( $@, '' );
+sub eval_ok ($;$) {
+    eval shift;
+    is( $@, '', @_);
 }
 
-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] }';
+our $anon1; eval_ok '$anon1 = sub : method { $_[0]++ }';
 
 eval 'sub e1 ($) : plugh ;';
 like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
@@ -81,8 +81,15 @@ like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?
 eval 'my A $x : plugh plover;';
 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"/;
+like $@, '';
+
+eval 'my Cat %nap;';
+like $@, '';
 
 sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
 sub X::foo { 1 }
@@ -91,20 +98,21 @@ sub X::foo { 1 }
 eval 'package Z; sub Y::bar : foo';
 like $@, qr/^X at /;
 
-eval 'package Z; sub Y::baz : locked {}';
-my @attrs = eval 'attributes::get \&Y::baz';
-is "@attrs", "locked";
-
 @attrs = eval 'attributes::get $anon1';
-is "@attrs", "locked method";
+is "@attrs", "method";
 
 sub Z::DESTROY { }
 sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
-my $thunk = eval 'bless +sub : method locked { 1 }, "Z"';
+my $thunk = eval 'bless +sub : method { 1 }, "Z"';
 is ref($thunk), "Z";
 
 @attrs = eval 'attributes::get $thunk';
-is "@attrs", "locked method Z";
+is "@attrs", "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';
@@ -142,6 +150,49 @@ eval_ok '
 
 # bug #15898
 eval 'our ${""} : foo = 1';
-like $@, qr/Can't declare scalar dereference in our/;
+like $@, qr/Can't declare scalar dereference in "our"/;
 eval 'my $$foo : bar = 1';
-like $@, qr/Can't declare scalar dereference in my/;
+like $@, qr/Can't declare scalar dereference in "my"/;
+
+
+my @code = qw(lvalue method);
+my @other = qw(shared);
+my @deprecated = qw(locked unique);
+my %valid;
+$valid{CODE} = {map {$_ => 1} @code};
+$valid{SCALAR} = {map {$_ => 1} @other};
+$valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
+my %deprecated;
+$deprecated{CODE} = { locked => 1 };
+$deprecated{ARRAY} = $deprecated{HASH} = $deprecated{SCALAR} = { unique => 1 };
+
+our ($scalar, @array, %hash);
+foreach my $value (\&foo, \$scalar, \@array, \%hash) {
+    my $type = ref $value;
+    foreach my $negate ('', '-') {
+       foreach my $attr (@code, @other, @deprecated) {
+           my $attribute = $negate . $attr;
+           eval "use attributes __PACKAGE__, \$value, '$attribute'";
+           if ($deprecated{$type}{$attr}) {
+               like $@, qr/^Attribute "$attr" is deprecated at \(eval \d+\)/,
+                   "$type attribute $attribute deprecated";
+           } elsif ($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";
+           }
+       }
+    }
+}
+
+# this will segfault if it fails
+sub PVBM () { 'foo' }
+{ my $dummy = index 'foo', PVBM }
+
+ok !defined(attributes::get(\PVBM)), 
+    'PVBMs don\'t segfault attributes::get';