#!/usr/bin/perl -w

BEGIN {
    if( $ENV{PERL_CORE} ) {
        chdir 't' if -d 't';
        @INC = ('../lib', 'lib');
    }
    else {
        unshift @INC, 't/lib';
    }
}

chdir 't';

use ExtUtils::MM;

use Test::More tests => 6;

my $mm = bless { NAME => "Foo" }, "MM";

# I don't expect anything to have a length shorter than 256 chars.
cmp_ok( $mm->max_exec_len, '>=', 256,   'max_exec_len' );

my $echo = $mm->oneliner(q{print @ARGV}, ['-l']);

# Force a short command length to make testing split_command easier.
$mm->{_MAX_EXEC_LEN} = length($echo) + 15;
is( $mm->max_exec_len, $mm->{_MAX_EXEC_LEN}, '  forced a short max_exec_len' );

my @test_args = qw(foo bar baz yar car har ackapicklerootyjamboree);
my @cmds = $mm->split_command($echo, @test_args);
isnt( @cmds, 0 );

s{\$\(PERLRUN\)}{$^X} foreach @cmds;
is( join('', map { s/\n+$//; $_ } map { `$_` } @cmds), join('', @test_args));


my %test_args = ( foo => 42, bar => 23, car => 'har' );
$even_args = $mm->oneliner(q{print !(@ARGV % 2)});
@cmds = $mm->split_command($even_args, %test_args);
isnt( @cmds, 0 );

s{\$\(PERLRUN\)}{$^X} foreach @cmds;
like( join('', map { s/\n+$//; $_ } map { `$_` } @cmds), qr/^1+$/,
                                                    'pairs preserved' );
