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
|
#!perl -T
use strict;
use warnings;
use lib 't/lib';
use Test::Leaner 'no_plan';
use Scope::Upper qw<yield HERE SCOPE>;
# @_[0 .. $#_] also ought to work, but it sometimes evaluates to nonsense in
# scalar context on perl 5.8.5 and below.
sub list { wantarray ? @_ : $_[$#_] }
my @blocks = (
[
'do {',
'}'
],
[
'(list map {', # map in scalar context yields the number of elements
'} 1)'
],
[
'sub {
my $next = shift;',
'}->($next, @_)'
],
[
'eval {',
'}'
],
);
my @contexts = (
[ '', '; ()', 'v' ],
[ 'scalar(', ')', 's' ],
[ 'list(', ')', 'l' ],
);
sub linearize { join ', ', map { defined($_) ? $_ : '(undef)' } @_ }
our @stack;
our @pre;
# Don't put closures in empty pads on 5.6.
my $dummy;
my $capture_outer_pad = "$]" < 5.008 ? "++\$dummy;" : '';
my @test_frames;
for my $block (@blocks) {
for my $context (@contexts) {
my $source = <<"FRAME";
sub {
my \$next = shift; $capture_outer_pad
$block->[0]
unshift \@stack, HERE;
$context->[0]
(\@{shift \@pre}, \$next->[0]->(\@_))
$context->[1]
$block->[1]
}
FRAME
my $code;
{
local $@;
$code = do {
no warnings 'void';
eval $source;
};
my $err = $@;
chomp $err;
die "$err. Source was :\n$source\n" if $@;
}
push @test_frames, [ $code, $source, $context->[2] ];
}
}
my @targets = (
[ sub {
my $depth = pop;
unshift @stack, HERE;
yield(@_ => $stack[$depth]);
}, 'target context from HERE' ],
[ sub {
my $depth = pop;
yield(@_ => SCOPE($depth == 0 ? 0 : (2 * ($depth - 1) + 1)));
}, 'target context from SCOPE' ],
);
my $seed = 0;
for my $args ([ ], [ 'A' ], [ qw<B C> ]) {
my @args = @$args;
for my $frame0 (@test_frames) {
for my $frame1 (@test_frames) {
for my $frame2 (@test_frames) {
my $max_depth = 3;
$seed += 5; # Coprime with $max_depth
my @prepend;
for (1 .. $max_depth) {
++$seed;
my $i = $seed + $_;
my $l = $seed % $max_depth - 1;
push @prepend, [ $i .. ($i + $l) ];
}
my $prepend_str = join ' ', map { '[' . join(' ', @$_) . ']' } @prepend;
for my $depth (0 .. $max_depth) {
my $exp = do {
my @cxts = map $_->[2], $frame0, $frame1, $frame2;
my @exp = @args;
for (my $i = $depth + 1; $i <= $max_depth; ++$i) {
my $c = $cxts[$max_depth - $i];
if ($c eq 'v') {
@exp = ();
} elsif ($c eq 's') {
@exp = @exp ? $exp[-1] : undef;
} else {
unshift @exp, @{$prepend[$max_depth - $i]};
}
}
linearize @exp;
};
for my $target (@targets) {
local @stack;
local @pre = @prepend;
my @res = $frame0->[0]->($frame1, $frame2, $target, @args, $depth);
my $got = linearize @res;
if ($got ne $exp) {
diag <<DIAG;
=== This testcase failed ===
$frame0->[1]
$frame1->[1]
$frame2->[1]
$target->[1]
==== vvvvv Errors vvvvvv ===
DIAG
}
is $got, $exp, "yield to depth $depth with args [@args] and prepending $prepend_str";
}
}
}
}
}
}
|