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.572'; # VERSION
7              
8 41     41   25975 use 5.010001;
  41         307  
9 41     41   181 use strict;
  41         60  
  41         829  
10 41     41   191 use warnings;
  41         72  
  41         1151  
11              
12 41     41   16405 use Data::Unixish qw(aiduxa);
  41         99  
  41         2318  
13 41     41   15832 use File::Which qw(which);
  41         35901  
  41         2240  
14 41     41   22916 use IPC::Cmd qw(run_forked);
  41         2033179  
  41         2202  
15 41     41   17731 use JSON::MaybeXS;
  41         262901  
  41         2045  
16 41     41   269 use Module::Load;
  41         80  
  41         242  
17 41     41   16943 use String::ShellQuote;
  41         27445  
  41         2139  
18 41     41   21526 use Test::More 0.96;
  41         2147525  
  41         289  
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   10676 no strict 'refs';
  41         84  
  41         35390  
28              
29 38     38 0 6311 my %args = @_;
30 38         103 my $fn = $args{func};
31 38         79 my $fnl = $fn; $fnl =~ s/.+:://;
  38         92  
32 38         215 load "Data::Unixish::$fn";
33 38         599 my $f = "Data::Unixish::$fn\::$fnl";
34 38         77 my $spec = \%{"Data::Unixish::$fn\::SPEC"};
  38         189  
35 38         103 my $meta = $spec->{$fn};
36              
37 38 50       146 $meta or die "BUG: func $fn not found or does not have meta";
38              
39 38         72 my $i = 0;
40             subtest $fn => sub {
41             TEST:
42 38     38   35964 for my $t (@{$args{tests}}) {
  38         136  
43 84         46322 $i++;
44 84   66     442 my $tn = $t->{name} // "test[$i]";
45             subtest $tn => sub {
46 84 100       57419 if ($t->{skip}) {
47 1         4 my $msg = $t->{skip}->();
48 1 50       7 plan skip_all => $msg if $msg;
49             }
50              
51             # test func
52 83 50       267 if ($t->{skip_func}) {
53 0         0 diag "func test skipped";
54             } else {
55             subtest "func" => sub {
56 83         56665 my $in = $t->{in};
57 83         149 my $out = $t->{out};
58 83         160 my $rout = [];
59 83         127 my $res;
60 83         122 eval { $res = $f->(in=>$in,out=>$rout,%{$t->{args}}) };
  83         147  
  83         604  
61 83         176 my $err = $@;
62 83 100 66     590 if ($t->{func_dies} // $t->{dies} // 0) {
      50        
63 3         11 ok($err, "dies");
64 3         946 return;
65             } else {
66 80 50       289 ok(!$err, "doesn't die") or do {
67 0         0 diag "func dies: $err";
68 0         0 return;
69             };
70             }
71 80         27999 is($res->[0], 200, "status");
72 80 100       26850 if ($t->{test_out}) {
73 1         4 $t->{test_out}->($rout);
74             } else {
75 79 50       295 is_deeply($rout, $out, "out")
76             or diag explain $rout;
77             }
78              
79             # if itemfunc, test against each item
80 80 100 66     51929 if ((grep {$_ eq 'itemfunc'} @{$meta->{tags}}) &&
  165         708  
  80         210  
81             ref($in) eq 'ARRAY') {
82 54 100       220 if ($t->{skip_itemfunc}) {
83 3         12 diag "itemfunc test skipped";
84             } else {
85 51         129 my $rout;
86 51         235 $rout = aiduxa([$fn, $t->{args}], $in);
87 51 50       180 if ($t->{test_out}) {
88 0         0 $t->{test_out}->($rout);
89             } else {
90 51 50       185 is_deeply($rout, $out, "out (itemfunc)")
91             or diag explain $rout;
92             }
93             }
94             }
95 83         532 };
96             }
97              
98             # test running through cmdline
99 83 100 100     131645 if ($t->{skip_cli} // 1) {
100             #diag "cli test skipped";
101             } else {
102             subtest cli => sub {
103 3 50       1966 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       26 unless (which("dux")) {
109 3         693 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         62 }
141 84         535 };
142             }
143 38         394 };
144             }
145              
146             1;
147             # ABSTRACT: Routines to test Data::Unixish
148              
149             __END__