| 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
Condition33%1/3
exists $t{"skip"} and $t{"skip"}->()
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"}->()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | 187 | ▶ | if (@envs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $t{"pre"} and not $t{"pre"}->()
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"}->()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
builtin::blessed($@) and $@->isa("Log::Log4Cli::Exception")
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")
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $t{$std} and not defined $t{$std}
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}
| |||||||||||||||||||||||||||||
| 69 | 373 | ▶ | $t{$std} = '' unless (exists $t{$std}); # silence expected by default | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 373 | ▶ | if (ref $t{$std} eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
not exists $t{"exit"} or defined $t{"exit"}
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"}
| |||||||||||||||||||||||||||||
| 84 | 186 | ▶ | $t{exit} = 0 unless exists $t{exit}; # defailt exit code | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 186 | ▶ | if (exists $t{post} and not $t{post}->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $t{"post"} and not $t{"post"}->()
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"}->()
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
not exists $t{"clean"} or defined $t{"clean"}
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"}
| |||||||||||||||||||||||||||||
| 99 | 186 | ▶ | @{$t{clean}} = "$t{name}.got" unless exists $t{clean}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 186 | ▶ | map { unlink $_ if (-f $_) } @{$t{clean}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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]
MC/DC0%0/2
shift() || (caller)[1]
shift()
(caller)[1]
Truth table33%1/3
A || B
A= shift()B= (caller)[1]
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||