File: test.t

package info (click to toggle)
libhook-lexwrap-perl 0.24-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 136 kB
  • sloc: perl: 136; makefile: 2
file content (200 lines) | stat: -rw-r--r-- 4,402 bytes parent folder | download | duplicates (2)
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
189
190
191
192
193
194
195
196
197
198
199
200
use Hook::LexWrap;
print "1..54\n";

sub ok   { print "ok $_[0]\n" }
sub fail(&) { print "not " if $_[0]->() }

sub actual { ok $_[0]; }


actual 1;

{ 
	my $lexical = wrap actual,
			pre  => sub { ok 2 },
			post => sub { ok 4 };

	wrap actual, pre => sub { $_[0]++ };

	my $x = 2;
	actual $x;
	1;	# delay destruction
}

wrap *main::actual, post => sub { ok 6 };

actual my $x = 4;

no warnings qw/bareword reserved/;
eval { wrap other, pre => sub { print "not ok 7\n" } } or ok 7;

eval { wrap actual, pre => 1 } and print "not ";
ok 8;

eval { wrap actual, post => [] } and print "not ";
ok 9;

BEGIN { *{CORE::GLOBAL::sqrt} = sub { CORE::sqrt(shift) } }
wrap 'CORE::GLOBAL::sqrt', pre => sub { $_[0]++ };

$x = 99;
ok sqrt($x);

sub temp { ok $_[0] };

my $sub = wrap \&temp,
	pre  => sub { ok $_[0]-1 },
	post => sub { ok $_[0]+1 };

$sub->(12);
temp(14);

{
	local $SIG{__WARN__} = sub { ok 15 };
	eval { wrap \&temp, pre => sub { ok $_[0]-1 }; 1 } and ok 16;
}

use Carp;

sub wrapped_callee {
	return join '|', caller;
}

wrap wrapped_callee,
	pre =>sub{
		print "not " unless $_[0] eq join '|', caller;
		ok 17
	},
	post=>sub{
		print "not " unless $_[0] eq join '|', caller;
		ok 18
	};

sub raw_callee {
	return join '|', caller;
}

print "not " unless wrapped_callee(scalar raw_callee); ok 19;

sub scalar_return { return 'string' }
wrap scalar_return, post => sub { $_[-1] .= 'ent' };
print "not " unless scalar_return eq 'stringent'; ok 20;

sub list_return { return (0..9) }
wrap list_return, post => sub { @{$_[-1]} = reverse @{$_[-1]} };
my @result = list_return;
for (0..9) {
	print "not " and last unless $_ + $result[$_] == 9;
}
ok 21;

sub shorted_scalar { return 2 };
wrap shorted_scalar, pre => sub { $_[-1] = 1 };
fail { shorted_scalar != 1 }; ok 22;

sub shorted_list { return (2..9) };
{
	my $lexical = wrap shorted_list, pre => sub { $_[-1] = [1..9] };
	fail { (shorted_list)[0] != 1 }; ok 23;
}
{
	my $lexical = wrap shorted_list, pre => sub { $_[-1] = 1 };
	fail { (shorted_list)[0] != 1 }; ok 24;
}
{
	my $lexical = wrap shorted_list, pre => sub { @{$_[-1]} = (1..9) };
	fail { (shorted_list)[0] != 1 }; ok 25;
}
{
	my $lexical = wrap shorted_list, pre => sub { @{$_[-1]} = [1..9] };
	fail { (shorted_list)[0]->[0] != 1 }; ok 26;
}
{
	my $lexical = wrap shorted_list, post => sub { $_[-1] = [1..9] };
	fail { (shorted_list)[0] != 1 }; ok 27;
}
{
	my $lexical = wrap shorted_list, post => sub { $_[-1] = 1 };
	fail { (shorted_list)[0] != 1 }; ok 28;
}
{
	my $lexical = wrap shorted_list, post => sub { @{$_[-1]} = (1..9) };
	fail { (shorted_list)[0] != 1 }; ok 29;
}
{
	my $lexical = wrap shorted_list, post => sub { @{$_[-1]} = [1..9] };
	fail { (shorted_list)[0]->[0] != 1 }; ok 30;
}

sub howmany { ok 32 if @_ == 3 }

wrap howmany,
	pre  => sub { ok 31 if @_ == 4 },
	post => sub { ok 33 if @_ == 4 };

howmany(1..3);

{
no warnings 'uninitialized';
sub wanted { 
	my $expected = $_[3];
	print 'not ' unless defined wantarray == defined $expected
			 && wantarray eq $expected;
	ok $_[1]
}

wrap wanted,
	pre => sub {
		my $expected = $_[3];
		print 'not ' unless defined wantarray == defined $expected
				 && wantarray eq $expected;
		ok $_[0]
	},
	post => sub {
		my $expected = $_[3];
		print 'not ' unless defined wantarray == defined $expected
				 && wantarray eq $expected;
		ok $_[2]
	};

my @array  = wanted(34..36, 1);
my $scalar = wanted(37..39, "");
wanted(40..42,undef);
}

sub caller_test {
	print "not " unless (caller 0)[3] eq 'main::caller_test';  ok $_[0];
	print "not " unless (caller 1)[3] eq 'main::caller_outer'; ok $_[0]+1;
	print "not " unless (caller 2)[3] eq 'main::wrapped';      ok $_[0]+2;
	print "not " unless (caller 3)[3] eq 'main::inner';        ok $_[0]+3;
	print "not " unless (caller 4)[3] eq 'main::middle';       ok $_[0]+4;
	print "not " unless (caller 5)[3] eq 'main::outer';        ok $_[0]+5;
}

sub caller_outer {
	caller_test(@_);
}

sub wrapped {
	caller_outer(@_);
}

sub outer  { middle(@_) }
sub middle { inner(@_) }
sub inner  { wrapped(@_) }

outer(43..48);

wrap wrapped,
	pre => sub {},
	post => sub {};

wrap wrapped,
	pre => sub {},
	post => sub {};

wrap wrapped,
	pre => sub {},
	post => sub {};

outer(49..54);