3 # This test file contains 57 tests.
4 # You need to number them manually. Don't forget to update this line for the
7 END {print "not ok 1\n" unless $loaded;}
9 use Attribute::Handlers;
12 CHECK { $main::phase++ }
14 ######################### End of black magic.
16 # Insert your test code below (better if it prints "ok 13"
17 # (correspondingly "not ok 13") depending on the success of chunk 13
20 sub ok { $::count++; push @::results, [$_[1], $_[0]?"":"not ", defined($_[2])?$_[2]:""]; }
22 END { print "1..$::count\n";
23 print map "$_->[1]ok $_->[0] $_->[2]\n",
24 sort {$a->[0]<=>$b->[0]}
25 grep $_->[0], @::results }
29 no warnings 'redefine';
31 sub UNIVERSAL::Lastly :ATTR(INIT) { ::ok $_[4][0] && $main::phase, $_[4][1] }
33 sub UNIVERSAL::Okay :ATTR(BEGIN) {
34 ::ok $_[4][0] && (!$main::phase || !ref $_[1] && $_[1] eq 'LEXICAL'), $_[4][1];
37 sub Dokay :ATTR(SCALAR) { ::ok @{$_[4]} }
38 sub Dokay :ATTR(HASH) { ::ok @{$_[4]} }
39 sub Dokay :ATTR(ARRAY) { ::ok @{$_[4]} }
40 sub Dokay :ATTR(CODE) { ::ok @{$_[4]} }
42 sub Vokay :ATTR(VAR) { ::ok @{$_[4]} }
44 sub Aokay :ATTR(ANY) { ::ok @{$_[4]} }
50 my @x1 :Lastly(1=>42);
52 sub x1 :Lastly(1,44) {}
54 my Test $x2 :Dokay(1,5);
57 ::ok(1, $_, '# skip : invalid before 5.11') for 55 .. 57;
61 my Test @x2 :Dokay(1,55);
62 my Test %x2 :Dokay(1,56);
64 $c = $c + 2 - $::count;
74 my Test $x4 :Dokay(1,7);
85 # BEGIN {eval 'sub y2 :Vokay(0,16) {}; 1' or
92 sub z :Aokay(1,20) {};
99 my Test $x6 :Dokay(1,22);
105 sub y3 :Okay(1,27) {}
112 sub x11 :Okay(1,4) {}
114 BEGIN { eval 'my $x7 :Dokay(0,28)' or ::ok(1,28); }
115 my Test $x8 :Dokay(1,29);
116 eval 'sub x7 :Dokay(0,30) {}' or ::ok(1,30);
121 sub TIESCALAR { ::ok(1,31); bless {}, $_[0] }
122 sub FETCH { ::ok(1,32); return 1 }
123 sub STORE { ::ok(1,33); return 1 }
127 sub TIEARRAY { ::ok(1,$_[1]); bless {}, $_[0] }
128 sub FETCH { ::ok(1,35); return 1 }
129 sub STORE { ::ok(1,36); return 1 }
130 sub FETCHSIZE { 100 }
132 package Tie::Row::dy;
134 sub TIEHASH { ::ok(1,$_[1]); bless {}, $_[0] }
135 sub FETCH { ::ok(1,38); return 1 }
136 sub STORE { ::ok(1,39); return 1 }
140 eval 'sub x7 :ATTR(SCALAR) :ATTR(CODE) {}' and ::ok(0,40) or ::ok(1,40);
142 use Attribute::Handlers autotie => { Other::Loud => Tie::Loud,
144 UNIVERSAL::Rowdy => Tie::Row::dy,
147 my Other $loud : Loud;
150 my @noisy : Noisy(34);
153 my %rowdy : Rowdy(37,'this arg should be ignored');
157 # check that applying attributes to lexicals doesn't unduly worry
161 sub UNIVERSAL::Dummy :ATTR { ++$applied };
162 sub Dummy::DESTROY { $out .= "bye\n" }
164 { my $dummy; $dummy = bless {}, 'Dummy'; }
165 ok( $out eq "begin\nbye\n", 45 );
167 { my $dummy : Dummy; $dummy = bless {}, 'Dummy'; }
169 ok( 1, 46, " # skip lexicals are not runtime prior to 5.8");
171 ok( $out eq "begin\nbye\nbye\n", 46);
173 # are lexical attributes reapplied correctly?
174 sub dummy { my $dummy : Dummy; }
178 ok(1, 47, " # skip does not work with perl prior to 5.8");
180 ok( $applied == 2, 47 );
182 # 45-47 again, but for our variables
184 { our $dummy; $dummy = bless {}, 'Dummy'; }
185 ok( $out eq "begin\n", 48 );
186 { no warnings; our $dummy : Dummy; $dummy = bless {}, 'Dummy'; }
187 ok( $out eq "begin\nbye\n", 49 );
189 ok( $out eq "begin\nbye\nbye\n", 50 );
191 # are lexical attributes reapplied correctly?
192 sub dummy_our { no warnings; our $banjo : Dummy; }
194 dummy_our(); dummy_our();
195 ok( $applied == 0, 51 );
197 sub UNIVERSAL::Stooge :ATTR(END) {};
199 local $SIG{__WARN__} = sub { die @_ };
200 my $groucho : Stooge;
202 my $match = $@ =~ /^Won't be able to apply END handler/;
204 ok(1,52 ,"# Skip, no difference between lexical handlers and normal handlers prior to 5.8");
210 # The next two check for the phase invariance that Marcel spotted.
211 # Subject: Attribute::Handlers phase variance
212 # Message-Id: <54EDDB80-FD75-11D6-A18D-00039379E28A@noug.at>
214 my ($code_applied, $scalar_applied);
215 sub Scotty :ATTR(CODE,BEGIN) { $code_applied = $_[5] }
217 no warnings 'redefine';
218 sub Scotty :ATTR(SCALAR,CHECK) { $scalar_applied = $_[5] }
221 sub warp_coil :Scotty {}
222 my $photon_torpedo :Scotty;
224 ok( $code_applied eq 'BEGIN', 53, "# phase variance" );
225 ok( $scalar_applied eq 'CHECK', 54 );