Small optimisations, by Brandon Black
[p5sagit/p5-mst-13.2.git] / t / op / attrs.t
CommitLineData
9c6390c7 1#!./perl
09bef843 2
3# Regression tests for attributes.pm and the C< : attrs> syntax.
4
9c6390c7 5use warnings;
6
09bef843 7BEGIN {
8 chdir 't' if -d 't';
20822f61 9 @INC = '../lib';
1ce0b88c 10 require './test.pl';
09bef843 11}
12
77c9267e 13plan 'no_plan';
09bef843 14
15$SIG{__WARN__} = sub { die @_ };
16
42262798 17sub eval_ok ($;$) {
18 eval shift;
19 is( $@, '', @_);
09bef843 20}
21
1ce0b88c 22eval_ok 'sub t1 ($) : locked { $_[0]++ }';
23eval_ok 'sub t2 : locked { $_[0]++ }';
24eval_ok 'sub t3 ($) : locked ;';
25eval_ok 'sub t4 : locked ;';
26our $anon1; eval_ok '$anon1 = sub ($) : locked:method { $_[0]++ }';
27our $anon2; eval_ok '$anon2 = sub : locked : method { $_[0]++ }';
28our $anon3; eval_ok '$anon3 = sub : method { $_[0]->[1] }';
09bef843 29
30eval 'sub e1 ($) : plugh ;';
1ce0b88c 31like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
09bef843 32
33eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
1ce0b88c 34like $@, qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
09bef843 35
36eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
1ce0b88c 37like $@, qr/Unterminated attribute parameter in attribute list at/;
09bef843 38
39eval 'sub e4 ($) : plugh + xyzzy ;';
1ce0b88c 40like $@, qr/Invalid separator character '[+]' in attribute list at/;
41
42eval_ok 'my main $x : = 0;';
43eval_ok 'my $x : = 0;';
44eval_ok 'my $x ;';
45eval_ok 'my ($x) : = 0;';
46eval_ok 'my ($x) ;';
47eval_ok 'my ($x) : ;';
48eval_ok 'my ($x,$y) : = 0;';
49eval_ok 'my ($x,$y) ;';
50eval_ok 'my ($x,$y) : ;';
09bef843 51
52eval 'my ($x,$y) : plugh;';
1ce0b88c 53like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
09bef843 54
8e7ae056 55# bug #16080
56eval '{my $x : plugh}';
57like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
58eval '{my ($x,$y) : plugh(})}';
59like $@, qr/^Invalid SCALAR attribute: ["']?plugh\(}\)["']? at/;
60
c9124e92 61# More syntax tests from the attributes manpage
62eval 'my $x : switch(10,foo(7,3)) : expensive;';
63like $@, qr/^Invalid SCALAR attributes: ["']?switch\(10,foo\(7,3\)\) : expensive["']? at/;
64eval q/my $x : Ugly('\(") :Bad;/;
65like $@, qr/^Invalid SCALAR attributes: ["']?Ugly\('\\\("\) : Bad["']? at/;
66eval 'my $x : _5x5;';
67like $@, qr/^Invalid SCALAR attribute: ["']?_5x5["']? at/;
68eval 'my $x : locked method;';
69like $@, qr/^Invalid SCALAR attributes: ["']?locked : method["']? at/;
70eval 'my $x : switch(10,foo();';
71like $@, qr/^Unterminated attribute parameter in attribute list at/;
72eval q/my $x : Ugly('(');/;
73like $@, qr/^Unterminated attribute parameter in attribute list at/;
74eval 'my $x : 5x5;';
75like $@, qr/error/;
76eval 'my $x : Y2::north;';
77like $@, qr/Invalid separator character ':' in attribute list at/;
78
09bef843 79sub A::MODIFY_SCALAR_ATTRIBUTES { return }
80eval 'my A $x : plugh;';
1ce0b88c 81like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
09bef843 82
83eval 'my A $x : plugh plover;';
1ce0b88c 84like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
09bef843 85
9c6390c7 86no warnings 'reserved';
87eval 'my A $x : plugh;';
88is $@, '';
89
3f8f4626 90eval 'package Cat; my Cat @socks;';
1ce0b88c 91like $@, qr/^Can't declare class for non-scalar \@socks in "my"/;
3f8f4626 92
09bef843 93sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
94sub X::foo { 1 }
95*Y::bar = \&X::foo;
96*Y::bar = \&X::foo; # second time for -w
0256094b 97eval 'package Z; sub Y::bar : foo';
1ce0b88c 98like $@, qr/^X at /;
09bef843 99
0256094b 100eval 'package Z; sub Y::baz : locked {}';
101my @attrs = eval 'attributes::get \&Y::baz';
1ce0b88c 102is "@attrs", "locked";
09bef843 103
104@attrs = eval 'attributes::get $anon1';
1ce0b88c 105is "@attrs", "locked method";
09bef843 106
107sub Z::DESTROY { }
108sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
109my $thunk = eval 'bless +sub : method locked { 1 }, "Z"';
1ce0b88c 110is ref($thunk), "Z";
09bef843 111
112@attrs = eval 'attributes::get $thunk';
1ce0b88c 113is "@attrs", "locked method Z";
09bef843 114
61dbb99a 115# Test attributes on predeclared subroutines:
116eval 'package A; sub PS : lvalue';
117@attrs = eval 'attributes::get \&A::PS';
118is "@attrs", "lvalue";
119
d3cea301 120# Test ability to modify existing sub's (or XSUB's) attributes.
121eval 'package A; sub X { $_[0] } sub X : lvalue';
122@attrs = eval 'attributes::get \&A::X';
1ce0b88c 123is "@attrs", "lvalue";
d3cea301 124
020f0e03 125# Above not with just 'pure' built-in attributes.
126sub Z::MODIFY_CODE_ATTRIBUTES { (); }
127eval 'package Z; sub L { $_[0] } sub L : Z lvalue';
128@attrs = eval 'attributes::get \&Z::L';
1ce0b88c 129is "@attrs", "lvalue Z";
020f0e03 130
95f0a2f1 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 {
1ce0b88c 139 ::pass;
95f0a2f1 140 ${$_[0]} = $_[1]*2;
141 }
142 package Tloop;
143 sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
144}
145
1ce0b88c 146eval_ok '
95f0a2f1 147 package Tloop;
148 for my $i (0..2) {
149 my $x : TieLoop = $i;
1ce0b88c 150 $x != $i*2 and ::is $x, $i*2;
95f0a2f1 151 }
152';
09bef843 153
1ce0b88c 154# bug #15898
155eval 'our ${""} : foo = 1';
fab01b8e 156like $@, qr/Can't declare scalar dereference in "our"/;
1ce0b88c 157eval 'my $$foo : bar = 1';
fab01b8e 158like $@, qr/Can't declare scalar dereference in "my"/;
42262798 159
160
77c9267e 161my @code = qw(lvalue locked method);
162unshift @code, 'assertion' if $] >= 5.009;
42262798 163my @other = qw(shared unique);
164my %valid;
165$valid{CODE} = {map {$_ => 1} @code};
166$valid{SCALAR} = {map {$_ => 1} @other};
167$valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
168
adb2fcba 169our ($scalar, @array, %hash);
42262798 170foreach 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}