Cmd/Tester.pm

filter: Statementstmt 96.7% 88 / 91 Branchbran 81.5% 31 / 38 Conditioncond 64.7% 11 / 17 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 95.0% 19 / 20 Podpod 100% 3 / 3 totaltotal 88.2% 157 / 178 CC 26 SCAR 18.7
CC
26
Cov
89%
__ANON__
14.0
test_cmd_ok
7.1
test_cmd
5.0
CRAP 6.5
↑ Summary Cmd/Role.pm »
1 package MooX::Cmd::Tester;
2 # ABSTRACT: MooX cli app commands tester
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 our $VERSION = '1.001';
6  
7 6 require Exporter;
8 6 ▶use Test::More import => ['!pass'];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Capture::Tiny qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use parent qw(Test::Builder::Module Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our @EXPORT = qw(test_cmd test_cmd_ok);
15 6 our @EXPORT_OK = qw(test_cmd test_cmd_ok);
16  
17 6 our $TEST_IN_PROGRESS;
18 6 my $CLASS = __PACKAGE__;
19  
20 BEGIN
21 6 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.1
22 3 ▶ *CORE::GLOBAL::exit = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
23 3 ▶ return CORE::exit(@_) unless $TEST_IN_PROGRESS;
Branch: 1 branch
BranchTrueFalseTotal
unless $MooX::Cmd::Tester::TEST_IN_PROGRESS 0 3 3
24 3 MooX::Cmd::Tester::Exited->throw($_[0]);
25 6 };
26 }
27  
28  
29 36 ▶sub result_class { 'MooX::Cmd::Tester::Result' }
Subroutine result_class: called CC=1 SCAR=0.0
Pod documented
30  
31  
32 sub test_cmd
33 36 ▶{
Subroutine test_cmd: called CC=5 SCAR=16.1
Pod documented
34 36 my ($app, $argv) = @_;
35  
36 36 my $result = _run_with_capture($app, $argv);
37 36 ▶ my $exit_code = defined $result->{error} ? ((0 + $!) || -1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $result->{'error'} ? : 6 30 36
Condition50%1/2
0 + $! or -1
l!l
06
MC/DC0%0/1
0 + $! or -1
0 + $!
Truth table50%1/2
A
A= 0 + $!
Aresult
11
00
38  
39 $result->{error}
40 6 and eval { $result->{error}->isa('MooX::Cmd::Tester::Exited') }
41 36 ▶ and $exit_code = ${$result->{error}};
Branch: 1 branch
BranchTrueFalseTotal
$result->{"error"} and eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } } and $exit_code = ${$result->{"error"};} 3 33 36
Condition100%3/3
$result->{"error"} and eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } }
!ll&&!rl&&r
3033
MC/DC100%2/2
$result->{"error"} and eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } }
$result->{"error"} eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } }
Truth table100%3/3
A and B
A= $result->{"error"}B= eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } }
ABresult
0X0
100
111
42  
43 36 result_class->new(
44 {
45 exit_code => $exit_code,
46 %$result,
47 }
48 );
49 }
50  
51  
52 sub test_cmd_ok
53 16 ▶{
Subroutine test_cmd_ok: called CC=6 SCAR=19.6
Pod documented
54 16 my $rv = test_cmd(@_);
55  
56 16 my $test_ident = $rv->app . " => [ " . join(" ", @{$_[1]}) . " ]";
57 16 ▶ ok(!$rv->error, "Everythink ok running cmd $test_ident") or diag($rv->error);
Branch: 1 branch
BranchTrueFalseTotal
&ok(!$rv->error, "Everythink ok running cmd $test_ident") or diag($rv->error) 16 0 16
58 # no error and cmd means, we're reasonable successful so far
59 $rv
60 and not $rv->error
61 and $rv->cmd
62 and $rv->cmd->command_name
63 16 ▶ and ok($rv->cmd->command_commands->{$rv->cmd->command_name}, "found command at $test_ident");
Branch: 1 branch
BranchTrueFalseTotal
$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") 10 6 16
Condition44%4/9
$rv and not $rv->error and $rv->cmd and $rv->cmd->command_name
!ll&&!rl&&r
0610
$rv and not $rv->error and $rv->cmd
!ll&&!rl&&r
0016
$rv and not $rv->error
!ll&&!rl&&r
0016
MC/DC25%1/4
$rv and not $rv->error and $rv->cmd and $rv->cmd->command_name
$rv not $rv->error $rv->cmd $rv->cmd->command_name
Truth table40%2/5
A and not(B) and C and D
A= $rvB= not $rv->errorC= $rv->cmdD= $rv->cmd->command_name
ABCDresult
0XXX0
10XX0
110X0
11100
11111
64  
65 16 $rv;
66 }
67  
68 ## no critic qw(ProhibitSubroutinePrototypes)
69 sub _capture_merged(&)
70 36 ▶{
Subroutine _capture_merged: called CC=2 SCAR=7.6
71 36 my $code = shift;
72 36 my ($stdout, $stderr, $merged, $ok);
73 36 ▶ if ($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 36 36
74 {
75 0 ▶ ($stdout, $stderr, $ok) = tee { $code->(); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 $merged = $stdout . $stderr;
77 }
78 else
79 {
80 ($merged) = tee_merged
81 36 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 36 ▶ ($stdout, $stderr, $ok) = tee { $code->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
83 36 };
84 }
85 36 ($stdout, $stderr, $merged, $ok);
86 }
87  
88 sub _run_with_capture
89 36 ▶{
Subroutine _run_with_capture: called CC=1 SCAR=0.0
90 36 my ($app, $argv) = @_;
91  
92 36 my ($execute_rv, $cmd, $cmd_name, $error);
93  
94 my ($stdout, $stderr, $merged, $ok) = _capture_merged
95 36 ▶ {
Subroutine __ANON__: called CC=14 SCAR=26.4
96 36 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $MooX::Cmd::Tester::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(scalar $cmd->isa($app), "got a '${app}' from new_with_cmd"); @$argv and defined($cmd_name = $cmd->command_name) and &ok(scalar 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 30 6 36
97 36 local $TEST_IN_PROGRESS = 1;
98 36 local @ARGV = @$argv;
99  
100 36 my $tb = $CLASS->builder();
101  
102 36 ▶ $cmd = ref $app ? $app : $app->new_with_cmd;
Branch: 1 branch
BranchTrueFalseTotal
ref $app ? : 8 28 36
103 30 ▶ ref $app and $app = ref $app;
Branch: 1 branch
BranchTrueFalseTotal
ref $app and $app = ref $app 8 22 30
104 30 my $test_ident = "$app => [ " . join(" ", @$argv) . " ]";
105 30 ok($cmd->isa($app), "got a '$app' from new_with_cmd");
106 @$argv
107 and defined($cmd_name = $cmd->command_name)
108 30 ▶ and ok((grep { index($cmd_name, $_) != -1 } @$argv), "proper cmd name from $test_ident");
Branch: 1 branch
BranchTrueFalseTotal
@$argv and defined($cmd_name = $cmd->command_name) and &ok(scalar grep({index($cmd_name, $_) != -1;} @$argv), "proper cmd name from $test_ident") 19 11 30
Condition100%3/3
@$argv and defined($cmd_name = $cmd->command_name)
!ll&&!rl&&r
10119
MC/DC100%2/2
@$argv and defined($cmd_name = $cmd->command_name)
@$argv defined($cmd_name = $cmd->command_name)
Truth table100%3/3
A and B
A= @$argvB= defined($cmd_name = $cmd->command_name)
ABresult
0X0
100
111
109 30 ok(scalar @{$cmd->command_chain} <= 1 + scalar @$argv, "\$#argv vs. command chain length testing $test_ident");
110 30 ▶ @$argv and ok($cmd->command_chain_end == $cmd->command_chain->[-1], "command_chain_end ok");
Branch: 1 branch
BranchTrueFalseTotal
@$argv and &ok($cmd->command_chain_end == $cmd->command_chain->[-1], "command_chain_end ok") 20 10 30
111  
112 30 ▶ unless ($execute_rv = $cmd->execute_return)
Branch: 1 branch
BranchTrueFalseTotal
unless ($execute_rv = $cmd->execute_return) 10 20 30
113 {
114 10 my ($command_execute_from_new, $command_execute_method_name);
115 10 my $cce = $cmd->can("command_chain_end");
116 10 ▶ $cce and $cce = $cce->($cmd);
Branch: 1 branch
BranchTrueFalseTotal
$cce and $cce = &$cce($cmd) 10 0 10
117 10 ▶ $cce and $command_execute_from_new = $cce->can("command_execute_from_new");
Branch: 1 branch
BranchTrueFalseTotal
$cce and $command_execute_from_new = $cce->can("command_execute_from_new") 10 0 10
118 10 ▶ $command_execute_from_new and $command_execute_from_new = $command_execute_from_new->($cce);
Branch: 1 branch
BranchTrueFalseTotal
$command_execute_from_new and $command_execute_from_new = &$command_execute_from_new($cce) 7 3 10
119 10 ▶ $command_execute_from_new or $command_execute_method_name = $cce->can('command_execute_method_name');
Branch: 1 branch
BranchTrueFalseTotal
$command_execute_from_new or $command_execute_method_name = $cce->can("command_execute_method_name") 0 10 10
120 10 ▶ $command_execute_method_name
Branch: 1 branch
BranchTrueFalseTotal
$command_execute_method_name and $execute_rv = [$cce->can(&$command_execute_method_name($cce))->($cce)] 7 3 10
121 and $execute_rv = [$cce->can($command_execute_method_name->($cce))->($cce)];
122 }
123 30 1;
124 } or $error = 1;
125 36 ▶ $@ and $error = $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and $error = $@ 6 30 36
126 36 };
127  
128 return {
129 36 app => $app,
130 cmd => $cmd,
131 stdout => $stdout,
132 stderr => $stderr,
133 output => $merged,
134 error => $error,
135 execute_rv => $execute_rv,
136 };
137 }
138  
139 {
140 ## no critic qw(ProhibitMultiplePackages)
141 package # no-index
142 MooX::Cmd::Tester::Result;
143  
144 sub new
145 36 ▶ {
Subroutine new: called CC=1 SCAR=0.0
146 36 my ($class, $arg) = @_;
147 36 bless $arg => $class;
148 }
149 }
150  
151 6 my $res = Package::Stash->new("MooX::Cmd::Tester::Result");
152 6 for my $attr (qw(app cmd stdout stderr output error execute_rv exit_code))
153 {
154 181 ▶ $res->add_symbol('&' . $attr, sub { $_[0]->{$attr} });
Subroutine __ANON__: called CC=1 SCAR=0.0
155 }
156  
157 {
158 ## no critic qw(ProhibitMultiplePackages)
159 package # no-index
160 MooX::Cmd::Tester::Exited;
161  
162 sub throw
163 3 ▶ {
Subroutine throw: called CC=2 SCAR=7.0
164 3 my ($class, $code) = @_;
165 3 ▶ defined $code or $code = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $code or $code = 0 3 0 3
166 3 my $self = (bless \$code => $class);
167 ## no critic qw(RequireCarping)
168 3 die $self;
169 }
170 }
171  
172  
173 6 1;
174  
175 __END__
↑ Summary Cmd/Role.pm »