1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
#!/usr/bin/perl
# This test checks that caller continues to work as expected in code that has
# been Aspect-hooked.
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use Test::More tests => 52;
use Test::NoWarnings;
use Aspect;
######################################################################
# Check for before advice
SCOPE: {
my @CALLER = ();
my $POINTS = 0;
my $EXPECTED = 0;
# Set up the Aspect
before { $POINTS++ } call 'Bar1::bar'; # Single
before { $POINTS++ } call 'Foo1::two'; # Multiple
before { $POINTS++ } call 'Foo1::three' & wantlist; # Nonmatched hook
is( $POINTS, 0, '$POINTS is false' );
is( scalar(@CALLER), 0, '@CALLER is empty' );
# Call methods above the wrapped method
my @EXPECTED = ( one => 1, two => 3, three => 4 );
while ( @EXPECTED ) {
my $method = shift @EXPECTED;
my $points = shift @EXPECTED;
my $result = Foo1->$method();
is( $result, 'value', "->$method is ok" );
is( $POINTS, $points, '$POINTS is correct' );
is( scalar(@CALLER), 2, '@CALLER is full' );
is( $CALLER[0]->[0], 'Foo1', 'First caller is Foo1' );
is( $CALLER[1]->[0], 'main', 'Second caller is main' );
}
# Test package
package Foo1;
sub one {
Bar1->bar;
}
sub two {
Bar1->bar;
}
sub three {
Bar1->bar;
}
package Bar1;
sub bar {
@CALLER = (
[ caller(0) ],
[ caller(1) ],
);
return 'value';
}
}
######################################################################
# Check for after advice
SCOPE: {
my @CALLER = ();
my $POINTS = 0;
my $EXPECTED = 0;
# Set up the Aspect
after { $POINTS++ } call 'Bar2::bar'; # Single
after { $POINTS++ } call 'Foo2::two'; # Multiple
after { $POINTS++ } call 'Foo2::three' & wantlist; # Nonmatched hook
is( $POINTS, 0, '$POINTS is false' );
is( scalar(@CALLER), 0, '@CALLER is empty' );
# Call methods above the wrapped method
my @EXPECTED = ( one => 1, two => 3, three => 4 );
while ( @EXPECTED ) {
my $method = shift @EXPECTED;
my $points = shift @EXPECTED;
my $result = Foo2->$method();
is( $result, 'value', "->$method is ok" );
is( $POINTS, $points, '$POINTS is correct' );
is( scalar(@CALLER), 2, '@CALLER is full' );
is( $CALLER[0]->[0], 'Foo2', 'First caller is Foo2' );
is( $CALLER[1]->[0], 'main', 'Second caller is main' );
}
# Test package
package Foo2;
sub one {
Bar2->bar;
}
sub two {
Bar2->bar;
}
sub three {
Bar2->bar;
}
package Bar2;
sub bar {
@CALLER = (
[ caller(0) ],
[ caller(1) ],
);
return 'value';
}
}
######################################################################
# Check for around advice
SCOPE: {
my @CALLER = ();
my $POINTS = 0;
my $EXPECTED = 0;
# Set up the Aspect
around { $POINTS++; $_->proceed } call 'Bar3::bar'; # Single
around { $POINTS++; $_->proceed } call 'Foo3::two'; # Multiple
around { $POINTS++; $_->proceed } call 'Foo3::three' & wantlist; # Nonmatched hook
is( $POINTS, 0, '$POINTS is false' );
is( scalar(@CALLER), 0, '@CALLER is empty' );
# Call methods above the wrapped method
my @EXPECTED = ( one => 1, two => 3, three => 4 );
while ( @EXPECTED ) {
my $method = shift @EXPECTED;
my $points = shift @EXPECTED;
my $result = Foo3->$method();
is( $result, 'value', "->$method is ok" );
is( $POINTS, $points, '$POINTS is correct' );
is( scalar(@CALLER), 2, '@CALLER is full' );
is( $CALLER[0]->[0], 'Foo3', 'First caller is Foo3' );
is( $CALLER[1]->[0], 'main', 'Second caller is main' );
}
# Test package
package Foo3;
sub one {
Bar3->bar;
}
sub two {
Bar3->bar;
}
sub three {
Bar3->bar;
}
package Bar3;
sub bar {
@CALLER = (
[ caller(0) ],
[ caller(1) ],
);
return 'value';
}
}
|