File: 51_legacy_wantarray.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 (143 lines) | stat: -rw-r--r-- 3,045 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
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
#!/usr/bin/perl

# Testing of the three wantarray-related pointcuts.
# Because each advice type generates different code,
# it's important to test with each different variation.

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

use Test::More tests => 16;
use Test::NoWarnings;
use Test::Exception;
use Aspect ':deprecated';

use vars qw{$COUNT};





######################################################################
# Wantarray with after_returning() advice

$COUNT = 0;

Aspect::after_returning { $COUNT += 1   } call 'Foo::after_returning_one'   & wantlist;
Aspect::after_returning { $COUNT += 10  } call 'Foo::after_returning_two'   & wantscalar;
Aspect::after_returning { $COUNT += 100 } call 'Foo::after_returning_three' & wantvoid;

SCOPE: {
	my @l = Foo::after_returning_one();
	my $s = Foo::after_returning_one();
	Foo::after_returning_one();
}
is( $COUNT, 1, 'Matched wantlist' );

SCOPE: {
	my @l = Foo::after_returning_two();
	my $s = Foo::after_returning_two();
	Foo::after_returning_two();
}
is( $COUNT, 11, 'Matched wantscalar' );

SCOPE: {
	my @l = Foo::after_returning_three();
	my $s = Foo::after_returning_three();
	Foo::after_returning_three();
}
is( $COUNT, 111, 'Matched wantvoid' );





######################################################################
# Wantarray with after_throwing() advice

$COUNT = 0;

Aspect::after_throwing { $COUNT += 1   } call 'Foo::after_throwing_one'   & wantlist;
Aspect::after_throwing { $COUNT += 10  } call 'Foo::after_throwing_two'   & wantscalar;
Aspect::after_throwing { $COUNT += 100 } call 'Foo::after_throwing_three' & wantvoid;

SCOPE: {
	throws_ok(
		sub { my @l = Foo::after_throwing_one(); },
		qr/one/,
		'after_throwing wantarray array'
	);
	throws_ok(
		sub { my $s = Foo::after_throwing_one(); },
		qr/one/,
		'after_throwing wantarray scalar'
	);
	throws_ok(
		sub { Foo::after_throwing_one(); },
		qr/one/,
		'after_throwing wantarray void'
	);
}
is( $COUNT, 1, 'Matched wantlist' );

SCOPE: {
	throws_ok(
		sub { my @l = Foo::after_throwing_two(); },
		qr/two/,
		'after_throwing wantscalar array'
	);
	throws_ok(
		sub { my $s = Foo::after_throwing_two(); },
		qr/two/,
		'after_throwing wantscalar scalar'
	);
	throws_ok(
		sub { Foo::after_throwing_two(); },
		qr/two/,
		'after_throwing wantscalar void'
	);
}
is( $COUNT, 11, 'Matched wantscalar' );

SCOPE: {
	throws_ok(
		sub { my @l = Foo::after_throwing_three(); },
		qr/three/,
		'after_throwing wantvoid array'
	);
	throws_ok(
		sub { my $s = Foo::after_throwing_three(); },
		qr/three/,
		'after_throwing wantvoid scalar'
	);
	throws_ok(
		sub { Foo::after_throwing_three(); },
		qr/three/,
		'after_throwing wantvoid void'
	);
}
is( $COUNT, 111, 'Matched wantvoid' );





######################################################################
# Support Methods

package Foo;

sub after_returning_one   { 1 }

sub after_returning_two   { 2 }

sub after_returning_three { 3 }

sub after_throwing_one    { die 'one'   }

sub after_throwing_two    { die 'two'   }

sub after_throwing_three  { die 'three' }