| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Test::Mojo::CommandOutputRole; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 79770 | use Role::Tiny; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 4 | 1 |  |  | 1 |  | 165 | use Mojo::Base -strict, -signatures; | 
|  | 1 |  |  |  |  | 25 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 5 | 1 |  |  | 1 |  | 178 | use Test::More; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 13 |  | 
| 6 | 1 |  |  | 1 |  | 870 | use Capture::Tiny 'capture'; | 
|  | 1 |  |  |  |  | 4206 |  | 
|  | 1 |  |  |  |  | 277 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.02'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 3 |  |  | 3 | 1 | 23535 | sub command_output ($t, $command, $args, $test, $test_name = 'Output test') { | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 3 |  | 
| 11 |  |  |  |  |  |  | subtest $test_name => sub { | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | # Capture successful command output | 
| 14 | 3 |  |  | 3 |  | 2488 | my ($output, $error) = capture {$t->app->start($command => @$args)}; | 
|  | 3 |  |  |  |  | 3421 |  | 
| 15 | 3 |  |  |  |  | 9752 | is $error => '', 'No error thrown by command'; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # Test code: execute tests on output | 
| 18 | 1 |  |  |  |  | 845 | return subtest 'Handle command output' => sub {$test->($output)} | 
| 19 | 3 | 100 |  |  |  | 1214 | if ref($test) eq 'CODE'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # Output string regex test | 
| 22 | 2 | 100 |  |  |  | 10 | return like $output => $test, 'Output regex' | 
| 23 |  |  |  |  |  |  | if ref($test) eq 'Regexp'; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # Output string equality test | 
| 26 | 1 |  |  |  |  | 3 | return is $output => $test, 'Correct output string'; | 
| 27 | 3 |  |  |  |  | 18 | }} | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | 1; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =pod | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =encoding utf8 | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =head1 NAME | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | Test::Mojo::CommandOutputRole | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | A role to extend L to make C output tests easy. | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | =begin html | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  |   | 
| 44 |  |  |  |  |  |  |   | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =end html | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | my $t = Test::Mojo->new->with_roles('Test::Mojo::CommandOutputRole'); | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | # Test for string equality | 
| 54 |  |  |  |  |  |  | $t->command_output(do_something => [qw(arg1 arg2)] => 'Expected output', | 
| 55 |  |  |  |  |  |  | 'Correct do_something output'); | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | # Test for regex matching | 
| 58 |  |  |  |  |  |  | $t->command_output(do_something => [qw(arg1 arg2)] => | 
| 59 |  |  |  |  |  |  | qr/^ \s* Expected\ answer\ is\ [3-5][1-3] \.? $/x, | 
| 60 |  |  |  |  |  |  | 'Matching do_something output'); | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # Complex test | 
| 63 |  |  |  |  |  |  | $t->command_output(do_something => [] => sub ($output) { | 
| 64 |  |  |  |  |  |  | ok defined($output), 'Output is defined'; | 
| 65 |  |  |  |  |  |  | is length($output) => 42, 'Correct length'; | 
| 66 |  |  |  |  |  |  | }, 'Output test results OK'); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | B: | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | # Subtest: Correct do_something output | 
| 71 |  |  |  |  |  |  | ok 1 - Command didn't die | 
| 72 |  |  |  |  |  |  | ok 2 - Correct output string | 
| 73 |  |  |  |  |  |  | 1..2 | 
| 74 |  |  |  |  |  |  | ok 3 - Correct do_something output | 
| 75 |  |  |  |  |  |  | # Subtest: Matching do_something output | 
| 76 |  |  |  |  |  |  | ok 1 - Command didn't die | 
| 77 |  |  |  |  |  |  | ok 2 - Output regex | 
| 78 |  |  |  |  |  |  | 1..2 | 
| 79 |  |  |  |  |  |  | ok 4 - Matching do_something output | 
| 80 |  |  |  |  |  |  | # Subtest: Output test results OK | 
| 81 |  |  |  |  |  |  | ok 1 - Command didn't die | 
| 82 |  |  |  |  |  |  | # Subtest: Handle command output | 
| 83 |  |  |  |  |  |  | ok 1 - Output is defined | 
| 84 |  |  |  |  |  |  | ok 2 - Correct length | 
| 85 |  |  |  |  |  |  | 1..2 | 
| 86 |  |  |  |  |  |  | ok 2 - Handle command output | 
| 87 |  |  |  |  |  |  | 1..2 | 
| 88 |  |  |  |  |  |  | ok 5 - Output test results OK | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Test::Mojo::CommandOutputRole adds a method C to L that offers a convenient way to test the output of commands. | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =head2 How to use it | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | This extension is a Role that needs to be added to L via C: | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | my $t = Test::Mojo->new->with_roles('Test::Mojo::CommandOutputRole'); | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head2 C<$t-Ecommand_output($command, $args, $test, $test_name);> | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | Runs a L with tests against the output of C<$command>. Arguments: | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =over 4 | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =item C<$command> | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | The name of the command to run. | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =item C<$args> | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | An array reference of commands for C<$command>. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =item C<$test> | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | The test to run the command output against. This can have three types: If it is a simple string, C tests for string equality. If it is a regular expression (via C), it tries to match it against the command output. If it is a code reference (via C), complex tests can be run inside the given code. The test output is then given as the first argument. | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =item C<$test_name> (optional) | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | A name for the enclosing subtest, default ist C<"Output test">. | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =back | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =head1 REPOSITORY AND ISSUE TRACKER | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | This distribution's source repository is hosted on L together with an issue tracker. | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | Copyright (c) 2019 L (L<@memowe|https://github.com/memowe>, L) | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | Released under the MIT License (see LICENSE.txt for details). | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =head2 CONTRIBUTORS | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =over 2 | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =item Renee Bäcker (L<@reneeb|https://github.com/reneeb>) | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =back | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =cut |