Aspect
view release on metacpan or search on metacpan
t/61_legacy_after_throwing.t view on Meta::CPAN
eval 'main::with_proto(1, 2)';
like( $@, qr/Too many arguments/, 'prototypes are obeyed' );
}
# Confirm correct parameter error during hooking
SCOPE: {
my $advice = Aspect::after_throwing {
$_->return_value('wrapped');
} call 'main::with_proto';
is( main::with_proto('foo'), 'wrapped', 'With prototype' );
local $@;
eval 'main::with_proto(1, 2)';
like( $@, qr/Too many arguments/, 'prototypes are obeyed' );
}
# Confirm correct parameter error after hooking
SCOPE: {
local $@;
eval 'main::with_proto(1, 2)';
like( $@, qr/Too many arguments/, 'prototypes are obeyed' );
}
######################################################################
# Caller Correctness
my @CALLER = ();
my $AFTER = 0;
SCOPE: {
# Set up the Aspect
my $aspect = Aspect::after_throwing { $AFTER++ } call 'My::Three::bar';
isa_ok( $aspect, 'Aspect::Advice' );
isa_ok( $aspect, 'Aspect::Advice::After' );
is( $AFTER, 0, '$AFTER is false' );
is( scalar(@CALLER), 0, '@CALLER is empty' );
# Call a method above the wrapped method
throws_ok( sub { My::Two->foo }, qr/value/, '->foo is ok' );
is( $AFTER, 1, '$AFTER is true' );
is( scalar(@CALLER), 2, '@CALLER is full' );
is( $CALLER[0]->[0], 'My::Two', 'First caller is My::Two' );
is( $CALLER[1]->[0], 'main', 'Second caller is main' );
}
SCOPE: {
package My::Two;
sub foo {
My::Three->bar;
}
package My::Three;
sub bar {
@CALLER = (
[ caller(0) ],
[ caller(1) ],
);
die 'value';
}
}
######################################################################
# Wantarray Support
my @CONTEXT = ();
# Before the aspects
SCOPE: {
throws_ok(
sub { () = Foo->after_throwing },
qr/bang/,
'after_throwing before throws ok',
);
throws_ok(
sub { my $dummy = Foo->after_throwing },
qr/bang/,
'after_throwing before throws ok',
);
throws_ok(
sub { Foo->after_throwing },
qr/bang/,
'after_throwing before throws ok',
);
}
SCOPE: {
my $aspect = Aspect::after_throwing {
if ( $_[0]->wantarray ) {
push @CONTEXT, 'ARRAY';
} elsif ( defined $_[0]->wantarray ) {
push @CONTEXT, 'SCALAR';
} else {
push @CONTEXT, 'VOID';
}
if ( wantarray ) {
push @CONTEXT, 'ARRAY';
} elsif ( defined wantarray ) {
push @CONTEXT, 'SCALAR';
} else {
push @CONTEXT, 'VOID';
}
} call 'Foo::after_throwing';
# During the aspects
throws_ok(
sub { () = Foo->after_throwing },
qr/bang/,
'after_throwing before throws ok',
);
throws_ok(
sub { my $dummy = Foo->after_throwing },
qr/bang/,
( run in 1.288 second using v1.01-cache-2.11-cpan-364913b4093 )