# Copyright (C) 2005-2006, The Perl Foundation.
# $Id: harness 15144 2006-11-07 07:11:23Z fperrad $

=head1 NAME

languages/lua/t/harness - A harness for Parrot Lua

=head1 SYNOPSIS

    cd languages && perl -I../lib -Ilua/t lua/t/harness --files

    cd languages && perl -I../lib -Ilua/t lua/t/harness

    cd languages && perl -I../lib -Ilua/t lua/t/harness lua/t/examples.t

    cd languages && perl -I../lib lua/t/harness lua/t/pmc/nil.t

=head1 DESCRIPTION

If I'm called with a single
argument of "--files", I just return a list of files to process.
This list is one per line, and is relative to the languages dir.

If I'm called with no args, I run the complete suite.

If I'm called with "--use-lua", I run with the original C<lua>
in order to valid of the test suite.

If I'm called with "--use-monkey", I run with C<monkey>.

If I'm called with "--use-lua2pir", I run with C<lua2pir> (and just after
C<luac>).

Otherwise I run the tests that were passed on the command line.

=cut

use strict;
use warnings;
use lib '..';

use Cwd();
use Data::Dumper;
use File::Spec;
use Getopt::Long;
use Test::Harness();

my $language = 'lua';

my $opt_files;
my ($use_orig_lua, $use_monkey, $use_lua2pir);
GetOptions(
    'files' => \$opt_files,
    'use-lua' => \$use_orig_lua,
    'use-monkey' => \$use_monkey,
    'use-lua2pir' => \$use_lua2pir,
);

if ( $opt_files ) {

    # Only the Makefile in 'parrot/languages' uses --files
    my $dir = File::Spec->catfile( $language, 't' );
    my @files = glob( File::Spec->catfile( $dir, 'pmc', '*.t' ) );
    push @files, glob( File::Spec->catfile( $dir, 'type', '*.t' ) );
    push @files, glob( File::Spec->catfile( $dir, '*.t' ) );
    print join( "\n", @files );
    print "\n" if scalar(@files);
}
else {
    my @files;

    if ( scalar(@ARGV) ) {

        # Someone specified tests for me to run.
        @files = grep { -f $_ } @ARGV;
    }
    else {
        ( undef, undef, my $current_dir )
            = File::Spec->splitpath( Cwd::getcwd() );
        my $dir;
        if ( $current_dir eq 'languages' ) {
            $dir = File::Spec->catfile( $language, 't' );
        }
        elsif ( $current_dir eq $language ) {
            $dir = 't';
        }
        if ($dir) {
            @files = glob( File::Spec->catfile( $dir, '*.t' ) );
            push @files, glob( File::Spec->catfile( $dir, 'pmc', '*.t' ) )
                unless ($use_orig_lua);
            push @files, glob( File::Spec->catfile( $dir, 'type', '*.t' ) )
                unless ($use_orig_lua);
        }
    }

    if ($use_orig_lua) {
        $ENV{PARROT_LUA_TEST_PROG} = 'lua';
    }
    elsif ($use_monkey) {
        $ENV{PARROT_LUA_TEST_PROG} = 'monkey';
    }
    elsif ($use_lua2pir) {
        $ENV{PARROT_LUA_TEST_PROG} = 'lua2pir';
    }
    else {
        $ENV{PARROT_LUA_TEST_PROG} = q{};
    }
    Test::Harness::runtests(@files) if scalar(@files);
}

=head1 HISTORY

Mostly taken from F<languages/bc/t/harness>.

=head1 SEE ALSO

F<languages/tcl/t/harness>, F<languages/scheme/t/harness>, F<languages/m4/t/harness>, F<languages/python/t/harness>

=head1 AUTHOR

Francois Perrad

=cut

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:

