MooX-Cmd

 view release on metacpan or  search on metacpan

lib/MooX/Cmd/Tester.pm  view on Meta::CPAN


    $result->{error}
      and eval { $result->{error}->isa('MooX::Cmd::Tester::Exited') }
      and $exit_code = ${$result->{error}};

    result_class->new(
        {
            exit_code => $exit_code,
            %$result,
        }
    );
}


sub test_cmd_ok
{
    my $rv = test_cmd(@_);

    my $test_ident = $rv->app . " => [ " . join(" ", @{$_[1]}) . " ]";
    ok(!$rv->error, "Everythink ok running cmd $test_ident") or diag($rv->error);
    # no error and cmd means, we're reasonable successful so far
    $rv
      and not $rv->error
      and $rv->cmd
      and $rv->cmd->command_name
      and ok($rv->cmd->command_commands->{$rv->cmd->command_name}, "found command at $test_ident");

    $rv;
}

## no critic qw(ProhibitSubroutinePrototypes)
sub _capture_merged(&)
{
    my $code = shift;
    my ($stdout, $stderr, $merged, $ok);
    if ($^O eq 'MSWin32')
    {
        ($stdout, $stderr, $ok) = tee { $code->(); };
        $merged = $stdout . $stderr;
    }
    else
    {
        ($merged) = tee_merged
        {
            ($stdout, $stderr, $ok) = tee { $code->() };
        };
    }
    ($stdout, $stderr, $merged, $ok);
}

sub _run_with_capture
{
    my ($app, $argv) = @_;

    my ($execute_rv, $cmd, $cmd_name, $error);

    my ($stdout, $stderr, $merged, $ok) = _capture_merged
    {
        eval {
            local $TEST_IN_PROGRESS = 1;
            local @ARGV             = @$argv;

            my $tb = $CLASS->builder();

            $cmd = ref $app ? $app : $app->new_with_cmd;
            ref $app and $app = ref $app;
            my $test_ident = "$app => [ " . join(" ", @$argv) . " ]";
            ok($cmd->isa($app), "got a '$app' from new_with_cmd");
            @$argv
              and defined($cmd_name = $cmd->command_name)
              and ok((grep { index($cmd_name, $_) != -1 } @$argv), "proper cmd name from $test_ident");
            ok(scalar @{$cmd->command_chain} <= 1 + scalar @$argv, "\$#argv vs. command chain length testing $test_ident");
            @$argv and ok($cmd->command_chain_end == $cmd->command_chain->[-1], "command_chain_end ok");

            unless ($execute_rv = $cmd->execute_return)
            {
                my ($command_execute_from_new, $command_execute_method_name);
                my $cce = $cmd->can("command_chain_end");
                $cce                      and $cce                      = $cce->($cmd);
                $cce                      and $command_execute_from_new = $cce->can("command_execute_from_new");
                $command_execute_from_new and $command_execute_from_new = $command_execute_from_new->($cce);
                $command_execute_from_new or $command_execute_method_name = $cce->can('command_execute_method_name');
                $command_execute_method_name
                  and $execute_rv = [$cce->can($command_execute_method_name->($cce))->($cce)];
            }
            1;
        } or $error = 1;
        $@ and $error = $@;
    };

    return {
        app        => $app,
        cmd        => $cmd,
        stdout     => $stdout,
        stderr     => $stderr,
        output     => $merged,
        error      => $error,
        execute_rv => $execute_rv,
    };
}

{
    ## no critic qw(ProhibitMultiplePackages)
    package    # no-index
      MooX::Cmd::Tester::Result;

    sub new
    {
        my ($class, $arg) = @_;
        bless $arg => $class;
    }
}

my $res = Package::Stash->new("MooX::Cmd::Tester::Result");
for my $attr (qw(app cmd stdout stderr output error execute_rv exit_code))
{
    $res->add_symbol('&' . $attr, sub { $_[0]->{$attr} });
}

{
    ## no critic qw(ProhibitMultiplePackages)



( run in 3.872 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )