Blame t/install.t

Packit 1c5632
use Sub::Install;
Packit 1c5632
use Test::More tests => 17;
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
  my $sub_ref = Sub::Install::install_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, \&ok, 'it returns the correct code ref');
Packit 1c5632
Packit 1c5632
  ok1(1, 'installed 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::install_sub({ code => \&ok, as => 'tmp_ok' });
Packit 1c5632
Packit 1c5632
    my $expected_warning = <<'END_WARNING';
Packit 1c5632
Subroutine main::tmp_ok redefined at t/install.t line 31
Packit 1c5632
Prototype mismatch: sub main::tmp_ok ($;$) vs ($$;$) at t/install.t line 31
Packit 1c5632
END_WARNING
Packit 1c5632
Packit 1c5632
    my $stderr = Test::Output::stderr_from(
Packit 1c5632
      sub { Sub::Install::install_sub({ code => \&is, as => 'tmp_ok' }) }
Packit 1c5632
    );
Packit 1c5632
Packit 1c5632
    $stderr =~ s!\.$!!gm;
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 $redef = 0;
Packit 1c5632
  my $proto = 0;
Packit 1c5632
Packit 1c5632
  local $SIG{__WARN__} = sub {
Packit 1c5632
    return ($redef = 1) if $_[0] =~ m{Subroutine \S+ redef.+t.install\.t};
Packit 1c5632
    return ($proto = 1) if $_[0] =~ m{Prototype mismatch.+t.install\.t};
Packit 1c5632
    # pass("warned as expected: $_[0]") if $_[0] =~ /redefined/;
Packit 1c5632
    die "unexpected warning: @_";
Packit 1c5632
  };
Packit 1c5632
Packit 1c5632
  my $sub_ref = Sub::Install::install_sub({ code => \&is, as => 'ok1' });
Packit 1c5632
Packit 1c5632
  ok($redef, 'correct redefinition warning went to $SIG{__WARN__}');
Packit 1c5632
  ok($proto, 'correct prototype 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, \&is, 'install2 returns correct code ref');
Packit 1c5632
Packit 1c5632
  ok1(1,1, 'installed sub runs (with new arguments)');
Packit 1c5632
}
Packit 1c5632
Packit 1c5632
{ # Install in another package...
Packit 1c5632
  my $sub_ref = Sub::Install::install_sub({
Packit 1c5632
    code => \&ok,
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, \&ok, 'it returns the correct code ref');
Packit 1c5632
Packit 1c5632
  ok1(1,1, 'sub previously installed into main still runs properly');
Packit 1c5632
Packit 1c5632
  package Other;
Packit 1c5632
  ok1(1,   'remotely installed sub runs properly');
Packit 1c5632
}
Packit 1c5632
Packit 1c5632
{ # cross-package installation
Packit 1c5632
  sub Other::Another::foo { return $_[0] }
Packit 1c5632
Packit 1c5632
  my $sub_ref = Sub::Install::install_sub({
Packit 1c5632
    code => 'foo',
Packit 1c5632
    from => 'Other::Another',
Packit 1c5632
    into => 'Other::YetAnother',
Packit 1c5632
    as   => 'return_lhs'
Packit 1c5632
  });
Packit 1c5632
Packit 1c5632
  isa_ok($sub_ref, 'CODE', 'return value of fourth install_sub');
Packit 1c5632
Packit 1c5632
  is_deeply(
Packit 1c5632
    $sub_ref,
Packit 1c5632
    \&Other::Another::foo,
Packit 1c5632
    'it returns the correct code ref'
Packit 1c5632
  );
Packit 1c5632
Packit 1c5632
  is(
Packit 1c5632
    Other::Another->foo,
Packit 1c5632
    'Other::Another',
Packit 1c5632
    'the original code does what we want',
Packit 1c5632
  );
Packit 1c5632
Packit 1c5632
  is(
Packit 1c5632
    Other::YetAnother->return_lhs,
Packit 1c5632
    'Other::YetAnother',
Packit 1c5632
    'and the installed code works, too',
Packit 1c5632
  );
Packit 1c5632
}