line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Mojo::CommandOutputRole; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
82929
|
use Role::Tiny; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
4
|
1
|
|
|
1
|
|
190
|
use Mojo::Base -strict, -signatures; |
|
1
|
|
|
|
|
27
|
|
|
1
|
|
|
|
|
10
|
|
5
|
1
|
|
|
1
|
|
192
|
use Test::More; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
18
|
|
6
|
1
|
|
|
1
|
|
921
|
use Capture::Tiny 'capture'; |
|
1
|
|
|
|
|
4405
|
|
|
1
|
|
|
|
|
280
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
1
|
24710
|
sub command_output ($t, $command, $args, $test, $test_name = 'Output test') { |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
6
|
|
11
|
|
|
|
|
|
|
subtest $test_name => sub { |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Capture successful command output |
14
|
3
|
|
|
3
|
|
2334
|
my ($output, $error) = capture {$t->app->start($command => @$args)}; |
|
3
|
|
|
|
|
3692
|
|
15
|
3
|
|
|
|
|
10089
|
is $error => '', 'No error thrown by command'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Test code: execute tests on output |
18
|
1
|
|
|
|
|
927
|
return subtest 'Handle command output' => sub {$test->($output)} |
19
|
3
|
100
|
|
|
|
1266
|
if ref($test) eq 'CODE'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Output string regex test |
22
|
2
|
100
|
|
|
|
11
|
return like $output => $test, 'Output regex' |
23
|
|
|
|
|
|
|
if ref($test) eq 'Regexp'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Output string equality test |
26
|
1
|
|
|
|
|
4
|
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 |