blib/lib/App/NDTools/Test.pm

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