Blame t/Utils.pm

Packit 82cce8
package Utils;
Packit 82cce8

Packit 82cce8
use strict;
Packit 82cce8
use warnings;
Packit 82cce8
use vars qw( $VERSION @ISA @EXPORT );
Packit 82cce8
require Exporter;
Packit 82cce8
use Module::ScanDeps qw(path_to_inc_name);
Packit 82cce8

Packit 82cce8
use Test::More;
Packit 82cce8

Packit 82cce8
@ISA=qw(Exporter);
Packit 82cce8
$VERSION   = '0.1';
Packit 82cce8
@EXPORT = qw( generic_scandeps_rv_test compare_scandeps_rvs generic_abs_path );
Packit 82cce8

Packit 82cce8
my $test = Test::More->builder;
Packit 82cce8

Packit 82cce8
sub import {
Packit 82cce8
    my($self) = shift;
Packit 82cce8
    my $pack = caller;
Packit 82cce8

Packit 82cce8
    $test->exported_to($pack);
Packit 82cce8
    $self->export_to_level(1, $self, @EXPORT);
Packit 82cce8
}
Packit 82cce8

Packit 82cce8
sub generic_scandeps_rv_test {
Packit 82cce8
    my $rv = shift;
Packit 82cce8
    my $array_ref = shift;
Packit 82cce8
    my @input_keys = sort @$array_ref;
Packit 82cce8
       $array_ref = shift;
Packit 82cce8
    my @known_deps = sort @$array_ref;
Packit 82cce8
    my @used_by;
Packit 82cce8
    my ($used_by_ok, $i);
Packit 82cce8

Packit 82cce8
    # sanity check input
Packit 82cce8
    foreach my $input (@input_keys) {
Packit 82cce8
      !(grep {$_ eq $input} @known_deps) or die "\@input_keys overlaps with \@known_deps\n";
Packit 82cce8
    }
Packit 82cce8

Packit 82cce8
    $test->ok(ref($rv) eq "HASH", "\$rv is a ref") or return;
Packit 82cce8

Packit 82cce8
    # check all input files and known deps correspond to an entry in rv
Packit 82cce8
    map {$_ = path_to_inc_name($_, 1)} @input_keys;
Packit 82cce8
    map {$_ =~ s|\\|\/|go} (@input_keys, @known_deps);
Packit 82cce8
    $test->ok(exists $rv->{$_}, "$_ is in rv") foreach (@input_keys, @known_deps);
Packit 82cce8

Packit 82cce8
    # Check general properties of the keys
Packit 82cce8
    foreach my $key (keys %$rv) {
Packit 82cce8
        $test->ok(exists($rv->{$key}{key})  && $key eq $rv->{$key}{key}, "For $key: the sub-key matches");
Packit 82cce8
        $test->ok(exists($rv->{$key}{file}) && $rv->{$key}{file} =~ /(?:^|[\/\\])\Q$key\E$/
Packit 82cce8
                                            && File::Spec->file_name_is_absolute($rv->{$key}{file}), "For $key: the file has been verified");
Packit 82cce8
        $test->ok(exists($rv->{$key}{type}) && $rv->{$key}{type} =~ /^(?:module|autoload|data|shared)$/, "For $key: the type matches module|autoload|data|shared");
Packit 82cce8

Packit 82cce8
        if (exists($rv->{$key}{used_by})) {
Packit 82cce8
            @used_by = sort @{$rv->{$key}{used_by}};
Packit 82cce8
            if (scalar @used_by > 0) {
Packit 82cce8
                $used_by_ok = 1;
Packit 82cce8
                if (scalar @used_by > 1) {
Packit 82cce8
                    for ($i=0; $i<$#used_by; $i++) {
Packit 82cce8
                        if ($used_by[$i] eq $used_by[$i+1]) { # relies on @used_by being sorted earlier
Packit 82cce8
                             $used_by_ok = 0;
Packit 82cce8
                             last;
Packit 82cce8
                        }
Packit 82cce8
                    }
Packit 82cce8
                }
Packit 82cce8
                $test->ok($used_by_ok, "$key\'s used_by has no duplicates");
Packit 82cce8

Packit 82cce8
                $used_by_ok = 1;
Packit 82cce8
                foreach my $used_by (@used_by) {
Packit 82cce8
                    $used_by_ok &= exists($rv->{$used_by});
Packit 82cce8
                }
Packit 82cce8
                $test->ok($used_by_ok, "All entries in $key\'s used_by are themselves described in \$rv");
Packit 82cce8

Packit 82cce8
                # check corresponding uses field
Packit 82cce8
                foreach my $used_by (@used_by) {
Packit 82cce8
                    if (exists($rv->{$used_by}{uses})) {
Packit 82cce8
                        $test->ok(scalar(grep { $_ eq $key } @{$rv->{$used_by}{uses}}), "\$rv contains a matching uses field for the used_by entry $used_by for key $key");
Packit 82cce8
                    } else {
Packit 82cce8
                        $test->ok(0, "\$rv contains a matching uses field for the used_by entry $used_by for key $key");
Packit 82cce8
                    }
Packit 82cce8
                }
Packit 82cce8
            } else {
Packit 82cce8
                $test->ok(0, "$key\'s used_by exists and isn't empty");
Packit 82cce8
            }
Packit 82cce8
        } else {
Packit 82cce8
            $test->ok((grep {$_ eq $key} @input_keys) | ($key =~ m/Plugin/o), "used-by not defined so $key must be one of the input files or is a plugin");
Packit 82cce8
        }
Packit 82cce8

Packit 82cce8
        if (exists($rv->{$key}{uses})) {
Packit 82cce8
            # check corresponding used_by field
Packit 82cce8
            foreach my $uses (@{$rv->{$key}{uses}}) {
Packit 82cce8
                if (exists($rv->{$uses}{used_by})) {
Packit 82cce8
                    $test->ok(scalar(grep { $_ eq $key } @{$rv->{$uses}{used_by}}), "\$rv contains a matching used_by field for the uses entry $uses for key $key");
Packit 82cce8
                } else {
Packit 82cce8
                    $test->ok(0, "\$rv contains a matching used_by field for the uses entry $uses for key $key");
Packit 82cce8
                }
Packit 82cce8
            }
Packit 82cce8
         }
Packit 82cce8
    }
Packit 82cce8
}
Packit 82cce8

