This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Teach t/TEST about Math-BigInt in dist/
[perl5.git] / t / op / attrs.t
CommitLineData
9c6390c7 1#!./perl
09bef843
SB
2
3# Regression tests for attributes.pm and the C< : attrs> syntax.
4
5BEGIN {
98d0ccc7
RGS
6 if ($ENV{PERL_CORE_MINITEST}) {
7 print "1..0 # skip: miniperl can't load attributes\n";
8 exit 0;
9 }
09bef843 10 chdir 't' if -d 't';
20822f61 11 @INC = '../lib';
1ce0b88c 12 require './test.pl';
09bef843
SB
13}
14
98d0ccc7
RGS
15use warnings;
16
eac910c8 17plan 92;
09bef843
SB
18
19$SIG{__WARN__} = sub { die @_ };
20
42262798
NC
21sub eval_ok ($;$) {
22 eval shift;
23 is( $@, '', @_);
09bef843
SB
24}
25
8e5dadda 26our $anon1; eval_ok '$anon1 = sub : method { $_[0]++ }';
09bef843
SB
27
28eval 'sub e1 ($) : plugh ;';
1ce0b88c 29like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
09bef843
SB
30
31eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
1ce0b88c 32like $@, qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
09bef843
SB
33
34eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
1ce0b88c 35like $@, qr/Unterminated attribute parameter in attribute list at/;
09bef843
SB
36
37eval 'sub e4 ($) : plugh + xyzzy ;';
1ce0b88c
RGS
38like $@, qr/Invalid separator character '[+]' in attribute list at/;
39
40eval_ok 'my main $x : = 0;';
41eval_ok 'my $x : = 0;';
42eval_ok 'my $x ;';
43eval_ok 'my ($x) : = 0;';
44eval_ok 'my ($x) ;';
45eval_ok 'my ($x) : ;';
46eval_ok 'my ($x,$y) : = 0;';
47eval_ok 'my ($x,$y) ;';
48eval_ok 'my ($x,$y) : ;';
09bef843
SB
49
50eval 'my ($x,$y) : plugh;';
1ce0b88c 51like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
09bef843 52
8e7ae056
RGS
53# bug #16080
54eval '{my $x : plugh}';
55like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
56eval '{my ($x,$y) : plugh(})}';
57like $@, qr/^Invalid SCALAR attribute: ["']?plugh\(}\)["']? at/;
58
c9124e92
RGS
59# More syntax tests from the attributes manpage
60eval 'my $x : switch(10,foo(7,3)) : expensive;';
61like $@, qr/^Invalid SCALAR attributes: ["']?switch\(10,foo\(7,3\)\) : expensive["']? at/;
62eval q/my $x : Ugly('\(") :Bad;/;
63like $@, qr/^Invalid SCALAR attributes: ["']?Ugly\('\\\("\) : Bad["']? at/;
64eval 'my $x : _5x5;';
65like $@, qr/^Invalid SCALAR attribute: ["']?_5x5["']? at/;
66eval 'my $x : locked method;';
67like $@, qr/^Invalid SCALAR attributes: ["']?locked : method["']? at/;
68eval 'my $x : switch(10,foo();';
69like $@, qr/^Unterminated attribute parameter in attribute list at/;
70eval q/my $x : Ugly('(');/;
71like $@, qr/^Unterminated attribute parameter in attribute list at/;
72eval 'my $x : 5x5;';
73like $@, qr/error/;
74eval 'my $x : Y2::north;';
75like $@, qr/Invalid separator character ':' in attribute list at/;
76
09bef843
SB
77sub A::MODIFY_SCALAR_ATTRIBUTES { return }
78eval 'my A $x : plugh;';
1ce0b88c 79like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
09bef843
SB
80
81eval 'my A $x : plugh plover;';
1ce0b88c 82like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
09bef843 83
9c6390c7
RGS
84no warnings 'reserved';
85eval 'my A $x : plugh;';
86is $@, '';
87
3f8f4626 88eval 'package Cat; my Cat @socks;';
d5e98372
VP
89like $@, '';
90
91eval 'my Cat %nap;';
92like $@, '';
3f8f4626 93
09bef843
SB
94sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
95sub X::foo { 1 }
96*Y::bar = \&X::foo;
97*Y::bar = \&X::foo; # second time for -w
0256094b 98eval 'package Z; sub Y::bar : foo';
1ce0b88c 99like $@, qr/^X at /;
09bef843 100
09bef843 101@attrs = eval 'attributes::get $anon1';
8e5dadda 102is "@attrs", "method";
09bef843
SB
103
104sub Z::DESTROY { }
105sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
8e5dadda 106my $thunk = eval 'bless +sub : method { 1 }, "Z"';
1ce0b88c 107is ref($thunk), "Z";
09bef843
SB
108
109@attrs = eval 'attributes::get $thunk';
8e5dadda 110is "@attrs", "method Z";
09bef843 111
61dbb99a
SF
112# Test attributes on predeclared subroutines:
113eval 'package A; sub PS : lvalue';
114@attrs = eval 'attributes::get \&A::PS';
115is "@attrs", "lvalue";
116
eac910c8
GG
117# Test attributes on predeclared subroutines, after definition
118eval 'package A; sub PS : lvalue; sub PS { }';
119@attrs = eval 'attributes::get \&A::PS';
120is "@attrs", "lvalue";
121
d3cea301 122# Test ability to modify existing sub's (or XSUB's) attributes.
885ef6f5 123eval 'package A; sub X { $_[0] } sub X : method';
d3cea301 124@attrs = eval 'attributes::get \&A::X';
885ef6f5 125is "@attrs", "method";
d3cea301 126
020f0e03
SB
127# Above not with just 'pure' built-in attributes.
128sub Z::MODIFY_CODE_ATTRIBUTES { (); }
885ef6f5 129eval 'package Z; sub L { $_[0] } sub L : Z method';
020f0e03 130@attrs = eval 'attributes::get \&Z::L';
885ef6f5 131is "@attrs", "method Z";
020f0e03 132
95f0a2f1
SB
133# Begin testing attributes that tie
134
135{
136 package Ttie;
137 sub DESTROY {}
138 sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
139 sub FETCH { ${$_[0]} }
140 sub STORE {
1ce0b88c 141 ::pass;
95f0a2f1
SB
142 ${$_[0]} = $_[1]*2;
143 }
144 package Tloop;
145 sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
146}
147
1ce0b88c 148eval_ok '
95f0a2f1
SB
149 package Tloop;
150 for my $i (0..2) {
151 my $x : TieLoop = $i;
1ce0b88c 152 $x != $i*2 and ::is $x, $i*2;
95f0a2f1
SB
153 }
154';
09bef843 155
1ce0b88c
RGS
156# bug #15898
157eval 'our ${""} : foo = 1';
fab01b8e 158like $@, qr/Can't declare scalar dereference in "our"/;
1ce0b88c 159eval 'my $$foo : bar = 1';
fab01b8e 160like $@, qr/Can't declare scalar dereference in "my"/;
42262798
NC
161
162
c32124fe 163my @code = qw(lvalue method);
f1a3ce43
NC
164my @other = qw(shared);
165my @deprecated = qw(locked unique);
42262798
NC
166my %valid;
167$valid{CODE} = {map {$_ => 1} @code};
168$valid{SCALAR} = {map {$_ => 1} @other};
169$valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
c32124fe
NC
170my %deprecated;
171$deprecated{CODE} = { locked => 1 };
f1a3ce43 172$deprecated{ARRAY} = $deprecated{HASH} = $deprecated{SCALAR} = { unique => 1 };
42262798 173
adb2fcba 174our ($scalar, @array, %hash);
42262798
NC
175foreach my $value (\&foo, \$scalar, \@array, \%hash) {
176 my $type = ref $value;
177 foreach my $negate ('', '-') {
c32124fe 178 foreach my $attr (@code, @other, @deprecated) {
42262798
NC
179 my $attribute = $negate . $attr;
180 eval "use attributes __PACKAGE__, \$value, '$attribute'";
c32124fe
NC
181 if ($deprecated{$type}{$attr}) {
182 like $@, qr/^Attribute "$attr" is deprecated at \(eval \d+\)/,
183 "$type attribute $attribute deprecated";
184 } elsif ($valid{$type}{$attr}) {
42262798
NC
185 if ($attribute eq '-shared') {
186 like $@, qr/^A variable may not be unshared/;
187 } else {
188 is( $@, '', "$type attribute $attribute");
189 }
190 } else {
191 like $@, qr/^Invalid $type attribute: $attribute/,
192 "Bogus $type attribute $attribute should fail";
193 }
194 }
195 }
196}
6e592b3a
BM
197
198# this will segfault if it fails
199sub PVBM () { 'foo' }
200{ my $dummy = index 'foo', PVBM }
201
202ok !defined(attributes::get(\PVBM)),
203 'PVBMs don\'t segfault attributes::get';
09330df8 204
8314a0a6
NC
205{
206 # [perl #49472] Attributes + Unkown Error
207 eval '
208 use strict;
209 sub MODIFY_CODE_ATTRIBUTE{}
210 sub f:Blah {$nosuchvar};
211 ';
212
213 my $err = $@;
214 like ($err, qr/Global symbol "\$nosuchvar" requires /, 'perl #49472');
215}
216
09330df8
Z
217# Test that code attributes always get applied to the same CV that
218# we're left with at the end (bug#66970).
219{
220 package bug66970;
221 our $c;
222 sub MODIFY_CODE_ATTRIBUTES { $c = $_[1]; () }
223 $c=undef; eval 'sub t0 :Foo';
224 main::ok $c == \&{"t0"};
225 $c=undef; eval 'sub t1 :Foo { }';
226 main::ok $c == \&{"t1"};
227 $c=undef; eval 'sub t2';
228 our $t2a = \&{"t2"};
229 $c=undef; eval 'sub t2 :Foo';
230 main::ok $c == \&{"t2"} && $c == $t2a;
231 $c=undef; eval 'sub t3';
232 our $t3a = \&{"t3"};
233 $c=undef; eval 'sub t3 :Foo { }';
234 main::ok $c == \&{"t3"} && $c == $t3a;
235 $c=undef; eval 'sub t4 :Foo';
236 our $t4a = \&{"t4"};
237 our $t4b = $c;
238 $c=undef; eval 'sub t4 :Foo';
239 main::ok $c == \&{"t4"} && $c == $t4b && $c == $t4a;
240 $c=undef; eval 'sub t5 :Foo';
241 our $t5a = \&{"t5"};
242 our $t5b = $c;
243 $c=undef; eval 'sub t5 :Foo { }';
244 main::ok $c == \&{"t5"} && $c == $t5b && $c == $t5a;
245}