This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test case for C<undef %File::Glob::>
[perl5.git] / t / op / attrs.t
CommitLineData
09bef843
SB
1#!./perl -w
2
3# Regression tests for attributes.pm and the C< : attrs> syntax.
4
5BEGIN {
6 chdir 't' if -d 't';
20822f61 7 @INC = '../lib';
09bef843
SB
8}
9
10sub NTESTS () ;
11
12my ($test, $ntests);
13BEGIN {$ntests=0}
14$test=0;
15my $failed = 0;
16
17print "1..".NTESTS."\n";
18
19$SIG{__WARN__} = sub { die @_ };
20
21sub mytest {
22 if (!$@ ne !$_[0] || $_[0] && $@ !~ $_[0]) {
23 if ($@) {
24 my $x = $@;
25 $x =~ s/\n.*\z//s;
26 print "# Got: $x\n"
27 }
28 else {
29 print "# Got unexpected success\n";
30 }
31 if ($_[0]) {
32 print "# Expected: $_[0]\n";
33 }
34 else {
35 print "# Expected success\n";
36 }
37 $failed = 1;
38 print "not ";
39 }
40 elsif (@_ == 3 && $_[1] ne $_[2]) {
41 print "# Got: $_[1]\n";
42 print "# Expected: $_[2]\n";
43 $failed = 1;
44 print "not ";
45 }
46 print "ok ",++$test,"\n";
47}
48
49eval 'sub t1 ($) : locked { $_[0]++ }';
50mytest;
51BEGIN {++$ntests}
52
53eval 'sub t2 : locked { $_[0]++ }';
54mytest;
55BEGIN {++$ntests}
56
57eval 'sub t3 ($) : locked ;';
58mytest;
59BEGIN {++$ntests}
60
61eval 'sub t4 : locked ;';
62mytest;
63BEGIN {++$ntests}
64
65my $anon1;
0120eecf 66eval '$anon1 = sub ($) : locked:method { $_[0]++ }';
09bef843
SB
67mytest;
68BEGIN {++$ntests}
69
70my $anon2;
0120eecf 71eval '$anon2 = sub : locked : method { $_[0]++ }';
09bef843
SB
72mytest;
73BEGIN {++$ntests}
74
75my $anon3;
76eval '$anon3 = sub : method { $_[0]->[1] }';
77mytest;
78BEGIN {++$ntests}
79
80eval 'sub e1 ($) : plugh ;';
81mytest qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
82BEGIN {++$ntests}
83
84eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
85mytest qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
86BEGIN {++$ntests}
87
88eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
89mytest qr/Unterminated attribute parameter in attribute list at/;
90BEGIN {++$ntests}
91
92eval 'sub e4 ($) : plugh + xyzzy ;';
93mytest qr/Invalid separator character '[+]' in attribute list at/;
94BEGIN {++$ntests}
95
96eval 'my main $x : = 0;';
97mytest;
98BEGIN {++$ntests}
99
100eval 'my $x : = 0;';
101mytest;
102BEGIN {++$ntests}
103
104eval 'my $x ;';
105mytest;
106BEGIN {++$ntests}
107
108eval 'my ($x) : = 0;';
109mytest;
110BEGIN {++$ntests}
111
112eval 'my ($x) ;';
113mytest;
114BEGIN {++$ntests}
115
116eval 'my ($x) : ;';
117mytest;
118BEGIN {++$ntests}
119
120eval 'my ($x,$y) : = 0;';
121mytest;
122BEGIN {++$ntests}
123
124eval 'my ($x,$y) ;';
125mytest;
126BEGIN {++$ntests}
127
128eval 'my ($x,$y) : ;';
129mytest;
130BEGIN {++$ntests}
131
132eval 'my ($x,$y) : plugh;';
133mytest qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
134BEGIN {++$ntests}
135
136sub A::MODIFY_SCALAR_ATTRIBUTES { return }
137eval 'my A $x : plugh;';
138mytest qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
139BEGIN {++$ntests}
140
141eval 'my A $x : plugh plover;';
142mytest qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
143BEGIN {++$ntests}
144
3f8f4626
DC
145eval 'package Cat; my Cat @socks;';
146mytest qr/^Can't declare class for non-scalar \@socks in "my"/;
147BEGIN {++$ntests}
148
09bef843
SB
149sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
150sub X::foo { 1 }
151*Y::bar = \&X::foo;
152*Y::bar = \&X::foo; # second time for -w
0256094b 153eval 'package Z; sub Y::bar : foo';
09bef843
SB
154mytest qr/^X at /;
155BEGIN {++$ntests}
156
0256094b
DM
157eval 'package Z; sub Y::baz : locked {}';
158my @attrs = eval 'attributes::get \&Y::baz';
09bef843
SB
159mytest '', "@attrs", "locked";
160BEGIN {++$ntests}
161
162@attrs = eval 'attributes::get $anon1';
163mytest '', "@attrs", "locked method";
164BEGIN {++$ntests}
165
166sub Z::DESTROY { }
167sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
168my $thunk = eval 'bless +sub : method locked { 1 }, "Z"';
169mytest '', ref($thunk), "Z";
170BEGIN {++$ntests}
171
172@attrs = eval 'attributes::get $thunk';
173mytest '', "@attrs", "locked method Z";
174BEGIN {++$ntests}
175
176
177# Other tests should be added above this line
178
179sub NTESTS () { $ntests }
180
181exit $failed;