# use "*_t" for basename of this file.
# symlink to *-<perl version string to test>.t.
# e.g.,
#   "20-foo-v5.5.3.t"
#   "20-bar-5.005_003"
#   "20-bar-v5.005003"
# will test varieties of the same perl version string.
########################################################################
# housekeeping
########################################################################

use 5.006;
use version;

use Test::More;
use Test::Deep;

use File::Basename  qw( basename    );
use FindBin         qw( $Bin        );

########################################################################
# package variables 
########################################################################

my $madness = 'Module::FromPerlVer';
my $v_dir   = "$Bin/version";
my $v_file  = 'perl_verison';
my @argz    = qw( use_dir 1 version_from ), $v_file;

note "Test sandbox: '$v_dir'";

########################################################################
# test reading version from "use" or "no".
########################################################################

delete $ENV{ PERL_VERSION };

SKIP:
{
    my $base    = basename $0, '.t';

    my ( $perl_v )
    =  do
    {
        my $i   = rindex $base, '-';
        substr $base, ++$i
    }
    or 
    skip "Botched test: no perl version in '$base'.", 1;

    note "Testing perl version: '$perl_v'.";

    version->parse( $perl_v )->numify
    or skip "Botched test: non-parsable '$perl_v' ($base).";

    -d $v_dir
    or skip "Botched test: missing '$v_dir'";

    for ( $v_file )
    {
        open my $fh, '>', $_
        or skip "Failed opening: '$_'";

        print $fh, $perl_v;
    }

    require_ok $madness;

    $madness->import( @argz );

    for my $found ( scalar $madness->source_files )
    {
        @$found
        ? pass 'Found source files in version directory'
        : fail 'Missing source files for cleanup'
        ;

        note "Source files:\n", explain $found;

        ok -e , "Found: '$_'"
        for @$found;
    }

    eval
    {
        $madness->cleanup;
        unlink $v_file;

        pass "Survived cleanup.";
    }
    or 
    fail "Failed cleanup: $@";
}

done_testing;
__END__
