File: 30_return_value.t

package info (click to toggle)
libaspect-perl 1.04-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 720 kB
  • sloc: perl: 6,846; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 862 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl

# This test validates which usages of return_value are allowed when setting the
# return value, and which are not.

use strict;
BEGIN {
	$|  = 1;
	$^W = 1;
}

use Test::More tests => 5;
use Test::NoWarnings;
use Aspect;

my $CALLED = 0;

SCOPE: {
	package Foo;

	sub foo {
		$CALLED++;
		return 2;
	}

	sub bar {
		$CALLED++;
		return 3;
	}

	sub baz {
		$CALLED++;
		return 4;
	}

	1;
}

# Set the return value and don't run the function
before {
	$_->return_value(10);
	return;
} call 'Foo::foo';

is( Foo::foo(), 10, 'Foo::foo() returns hijacked return value' );
is( $CALLED, 0, 'Original function was not called' );

# Set the return value in an explicit return
before {
	return $_->return_value(20);
} call 'Foo::bar';

is( Foo::bar(), 20, 'Foo::bar() returns hijacked return value' );
is( $CALLED, 0, 'Original function was not called' );