Packit 82cce8
sub compare_scandeps_rvs {
Packit 82cce8
    my $rv_to_test = shift;
Packit 82cce8
    my $rv_to_match = shift;
Packit 82cce8
    my $array_ref = shift;
Packit 82cce8
    my @input_keys = @$array_ref;
Packit 82cce8

Packit 82cce8
    my (@used_by_test, @used_by_match);
Packit 82cce8
    my (@uses_test, @uses_match);
Packit 82cce8
    my ($used_by_ok, $uses_ok);
Packit 82cce8
    my ($compare_ok, $i);
Packit 82cce8

Packit 82cce8
    generic_scandeps_rv_test($rv_to_match, \@input_keys, []); # validate test data
Packit 82cce8

Packit 82cce8
    $test->ok(ref($rv_to_test) eq "HASH", "\$rv_to_test is a ref") or return;
Packit 82cce8

Packit 82cce8
    my @rv_to_match_keys = sort keys %{$rv_to_match};
Packit 82cce8
    my @rv_to_test_keys  = sort keys %{$rv_to_test};
Packit 82cce8
    $test->cmp_ok(scalar @rv_to_test_keys, '==', scalar @rv_to_match_keys, "Number of keys in \$rv_to_test == Number of keys in \$rv_to_match") or return;
Packit 82cce8
    $compare_ok = 1;
Packit 82cce8
    for ($i=0; $i<=$#rv_to_match_keys; $i++) {
Packit 82cce8
        $compare_ok &= ($rv_to_match_keys[$i] eq $rv_to_test_keys[$i]);
Packit 82cce8
    }
Packit 82cce8
    $test->ok($compare_ok, "Keys in \$rv_to_test all eq keys in \$rv_to_match");
Packit 82cce8

Packit 82cce8
    foreach my $key (@rv_to_match_keys) {
Packit 82cce8
        $test->ok(exists($rv_to_test->{$key}{key})  && $rv_to_test->{$key}{key}  eq $rv_to_match->{$key}{key}, "For $key: sub-key matches the expected");
Packit 82cce8
        $test->ok(exists($rv_to_test->{$key}{file}) && $rv_to_test->{$key}{file} eq $rv_to_match->{$key}{file}, "For $key: file matches the expected");
Packit 82cce8
        $test->ok(exists($rv_to_test->{$key}{type}) && $rv_to_test->{$key}{type} eq $rv_to_match->{$key}{type}, "For $key: type matches the expected");
Packit 82cce8

Packit 82cce8
        if (exists($rv_to_match->{$key}{used_by})) {
Packit 82cce8
            $test->ok(exists($rv_to_test->{$key}{used_by}), "For $key: used_by exists as expected") or next;
Packit 82cce8

Packit 82cce8
            @used_by_test  = sort @{$rv_to_test->{$key}{used_by}}; # order isn't important
Packit 82cce8
            @used_by_match = sort @{$rv_to_match->{$key}{used_by}}; # order isn't important   
Packit 82cce8
            $test->cmp_ok(scalar @used_by_test, '==', scalar @used_by_match, "For $key: number of used_by in \$rv_to_test == Number of used_by in \$rv_to_match") or next;
Packit 82cce8

Packit 82cce8
            $used_by_ok = 1;
Packit 82cce8
            for ($i=0; $i < scalar @used_by_match; $i++) {
Packit 82cce8
                $used_by_ok &= ($used_by_match[$i] eq $used_by_test[$i]);
Packit 82cce8
            }
Packit 82cce8
            $test->ok($used_by_ok, "For $key: used_by in \$rv_to_test all eq used_by in \$rv_to_match");
Packit 82cce8
        }
Packit 82cce8

Packit 82cce8
        if (exists($rv_to_match->{$key}{uses})) {
Packit 82cce8
            $test->ok(exists($rv_to_test->{$key}{uses}), "For $key: uses exists as expected") or next;
Packit 82cce8

Packit 82cce8
            @uses_test  = sort @{$rv_to_test->{$key}{uses}}; # order isn't important
Packit 82cce8
            @uses_match = sort @{$rv_to_match->{$key}{uses}}; # order isn't important
Packit 82cce8
            $test->cmp_ok(scalar @uses_test, '==', scalar @uses_match, "For $key: number of uses in \$rv_to_test == Number of uses in \$rv_to_match") or next;
Packit 82cce8

Packit 82cce8
            $uses_ok = 1;
Packit 82cce8
            for ($i=0; $i < scalar @uses_match; $i++) {
Packit 82cce8
                $uses_ok &= ($uses_match[$i] eq $uses_test[$i]);
Packit 82cce8
            }
Packit 82cce8
            $test->ok($uses_ok, "For $key: uses in \$rv_to_test all eq uses in \$rv_to_match");
Packit 82cce8
        }
Packit 82cce8
    }
Packit 82cce8
}
Packit 82cce8

Packit 82cce8
sub generic_abs_path {
Packit 82cce8
  my $file = shift @_;
Packit 82cce8
  $file = File::Spec->rel2abs($file);
Packit 82cce8
  $file =~ s|\\|\/|go;
Packit 82cce8
  return $file;
Packit 82cce8
}
Packit 82cce8

Packit 82cce8

Packit 82cce8
1;
Packit 82cce8
# Marks the end of any code. Any symbols after this are ignored. Use for documentation
Packit 82cce8
__END__