File Coverage

blib/lib/Test/Data/Unixish.pm
Criterion Covered Total %
statement 84 110 76.3
branch 21 36 58.3
condition 9 18 50.0
subroutine 13 13 100.0
pod 0 1 0.0
total 127 178 71.3


line stmt bran cond sub pod time code
1             ## no critic: (Modules::ProhibitAutomaticExportation)
2              
3             package Test::Data::Unixish;
4              
5             our $DATE = '2019-10-26'; # DATE
6             our $VERSION = '1.571'; # VERSION
7              
8 41     41   31669 use 5.010001;
  41         376  
9 41     41   210 use strict;
  41         83  
  41         1104  
10 41     41   214 use warnings;
  41         77  
  41         1389  
11              
12 41     41   19291 use Data::Unixish qw(aiduxa);
  41         108  
  41         2811  
13 41     41   18844 use File::Which qw(which);
  41         42951  
  41         2642  
14 41     41   27358 use IPC::Cmd qw(run_forked);
  41         2451024  
  41         2718  
15 41     41   20947 use JSON::MaybeXS;
  41         318617  
  41         2538  
16 41     41   319 use Module::Load;
  41         95  
  41         282  
17 41     41   20573 use String::ShellQuote;
  41         32988  
  41         2604  
18 41     41   25519 use Test::More 0.96;
  41         2632673  
  41         353  
19              
20             require Exporter;
21             our @ISA = qw(Exporter);
22             our @EXPORT = qw(test_dux_func);
23              
24             my $json = JSON::MaybeXS->new->allow_nonref;
25              
26             sub test_dux_func {
27 41     41   12801 no strict 'refs';
  41         105  
  41         41690  
28              
29 38     38 0 6916 my %args = @_;
30 38         123 my $fn = $args{func};
31 38         93 my $fnl = $fn; $fnl =~ s/.+:://;
  38         114  
32 38         253 load "Data::Unixish::$fn";
33 38         688 my $f = "Data::Unixish::$fn\::$fnl";
34 38         90 my $spec = \%{"Data::Unixish::$fn\::SPEC"};
  38         259  
35 38         119 my $meta = $spec->{$fn};
36              
37 38 50       161 $meta or die "BUG: func $fn not found or does not have meta";
38              
39 38         82 my $i = 0;
40             subtest $fn => sub {
41             TEST:
42 38     38   42429 for my $t (@{$args{tests}}) {
  38         185  
43 84         58053 $i++;
44 84   66     516 my $tn = $t->{name} // "test[$i]";
45             subtest $tn => sub {
46 84 100       70791 if ($t->{skip}) {
47 1         6 my $msg = $t->{skip}->();
48 1 50       7 plan skip_all => $msg if $msg;
49             }
50              
51             # test func
52 83 50       324 if ($t->{skip_func}) {
53 0         0 diag "func test skipped";
54             } else {
55             subtest "func" => sub {
56 83         68199 my $in = $t->{in};
57 83         200 my $out = $t->{out};
58 83         177 my $rout = [];
59 83         152 my $res;
60 83         135 eval { $res = $f->(in=>$in,out=>$rout,%{$t->{args}}) };
  83         217  
  83         668  
61 83         249 my $err = $@;
62 83 100 66     720 if ($t->{func_dies} // $t->{dies} // 0) {
      50        
63 3         16 ok($err, "dies");
64 3         1309 return;
65             } else {
66 80 50       372 ok(!$err, "doesn't die") or do {
67 0         0 diag "func dies: $err";
68 0         0 return;
69             };
70             }
71 80         34458 is($res->[0], 200, "status");
72 80 100       43693 if ($t->{test_out}) {
73 1         5 $t->{test_out}->($rout);
74             } else {
75 79 50       355 is_deeply($rout, $out, "out")
76             or diag explain $rout;
77             }
78              
79             # if itemfunc, test against each item
80 80 100 66     63286 if ((grep {$_ eq 'itemfunc'} @{$meta->{tags}}) &&
  165         852  
  80         253  
81             ref($in) eq 'ARRAY') {
82 54 100       242 if ($t->{skip_itemfunc}) {
83 3         13 diag "itemfunc test skipped";
84             } else {
85 51         153 my $rout;
86 51         276 $rout = aiduxa([$fn, $t->{args}], $in);
87 51 50       225 if ($t->{test_out}) {
88 0         0 $t->{test_out}->($rout);
89             } else {
90 51 50       204 is_deeply($rout, $out, "out (itemfunc)")
91             or diag explain $rout;
92             }
93             }
94             }
95 83         624 };
96             }
97              
98             # test running through cmdline
99 83 100 100     161339 if ($t->{skip_cli} // 1) {
100             #diag "cli test skipped";
101             } else {
102             subtest cli => sub {
103 3 50       2696 if ($^O =~ /win/i) {
104 0         0 plan skip_all => "run_forked() not available ".
105             "on Windows";
106 0         0 return;
107             }
108 3 50       53 unless (which("dux")) {
109 3         962 plan skip_all => "dux command-line not available, ".
110             "you might want to install App::dux first";
111 0           return;
112             }
113             my $cmd = "dux $fn ".
114             join(" ", map {
115 0           my $v = $t->{args}{$_};
116 0           my $p = $_; $p =~ s/_/-/g;
  0            
117 0 0         ref($v) ?
118             ("--$p-json",
119             shell_quote($json->encode($v))) :
120             ("--$p", shell_quote($v))
121             }
122 0           keys %{ $t->{args} });
  0            
123             #diag "cmd: $cmd";
124             my %runopts = (
125 0           child_stdin => join("", map {"$_\n"} @{ $t->{in} }),
  0            
  0            
126             );
127 0           my $res = run_forked($cmd, \%runopts);
128 0 0 0       if ($t->{cli_dies} // $t->{dies} // 0) {
      0        
129 0           ok($res->{exit_code}, "dies");
130 0           return;
131             } else {
132 0 0         ok(!$res->{exit_code}, "doesn't die") or do {
133 0           diag "dux dies ($res->{exit_code})";
134 0           return;
135             };
136             }
137 0           is_deeply(join("", map {"$_\n"} @{ $t->{out} }),
  0            
138 0           $res->{stdout}, "output");
139             }
140 3         67 }
141 84         651 };
142             }
143 38         407 };
144             }
145              
146             1;
147             # ABSTRACT: Routines to test Data::Unixish
148              
149             __END__