blib/lib/App/NDTools/Test.pm

filter: Statementstmt 85.8% 79 / 92 Branchbran 73.6% 28 / 38 Conditioncond 50.0% 12 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 4 totaltotal 70.0% 133 / 190 CC 29 SCAR 24.7
CC
29
Cov
77%
__ANON__
19.9
run_ok
13.1
t_dir
2.3
CRAP 11.9
« blib/lib/App/NDTools/Slurp.pm ↑ Summary blib/lib/App/NDTools/NDDiff.pm »
1 package App::NDTools::Test;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # relative path will be incorrect when `cd t_dir` used
15 # required by Pod::Usage
16 15 $0 = abs_path($0);
17 }
18  
19 15 our @EXPORT = qw(
20 run_ok
21 t_ab_cmp
22 t_dir
23 t_dump
24 );
25  
26 246 ▶sub run_ok {
Subroutine run_ok: called CC=11 SCAR=25.7
Pod undocumented
27 246 my %t = @_;
28  
29 246 local $Test::Builder::Level = $Test::Builder::Level + 1;
30  
31 246 ▶ if (exists $t{skip} and $t{skip}->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t{"skip"} and $t{"skip"}->()) 0 246 246
Condition33%1/3
exists $t{"skip"} and $t{"skip"}->()
!ll&&!rl&&r
24600
MC/DC0%0/2
exists $t{"skip"} and $t{"skip"}->()
exists $t{"skip"} $t{"skip"}->()
Truth table33%1/3
A and B
A= exists $t{"skip"}B= $t{"skip"}->()
ABresult
0X0
100
111
32 0 pass("Test '$t{name}' cancelled by 'skip' opt");
33 0 return;
34 }
35  
36 0 ▶ my @envs = exists $t{env} ? %{$t{env}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $t{'env'} ? : 0 246 246
37 SET_ENV: # can't use loop here - env vars will be localized in it's block
38 246 ▶ @envs and local $ENV{$envs[0]} = $envs[1];
Branch: 1 branch
BranchTrueFalseTotal
@envs and local $ENV{$envs[0]} = $envs[1] 0 246 246
39 246 ▶ if (@envs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@envs) 0 246 246
40 0 splice @envs, 0, 2;
41 0 goto SET_ENV;
42 }
43  
44 246 ▶ if (exists $t{pre} and not $t{pre}->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t{"pre"} and not $t{"pre"}->()) 0 246 246
Condition66%2/3
exists $t{"pre"} and not $t{"pre"}->()
!ll&&!rl&&r
1381080
MC/DC0%0/2
exists $t{"pre"} and not $t{"pre"}->()
exists $t{"pre"} not $t{"pre"}->()
Truth table66%2/3
A and not(B)
A= exists $t{"pre"}B= not $t{"pre"}->()
ABresult
0X0
100
111
45 0 fail("Pre hook for '$t{name}' failed");
46 0 return;
47 }
48  
49 246 my ($out, $err, $exit);
50 246 ▶ if (eval { $t{cmd}->[0]->isa('App::NDTools::NDTool') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $t{'cmd'}[0]->isa('App::NDTools::NDTool') } }) { } 243 3 246
51 243 my $tool = shift @{$t{cmd}};
52  
53 243 ▶ ($out, $err) = capture {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 243 local $Log::Log4Cli::LEVEL = 0; # reset loglevel
55 243 eval { $tool->new(@{$t{cmd}})->exec() }
56 243 };
57  
58 243 ▶ if (blessed($@) and $@->isa('Log::Log4Cli::Exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa('Log::Log4Cli::Exception')) { } 243 0 243
Condition33%1/3
builtin::blessed($@) and $@->isa("Log::Log4Cli::Exception")
!ll&&!rl&&r
00243
MC/DC0%0/2
builtin::blessed($@) and $@->isa("Log::Log4Cli::Exception")
builtin::blessed($@) $@->isa("Log::Log4Cli::Exception")
Truth table33%1/3
A and B
A= builtin::blessed($@)B= $@->isa("Log::Log4Cli::Exception")
ABresult
0X0
100
111
59 243 $err .= $@->{LOG_MESSAGE};
60 243 $exit = $@->{EXIT_CODE};
61 } else {
62 0 $err .= $@;
63 0 $exit = 255;
64 }
65  
66 243 unshift @{$t{cmd}}, $tool;
67 } else { # assume it's binary
68 3 ▶ ($out, $err, $exit) = capture { system(@{$t{cmd}}) };
Subroutine __ANON__: called CC=1 SCAR=0.0
69 3 $exit = $exit >> 8;
70 }
71  
72 246 ▶ subtest $t{name} => sub {
Subroutine __ANON__: called CC=18 SCAR=29.9
73  
74 246 for my $std ('stdout', 'stderr') {
75 492 ▶ next if (exists $t{$std} and not defined $t{$std}); # set to undef to skip test
Branch: 1 branch
BranchTrueFalseTotal
if exists $t{$std} and not defined $t{$std} 0 492 492
Condition66%2/3
exists $t{$std} and not defined $t{$std}
!ll&&!rl&&r
3251670
MC/DC0%0/2
exists $t{$std} and not defined $t{$std}
exists $t{$std} not defined $t{$std}
Truth table66%2/3
A and not(B)
A= exists $t{$std}B= not defined $t{$std}
ABresult
0X0
100
111
76 492 ▶ $t{$std} = '' unless (exists $t{$std}); # silence expected by default
Branch: 1 branch
BranchTrueFalseTotal
unless exists $t{$std} 325 167 492
77  
78 492 my $desc = uc($std) . " check for $t{name}: [@{$t{cmd}}]";
79 492 ▶ my $data = $std eq 'stdout' ? $out : $err;
Branch: 1 branch
BranchTrueFalseTotal
$std eq 'stdout' ? : 246 246 492
80  
81 492 ▶ if (ref $t{$std} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $t{$std} eq 'CODE') { } 112 380 492
elsif (ref $t{$std} eq 'Regexp') { } 52 328 380
82 112 ok($t{$std}->($data), $desc);
83 } elsif (ref $t{$std} eq 'Regexp') {
84 52 like($data, $t{$std}, $desc);
85 } else {
86 328 is($data, $t{$std}, $desc);
87 }
88 }
89  
90 246 ▶ if (not exists $t{exit} or defined $t{exit}) { # set to undef to skip test
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $t{"exit"} or defined $t{"exit"}) 246 0 246
Condition66%2/3
not exists $t{"exit"} or defined $t{"exit"}
l!l&&r!l&&!r
1321140
MC/DC0%0/2
not exists $t{"exit"} or defined $t{"exit"}
not exists $t{"exit"} defined $t{"exit"}
Truth table66%2/3
not(A) or B
A= not exists $t{"exit"}B= defined $t{"exit"}
ABresult
1X1
011
000
91 246 ▶ $t{exit} = 0 unless exists $t{exit}; # defailt exit code
Branch: 1 branch
BranchTrueFalseTotal
unless exists $t{"exit"} 132 114 246
92 is(
93 $exit, $t{exit},
94 246 "Exit code check for $t{name}: [@{$t{cmd}}]"
95 );
96 }
97  
98 246 ▶ $t{test}->() if (exists $t{test});
Branch: 1 branch
BranchTrueFalseTotal
if exists $t{"test"} 88 158 246
99  
100 246 ▶ if (exists $t{post} and not $t{post}->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t{"post"} and not $t{"post"}->()) 0 246 246
Condition33%1/3
exists $t{"post"} and not $t{"post"}->()
!ll&&!rl&&r
24600
MC/DC0%0/2
exists $t{"post"} and not $t{"post"}->()
exists $t{"post"} not $t{"post"}->()
Truth table33%1/3
A and not(B)
A= exists $t{"post"}B= not $t{"post"}->()
ABresult
0X0
100
111
101 0 fail("Post hook for '$t{name}' failed");
102 0 return;
103 }
104  
105 246 ▶ if (not exists $t{clean} or defined $t{clean}) { # set to undef to skip
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $t{"clean"} or defined $t{"clean"}) 246 0 246
Condition66%2/3
not exists $t{"clean"} or defined $t{"clean"}
l!l&&r!l&&!r
23970
MC/DC0%0/2
not exists $t{"clean"} or defined $t{"clean"}
not exists $t{"clean"} defined $t{"clean"}
Truth table66%2/3
not(A) or B
A= not exists $t{"clean"}B= defined $t{"clean"}
ABresult
1X1
011
000
106 246 ▶ @{$t{clean}} = "$t{name}.got" unless exists $t{clean};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $t{"clean"} 239 7 246
107 246 ▶ map { unlink $_ if (-f $_) } @{$t{clean}};
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ 119 134 253
108 }
109  
110 246 done_testing();
111 }
112 246 }
113  
114 0 ▶sub t_ab_cmp {
Subroutine t_ab_cmp: not called CC=1 SCAR=6.9
Pod undocumented
115 0 return "GOT: " . t_dump(shift) . "\nEXP: " . t_dump(shift);
116 }
117  
118 15 ▶sub t_dir {
Subroutine t_dir: called CC=2 SCAR=8.1
Pod undocumented
119 15 ▶ my $tfile = shift || (caller)[1];
Condition33%1/3
shift() || (caller)[1]
l!l&&r!l&&!r
0150
MC/DC0%0/2
shift() || (caller)[1]
shift() (caller)[1]
Truth table33%1/3
A || B
A= shift()B= (caller)[1]
ABresult
1X1
011
000
120 15 substr($tfile, 0, length($tfile) - 1) . "d";
121 }
122  
123 0 ▶sub t_dump {
Subroutine t_dump: not called CC=1 SCAR=6.9
Pod undocumented
124 0 return Data::Dumper->new([shift])->Terse(1)->Sortkeys(1)->Quotekeys(0)->Indent(0)->Deepcopy(1)->Dump();
125 }
126  
127 15 1;
« blib/lib/App/NDTools/Slurp.pm ↑ Summary blib/lib/App/NDTools/NDDiff.pm »