File Coverage

blib/lib/Test/Archive/Libarchive.pm
Criterion Covered Total %
statement 92 104 88.4
branch 20 32 62.5
condition 5 8 62.5
subroutine 14 14 100.0
pod 1 1 100.0
total 132 159 83.0


line stmt bran cond sub pod time code
1             package Test::Archive::Libarchive;
2              
3 1     1   237970 use strict;
  1         8  
  1         31  
4 1     1   5 use warnings;
  1         2  
  1         24  
5 1     1   28 use 5.020;
  1         4  
6 1     1   7 use Test2::Tools::Basic qw( diag );
  1         2  
  1         51  
7 1     1   5 use Test2::Tools::Compare qw( is object call T );
  1         2  
  1         71  
8 1     1   8 use Test2::API qw( context release );
  1         2  
  1         75  
9 1     1   525 use Ref::Util qw( is_blessed_ref );
  1         1736  
  1         79  
10 1     1   544 use experimental qw( signatures );
  1         3617  
  1         5  
11 1     1   770 use parent qw( Exporter );
  1         300  
  1         5  
12              
13             # ABSTRACT: Testing tools for Archive::Libarchive
14             our $VERSION = '0.01'; # VERSION
15              
16              
17             our @EXPORT = qw( la_ok la_eof la_warn la_failed la_fatal la_read_data_ok );
18              
19             our %code = (
20             eof => 1,
21             ok => 0,
22             retry => -10,
23             warn => -20,
24             failed => -25,
25             fatal => -30,
26             );
27              
28             sub _ok
29             {
30 25     25   68 my($code, $archive, $method, $arguments, $test_name) = @_;
31              
32 25   33     97 $test_name //= do {
33 25         60 my $name = "\$archive->$method";
34 25 100       64 if(@$arguments)
35             {
36 10         24 my $first = 1;
37 10         20 $name .= '(';
38 10         24 foreach my $arg (@$arguments)
39             {
40 30 100       66 $name .= ", " unless $first;
41 30         59 $first = 0;
42              
43 30         44 my $ref = ref $arg;
44 30 50       68 if($ref eq '')
    0          
    0          
    0          
45             {
46 30 50       77 if(length $arg > 34)
47             {
48 0         0 $name .= "'@{[ substr($arg, 0, 30) =~ s/\n/\\n/rg ]}...'";
  0         0  
49             }
50             else
51             {
52 30         55 $name .= "'@{[ $arg =~ s/\n/\\n/rg ]}'";
  30         121  
53             }
54             }
55             elsif($ref eq 'HASH')
56             {
57 0         0 $name .= "{...}";
58             }
59             elsif($ref eq 'ARRAY')
60             {
61 0         0 $name .= "[...]";
62             }
63             elsif($ref eq 'CODE')
64             {
65 0         0 $name .= "sub {...}";
66             }
67             }
68 10         20 $name .= ')';
69             }
70 25         43 $name .= " == ARCHIVE_@{[ uc $code ]}";
  25         94  
71 25         114 $name;
72             };
73              
74             my $ret = is(
75             $archive,
76             object {
77 25     25   1548 call([ isa => 'Archive::Libarchive::Archive' ] => T());
78 25 100       2291 if(@$arguments)
79             {
80 10         50 call([ $method => @$arguments ] => $code{$code});
81             }
82             else
83             {
84 15         51 call($method => $code{$code});
85             }
86             },
87 25         154 $test_name,
88             );
89              
90 25 100       99020 unless($ret)
91             {
92 15 50       45 if(defined $archive)
93             {
94 15 50       75 if($archive->can('errno'))
95             {
96 0         0 diag("error: @{[ $archive->errno ]}");
  0         0  
97             }
98 15 50       56 if($archive->can('error_string'))
99             {
100 0         0 diag("error: @{[ $archive->error_string ]}");
  0         0  
101             }
102             }
103             else
104             {
105 0         0 diag("archive is not defined");
106             }
107             }
108              
109 25         81 return $ret;
110             }
111              
112              
113             foreach my $code (qw( ok eof retry warn failed fatal )) {
114              
115 25         49 my $sub = sub ($archive, $method, $arguments=[], $test_name=undef)
  25         49  
  25         74  
116 25     25   98254 {
  25         45  
  25         38  
117 25         72 my $ctx = context();
118 25         2202 my $ret = _ok($code, $archive, $method, $arguments, $test_name=undef);
119 25         84 $ctx->release;
120 25         646 return $ret;
121             };
122              
123 1     1   963 no strict 'refs';
  1         2  
  1         436  
124             *{"la_$code"} = $sub;
125             }
126              
127              
128 6         15 sub la_read_data_ok ($r, $test_name=undef)
129 6     6 1 14365 {
  6         14  
  6         9  
130 6         18 my $ctx = context();
131              
132 6   50     611 $test_name ||= "\$archive->read_data(\\\$buffer) >= 0; # multiple calls";
133              
134 6         12 my $content = '';
135              
136 6 100 100     96 unless(is_blessed_ref $r && $r->isa("Archive::Libarchive::ArchiveRead"))
137             {
138 2         9 $ctx->fail_and_release($test_name, "Object is not an instance of Archive::Libarchive::ArchiveRead");
139 2         419 return $content;
140             }
141              
142 4 50       28 unless($r->can('read_data'))
143             {
144 0         0 $ctx->fail_and_release($test_name, "Object does not implement read_data");
145 0         0 return $content;
146             }
147              
148 4         9 my $count = 0;
149              
150 4         8 while(1)
151             {
152 8         13 my $buffer;
153 8         12 $count++;
154 8         21 my $size = $r->read_data(\$buffer);
155 8 100       112 if($size > 0)
    100          
156             {
157 4         10 $content .= $buffer;
158             }
159             elsif($size == 0)
160             {
161 2         6 last;
162             }
163             else
164             {
165 2         12 my %rcode = map { $code{$_} => "ARCHIVE_" . uc($_) } keys %code;
  12         50  
166 2         18 $ctx->fail_and_release($test_name, "Call read_data # $count returned $rcode{$size}");
167 2         483 return $content;
168             }
169             }
170              
171 2         10 $ctx->pass_and_release($test_name);
172 2         385 return $content;
173             }
174              
175             1;
176              
177             __END__