The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Mojo::CommandOutputRole

A role to extend Test::Mojo to make mojo command output tests easy.

Travis CI tests

SYNOPSIS

    my $t = Test::Mojo->new->with_roles('Test::Mojo::CommandOutputRole');

    # Test for string equality
    $t->command_output(do_something => [qw(arg1 arg2)] => 'Expected output',
        'Correct do_something output');

    # Test for regex matching
    $t->command_output(do_something => [qw(arg1 arg2)] =>
        qr/^ \s* Expected\ answer\ is\ [3-5][1-3] \.? $/x,
        'Matching do_something output');

    # Complex test
    $t->command_output(do_something => [] => sub ($output) {
        ok defined($output), 'Output is defined';
        is length($output) => 42, 'Correct length';
    }, 'Output test results OK');

Test results:

    # Subtest: Correct do_something output
        ok 1 - Command didn't die
        ok 2 - Correct output string
        1..2
    ok 3 - Correct do_something output
    # Subtest: Matching do_something output
        ok 1 - Command didn't die
        ok 2 - Output regex
        1..2
    ok 4 - Matching do_something output
    # Subtest: Output test results OK
        ok 1 - Command didn't die
        # Subtest: Handle command output
            ok 1 - Output is defined
            ok 2 - Correct length
            1..2
        ok 2 - Handle command output
        1..2
    ok 5 - Output test results OK

DESCRIPTION

Test::Mojo::CommandOutputRole adds a method command_output to Test::Mojo that offers a convenient way to test the output of commands.

How to use it

This extension is a Role that needs to be added to Test::Mojo via with_role:

    my $t = Test::Mojo->new->with_roles('Test::Mojo::CommandOutputRole');

$t->command_output($command, $args, $test, $test_name);

Runs a "subtest" in Test::More with tests against the output of $command. Arguments:

$command

The name of the command to run.

$args

An array reference of commands for $command.

$test

The test to run the command output against. This can have three types: If it is a simple string, command_output tests for string equality. If it is a regular expression (via qr/.../), it tries to match it against the command output. If it is a code reference (via sub {...}), complex tests can be run inside the given code. The test output is then given as the first argument.

$test_name (optional)

A name for the enclosing subtest, default ist "Output test".

REPOSITORY AND ISSUE TRACKER

This distribution's source repository is hosted on GitHub together with an issue tracker.

LICENSE AND COPYRIGHT

Copyright (c) 2019 Mirko Westermeier (@memowe, mirko@westermeier.de)

Released under the MIT License (see LICENSE.txt for details).

CONTRIBUTORS

Renee Bäcker (@reneeb)