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