Blame t/reinstall.t

Packit 1c5632
use Sub::Install qw(reinstall_sub);
Packit 1c5632
use Test::More tests => 15;
Packit 1c5632
Packit 1c5632
use strict;
Packit 1c5632
use warnings;
Packit 1c5632
Packit 1c5632
# These tests largely copied from Damian Conway's Sub::Installer tests.
Packit 1c5632
Packit 1c5632
{ # Install a sub in a package...
Packit 1c5632
Packit 1c5632
  my $sub_ref = reinstall_sub({ code => \&ok, as => 'ok1' });
Packit 1c5632
Packit 1c5632
  isa_ok($sub_ref, 'CODE', 'return value of first install_sub');
Packit 1c5632
Packit 1c5632
  is_deeply($sub_ref, \&Test::More::ok, 'it returned the right coderef');
Packit 1c5632
Packit 1c5632
  $sub_ref->(1, 'returned code ref runs');
Packit 1c5632
  ok1(1, "reinstalled sub runs");
Packit 1c5632
}
Packit 1c5632
Packit 1c5632
{
Packit 1c5632
  my $to_avail = eval "use Test::Output; 1";
Packit 1c5632
  SKIP: {
Packit 1c5632
    skip "can't run this test without Test::Output", 1 unless $to_avail;
Packit 1c5632
    Sub::Install::reinstall_sub({ code => \&ok, as => 'tmp_ok' });
Packit 1c5632
Packit 1c5632
    my $expected_warning = <<'END_WARNING';
Packit 1c5632
Prototype mismatch: sub main::tmp_ok ($;$) vs ($$;$) at t/reinstall.t line 32
Packit 1c5632
END_WARNING
Packit 1c5632
Packit 1c5632
    my $stderr = Test::Output::stderr_from(
Packit 1c5632
      sub { Sub::Install::reinstall_sub({ code => \&is, as => 'tmp_ok' }) }
Packit 1c5632
    );
Packit 1c5632
Packit 1c5632
    $stderr =~ s!\\!/!g;
Packit 1c5632
    $stderr =~ s!\.$!!g;
Packit 1c5632
    is(
Packit 1c5632
      $stderr,
Packit 1c5632
      $expected_warning,
Packit 1c5632
      "got expected warning",
Packit 1c5632
    );
Packit 1c5632
  }
Packit 1c5632
}
Packit 1c5632
Packit 1c5632
{ # Install the same sub in the same package...
Packit 1c5632
  my $proto = 0;
Packit 1c5632
Packit 1c5632
  local $SIG{__WARN__} = sub {
Packit 1c5632
    return ($proto = 1) if $_[0] =~ m{Prototype mismatch.+t.reinstall\.t};
Packit 1c5632
    die "unexpected warning: @_";
Packit 1c5632
  };
Packit 1c5632
Packit 1c5632
  my $sub_ref = reinstall_sub({ code => \&is, as => 'ok1' });
Packit 1c5632
Packit 1c5632
  ok($proto, 'correct warning went to $SIG{__WARN__}');
Packit 1c5632
Packit 1c5632
  isa_ok($sub_ref, 'CODE', 'return value of second install_sub');
Packit 1c5632
Packit 1c5632
  is_deeply($sub_ref, \&Test::More::is, 'it returned the right coderef');
Packit 1c5632
Packit 1c5632
  $sub_ref->(1, 1, 'returned code ref runs');
Packit 1c5632
  ok1(1,1, 'reinstalled sub reruns');
Packit 1c5632
}
Packit 1c5632
Packit 1c5632
{ # Install in another package...
Packit 1c5632
  my $new_code = sub { ok(1, "remotely installed sub runs") };
Packit 1c5632
Packit 1c5632
  my $sub_ref = reinstall_sub({
Packit 1c5632
    code => $new_code,
Packit 1c5632
    into => 'Other',
Packit 1c5632
    as   => 'ok1',
Packit 1c5632
  });
Packit 1c5632
Packit 1c5632
  isa_ok($sub_ref, 'CODE', 'return value of third install_sub');
Packit 1c5632
Packit 1c5632
  is_deeply($sub_ref, $new_code, 'it returned the right coderef');
Packit 1c5632
Packit 1c5632
  ok1(1,1, 'reinstalled sub reruns');
Packit 1c5632
Packit 1c5632
  package Other;
Packit 1c5632
  ok1();
Packit 1c5632
}
Packit 1c5632
Packit 1c5632
eval {
Packit 1c5632
  my $arg = { code => sub {}, into => 'Other', as => 'ok1' };
Packit 1c5632
  Sub::Install::_build_public_installer(\&Sub::Install::_install_fatal)->($arg);
Packit 1c5632
};
Packit 1c5632
Packit 1c5632
like($@, qr/redefine/, "(experimental fatal installer should croak)");