Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 146 lines (131 sloc) 4.063 kb
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
use strict;
use warnings;

package PAUSE::Test::pmfile;

use Test::FITesque::Fixture;
use base qw(Test::FITesque::Fixture);
use Test::More;

use Test::MockObject;
use Test::MockObject::Extends;
use PAUSE::mldistwatch;
use Mock::Dist;
use Module::Faker::Dist;
use Path::Class ();
use Data::Dumper;
use YAML;

#my $PACKAGE = Test::MockObject::Extends->new('PAUSE::package');
my $PACKAGE = Test::MockObject->new;
$PACKAGE->fake_module(
  'PAUSE::package',
  new => sub { shift; %{ $PACKAGE } = @_; return $PACKAGE },
);
$PACKAGE->mock(examine_pkg => sub {});

my ($fake_dist, $fake_dist_dir);
sub fake_dist {
  $fake_dist ||= Module::Faker::Dist->from_file('t/dist/My-Dist.yaml');
}

sub fake_dist_dir {
  $fake_dist_dir ||= Path::Class::dir(shift->fake_dist->make_dist_dir);
}

sub new {
  my ($self) = shift->SUPER::new(@_);
  my $mock = delete $self->{mock} || {};
  $self->{dist} ||= Mock::Dist->new;
  $self->{dist}{DIST} ||= 'My-Dist';
  $self->{pmfile} ||= PAUSE::pmfile->new(
    PMFILE => "fake-pmfile",
    DIO => $self->{dist},
    USERID => 'FAKE',
    TIME => time,
    YAML_CONTENT => {},
    VERSION => $self->fake_dist->version,
  );
  $self->{pmfile} = Test::MockObject::Extends->new($self->{pmfile});
  for my $o (keys %$mock) {
    for my $m (keys %{ $mock->{$o} || {} }) {
      my $to_mock = $mock->{$o}{$m};
      if (ref $to_mock eq 'ARRAY') {
        $self->{$o}->$m(@$_) for @$to_mock;
      } else {
        $self->{$o}->$m($to_mock->{$_}) for keys %$to_mock;
      }
    }
  }
  return $self;
}

sub dist_mock_ok {
  my ($self, $method, $args) = @_;
  $self->{pmfile}->$method(@$args);
  $self->{dist}->next_call_ok($method, $args);
}

sub dist_mock :Test :Plan(1) {
  my ($self, $method, @args) = @_;
  $self->dist_mock_ok($method, \@args);
}

my $ppp = 'My::Package';
sub filter_ppps :Test :Plan(3) {
  my ($self, $no_index, $expect) = @_;
  $self->{pmfile}{YAML_CONTENT}{no_index} = $no_index;

  my @res = $self->{pmfile}->filter_ppps($ppp);
  is_deeply(
    \@res,
    $expect->{skip} ? [] : [$ppp],
    "expected result",
  );
  if ($expect->{reason}) {
    my $reason = $expect->{reason};
    if ($no_index) {
      $reason = ($expect->{skip})
              ? "Skipping ppp[$ppp] $reason"
              : "NOT skipping ppp[$ppp] $reason";
    }
    $self->{dist}->next_call_ok(verbose => [ 1, $reason ]);
    $self->{dist}->next_call_ok(verbose => [ 1, "Result of filter_ppps: res[@res]" ]);
  } else {
    ok( ! $self->{dist}->called('verbose'), "no verbose() call");
    ok(1, "dummy");
    $self->{dist}->clear;
  }
}

sub simile :Test :Plan(2) {
  my ($self, $file, $package, $ret) = @_;
  my $label = "$file and $package are "
    . ($ret ? "" : "not ") . "similes";
  ok( $self->{pmfile}->simile($file, $package) == $ret, $label );
  $file =~ s/\.pm$//;
  $self->{dist}->verbose_ok(
    1, "Result of simile(): file[$file] package[$package] ret[$ret]\n"
  );
}

sub examine_fio :Test :Plan(3) {
  my ($self) = @_;
  my $pmfile = $self->{pmfile};
  $pmfile->{PMFILE} = $self->fake_dist_dir->file('lib/My/Dist.pm')->stringify;
  $pmfile->examine_fio;
  $self->{dist}->next_call for 1..5; # skip over some irrelevant logging
# $self->{dist}->next_call_ok(connect => []);
# $self->{dist}->next_call_ok(version_from_yaml_ok => []);
# $self->{dist}->verbose_ok(1, "simile: file[Dist] package[My::Dist] ret[1]\n");
# $self->{dist}->verbose_ok(1, "no keyword 'no_index' or 'private' in YAML_CONTENT");
# $self->{dist}->verbose_ok(1, "res[My::Dist]");
  $self->{dist}->verbose_ok(1, "Will check keys_ppp[My::Dist]\n");
  is_deeply(
    [ @{$PACKAGE}{ qw(PACKAGE DIST FIO TIME PMFILE USERID YAML_CONTENT) } ],
    [
      'My::Dist', 'My-Dist', $pmfile,
      @{$pmfile}{qw(TIME PMFILE USERID YAML_CONTENT)},
    ],
    "correct package info",
  );
  delete $PACKAGE->{PP}{pause_reg}; # cannot guess
  is_deeply(
    $PACKAGE->{PP},
    {
      parsed => 1,
      filemtime => (stat $pmfile->{PMFILE})[9],
      infile => $pmfile->{PMFILE},
      simile => $pmfile->{PMFILE},
    },
    "correct package PP",
  );
}

1;
Something went wrong with that request. Please try again.