Blame t/Devel-LexAlias.t

Packit 510177
#!perl -w
Packit 510177
use strict;
Packit 510177
use Test::More tests => 11;
Packit 510177
Packit 510177
use Devel::LexAlias qw(lexalias);
Packit 510177
Packit 510177
# testing for predictive destruction.  especially around ithreads
Packit 510177
my $expect;
Packit 510177
sub Foo::DESTROY {
Packit 510177
    my ($destroyed) = @{ shift() };
Packit 510177
    is( $destroyed, $expect, "expected destruction of $expect" );
Packit 510177
}
Packit 510177
Packit 510177
sub inner {
Packit 510177
    my $inner = bless ['$inner'], 'Foo';
Packit 510177
    $expect = '$outer';
Packit 510177
    lexalias(1, '$outer', \$inner);
Packit 510177
    $expect = '';
Packit 510177
}
Packit 510177
Packit 510177
sub outer {
Packit 510177
    my $outer = bless [ '$outer' ], 'Foo';
Packit 510177
    inner;
Packit 510177
    is ( $outer->[0], '$inner', "alias worked" );
Packit 510177
    $expect = '$inner';
Packit 510177
}
Packit 510177
outer;
Packit 510177
Packit 510177
sub steal_foo {
Packit 510177
    my $foo = 1;
Packit 510177
    lexalias(\&foo, '$x', \$foo);
Packit 510177
    lexalias(\&foo, '@y', [qw( foo bar baz )]);
Packit 510177
Packit 510177
    eval { lexalias(\&foo, '$x', $foo) };
Packit 510177
    ok( $@, "blew an error" );
Packit 510177
    like( $@, qr/^ref is not a reference/, "useful error" );
Packit 510177
}
Packit 510177
Packit 510177
sub bar {
Packit 510177
    my $foo = 2;
Packit 510177
    lexalias(2, '$x', \$foo);
Packit 510177
}
Packit 510177
Packit 510177
sub steal_above {
Packit 510177
    bar();
Packit 510177
    lexalias(1, '@y', [qw( foo bar bray )]);
Packit 510177
}
Packit 510177
Packit 510177
Packit 510177
sub foo {
Packit 510177
    my $x = 22;
Packit 510177
    my @y = qw( a b c );
Packit 510177
Packit 510177
    is( $x, 22, "x before" );
Packit 510177
    is_deeply( \@y, [qw( a b c )], "y before" );
Packit 510177
Packit 510177
    steal_foo;
Packit 510177
Packit 510177
    is( $x, 1, "x after" );
Packit 510177
    is_deeply( \@y, [qw( foo bar baz )], "y after" );
Packit 510177
Packit 510177
    steal_above;
Packit 510177
Packit 510177
    is( $x, 2, "x above after" );
Packit 510177
    is_deeply( \@y, [qw( foo bar bray )], "y after" );
Packit 510177
Packit 510177
}
Packit 510177
Packit 510177
foo;
Packit 510177
print "# out of foo\n";
Packit 510177
Packit 510177
exit 0;