| 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
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"}->()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 39 | 246 | ▶ | if (@envs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
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"}->()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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")
| |||||||||||||||||||||||||||||
| 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
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}
| |||||||||||||||||||||||||||||
| 76 | 492 | ▶ | $t{$std} = '' unless (exists $t{$std}); # silence expected by default | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 492 | ▶ | if (ref $t{$std} eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
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"}
| |||||||||||||||||||||||||||||
| 91 | 246 | ▶ | $t{exit} = 0 unless exists $t{exit}; # defailt exit code | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 246 | ▶ | 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"}->()
| |||||||||||||||||||||||||||||
| 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
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"}
| |||||||||||||||||||||||||||||
| 106 | 246 | ▶ | @{$t{clean}} = "$t{name}.got" unless exists $t{clean}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 246 | ▶ | map { unlink $_ if (-f $_) } @{$t{clean}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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]
MC/DC0%0/2
shift() || (caller)[1]
shift()
(caller)[1]
Truth table33%1/3
A || B
A= shift()B= (caller)[1]
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||