Aspect

 view release on metacpan or  search on metacpan

t/34_point_functions.t  view on Meta::CPAN

# Confirm correct parameter error during hooking
SCOPE: {
	my $advice = around {
		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 $AROUND = 0;

SCOPE: {
	# Set up the Aspect
	my $aspect = around {
		$AROUND++;
		proceed;
	} call 'My::Three::bar';
	isa_ok( $aspect, 'Aspect::Advice' );
	isa_ok( $aspect, 'Aspect::Advice::Around' );
	is( $AROUND,         0, '$AROUND is false' );
	is( scalar(@CALLER), 0, '@CALLER is empty' );

	# Call a method above the wrapped method
	my $rv = My::Two->foo;
	is( $rv, 'value', '->foo is ok' );
	is( $AROUND,         1, '$AROUND 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) ],
		);
		return 'value';
	}
}





######################################################################
# Wantarray Support

my @CONTEXT = ();

# Before the aspects
SCOPE: {
	() = Foo->around;
	my $dummy = Foo->around;
	Foo->around;
}

SCOPE: {
	my $aspect = around {
		if ( wantarray ) {
			push @CONTEXT, 'ARRAY';
		} elsif ( defined wantarray ) {
			push @CONTEXT, 'SCALAR';
		} else {
			push @CONTEXT, 'VOID';
		}
		if ( CORE::wantarray ) {
			push @CONTEXT, 'ARRAY';
		} elsif ( defined CORE::wantarray ) {
			push @CONTEXT, 'SCALAR';
		} else {
			push @CONTEXT, 'VOID';
		}
		proceed;
	} call 'Foo::around';

	# During the aspects
	() = Foo->around;
	my $dummy = Foo->around;
	Foo->around;
}

# After the aspects
SCOPE: {
	() = Foo->around;
	my $dummy = Foo->around;
	Foo->around;
}

# Check the results in aggregate
is_deeply(
	\@CONTEXT,
	[ qw{
		array
		scalar
		void



( run in 2.782 seconds using v1.01-cache-2.11-cpan-98e64b0badf )