Small perlivp.PL updates
[p5sagit/p5-mst-13.2.git] / t / op / attrs.t
1 #!./perl
2
3 # Regression tests for attributes.pm and the C< : attrs> syntax.
4
5 use warnings;
6
7 BEGIN {
8     chdir 't' if -d 't';
9     @INC = '../lib';
10     require './test.pl';
11 }
12
13 plan 'no_plan';
14
15 $SIG{__WARN__} = sub { die @_ };
16
17 sub eval_ok ($;$) {
18     eval shift;
19     is( $@, '', @_);
20 }
21
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] }';
29
30 eval 'sub e1 ($) : plugh ;';
31 like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
32
33 eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
34 like $@, qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
35
36 eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
37 like $@, qr/Unterminated attribute parameter in attribute list at/;
38
39 eval 'sub e4 ($) : plugh + xyzzy ;';
40 like $@, qr/Invalid separator character '[+]' in attribute list at/;
41
42 eval_ok 'my main $x : = 0;';
43 eval_ok 'my $x : = 0;';
44 eval_ok 'my $x ;';
45 eval_ok 'my ($x) : = 0;';
46 eval_ok 'my ($x) ;';
47 eval_ok 'my ($x) : ;';
48 eval_ok 'my ($x,$y) : = 0;';
49 eval_ok 'my ($x,$y) ;';
50 eval_ok 'my ($x,$y) : ;';
51
52 eval 'my ($x,$y) : plugh;';
53 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
54
55 # bug #16080
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/;
60
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/;
66 eval 'my $x : _5x5;';
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/;
74 eval 'my $x : 5x5;';
75 like $@, qr/error/;
76 eval 'my $x : Y2::north;';
77 like $@, qr/Invalid separator character ':' in attribute list at/;
78
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/;
82
83 eval 'my A $x : plugh plover;';
84 like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
85
86 no warnings 'reserved';
87 eval 'my A $x : plugh;';
88 is $@, '';
89
90 eval 'package Cat; my Cat @socks;';
91 like $@, qr/^Can't declare class for non-scalar \@socks in "my"/;
92
93 sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
94 sub X::foo { 1 }
95 *Y::bar = \&X::foo;
96 *Y::bar = \&X::foo;     # second time for -w
97 eval 'package Z; sub Y::bar : foo';
98 like $@, qr/^X at /;
99
100 eval 'package Z; sub Y::baz : locked {}';
101 my @attrs = eval 'attributes::get \&Y::baz';
102 is "@attrs", "locked";
103
104 @attrs = eval 'attributes::get $anon1';
105 is "@attrs", "locked method";
106
107 sub Z::DESTROY { }
108 sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
109 my $thunk = eval 'bless +sub : method locked { 1 }, "Z"';
110 is ref($thunk), "Z";
111
112 @attrs = eval 'attributes::get $thunk';
113 is "@attrs", "locked method Z";
114
115 # Test attributes on predeclared subroutines:
116 eval 'package A; sub PS : lvalue';
117 @attrs = eval 'attributes::get \&A::PS';
118 is "@attrs", "lvalue";
119
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";
124
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";
130
131 # Begin testing attributes that tie
132
133 {
134     package Ttie;
135     sub DESTROY {}
136     sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
137     sub FETCH { ${$_[0]} }
138     sub STORE {
139         ::pass;
140         ${$_[0]} = $_[1]*2;
141     }
142     package Tloop;
143     sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
144 }
145
146 eval_ok '
147     package Tloop;
148     for my $i (0..2) {
149         my $x : TieLoop = $i;
150         $x != $i*2 and ::is $x, $i*2;
151     }
152 ';
153
154 # bug #15898
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"/;
159
160
161 my @code = qw(lvalue locked method);
162 unshift @code, 'assertion' if $] >= 5.009;
163 my @other = qw(shared unique);
164 my %valid;
165 $valid{CODE} = {map {$_ => 1} @code};
166 $valid{SCALAR} = {map {$_ => 1} @other};
167 $valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
168
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/;
179                 } else {
180                     is( $@, '', "$type attribute $attribute");
181                 }
182             } else {
183                 like $@, qr/^Invalid $type attribute: $attribute/,
184                     "Bogus $type attribute $attribute should fail";
185             }
186         }
187     }
188 }