#! perl
# Copyright (C) 2006, The Perl Foundation.
# $Id: pmc2c.t 14964 2006-10-18 18:20:48Z particle $
# 02-find_file.t

BEGIN {
    use FindBin qw($Bin);
    use Cwd qw(cwd realpath);
    realpath($Bin) =~ m{^(.*\/parrot)\/[^/]*\/[^/]*\/[^/]*$};
    $topdir = $1;
    if (defined $topdir) {
        print "\nOK:  Parrot top directory located\n";
    } else {
        die "Unable to locate top-level Parrot directory";
    }
    unshift @INC, qq{$topdir/lib};
}
use strict;
use warnings;
use Test::More tests =>  7;
use_ok( 'Parrot::Pmc2c::Utils' );

my (%opt, @include, @args);
my $self;

my $pmcdir = qq{$main::topdir/src/pmc};
@include = ( $pmcdir );
$self = Parrot::Pmc2c::Utils->new( {
    include => \@include,
    opt     => \%opt,
    args    => [ @args ],
} );

my ($file, $path);
$file = q{array.pmc};

$path = $self->find_file($file);
ok(-f $path, "$file found");

$path = $self->find_file(qq{$pmcdir/$file});
ok(-f $path, "absolute path; $file found");

$file = q{dskflajsdfghjdshfg};

$path = $self->find_file($file);
ok(! defined $path,
    "nonexistent file correctly returned undef");

$path = $self->find_file(qq{$pmcdir/$file});
ok(! defined $path,
    "absolute path but nonexistent file correctly returned undef");

eval { $path = $self->find_file($file, 1); };
like($@, qr/^cannot find file '$file' in path/,
    "program correctly died when 'die_unless_found' flag is set");
    
pass("Completed all tests in $0");

################### DOCUMENTATION ###################

=head1 NAME

02-find_file.t - test C<Parrot::Pmc2c::Utils::find_file()>

=head1 SYNOPSIS

    % prove t/tools/pmc2cutils/02-find_file.t

=head1 DESCRIPTION

The files in this directory test the publicly callable methods of 
F<lib/Parrot/Pmc2c/Utils.pm>.  By doing so, they test the functionality 
of the F<pmc2c.pl> utility.  That functionality has largely been extracted 
into the methods of F<Utils.pm>.

F<02-find_file.t> tests C<Parrot::Pmc2c::Utils::find_file()>, which is 
called within other Parrot::Pmc2c::Util methods.  The tests assume a certain
file structure in the Parrot tarball; they may no longer be valid if that
structure changes in the future.

=head1 AUTHOR

James E Keenan

=head1 SEE ALSO

Parrot::Pmc2c, pmc2c.pl.

=cut

