File Coverage

blib/lib/Data/TableReader/Decoder/Mock.pm
Criterion Covered Total %
statement 56 59 94.9
branch 10 14 71.4
condition 2 8 25.0
subroutine 11 12 91.6
pod 1 1 100.0
total 80 94 85.1


line stmt bran cond sub pod time code
1             package Data::TableReader::Decoder::Mock;
2             $Data::TableReader::Decoder::Mock::VERSION = '0.010';
3 2     2   1473 use Moo 2;
  2         58  
  2         16  
4 2     2   784 use Carp 'croak';
  2         5  
  2         142  
5 2     2   1281 use IO::Handle;
  2         12831  
  2         451  
6              
7             extends 'Data::TableReader::Decoder';
8              
9             # ABSTRACT: Mock decoder for test cases
10              
11              
12             has data => ( is => 'rw' );
13              
14             sub iterator {
15 10     10 1 3632 my $self= shift;
16 10         32 my $data= $self->data;
17 10         20 my $table= $data->[0];
18 10 50       28 my $colmax= $table? scalar(@{$table->[0]})-1 : -1;
  10         25  
19 10 50       30 my $rowmax= $table? $#$table : -1;
20 10         18 my $row= -1;
21             Data::TableReader::Decoder::Mock::_Iter->new(
22             sub {
23 43     43   82 my $slice= shift;
24 43 100       163 return undef unless $row < $rowmax;
25 33         58 ++$row;
26 33         59 my $datarow= $table->[$row];
27 33 100       77 return [ @{$datarow}[@$slice] ] if $slice;
  16         100  
28 17         42 return $datarow;
29             },
30             {
31 10         150 data => $data,
32             table_idx => 0,
33             table_ref => \$table,
34             row_ref => \$row,
35             colmax_ref => \$colmax,
36             rowmax_ref => \$rowmax,
37             origin => [ $table, $row ],
38             }
39             );
40             }
41              
42             # If you need to subclass this iterator, don't. Just implement your own.
43             # i.e. I'm not declaring this implementation stable, yet.
44 2     2   20 use Data::TableReader::Iterator;
  2         5  
  2         98  
45 2     2   773 BEGIN { @Data::TableReader::Decoder::Mock::_Iter::ISA= ('Data::TableReader::Iterator'); }
46              
47             sub Data::TableReader::Decoder::Mock::_Iter::position {
48 19     19   63 my $f= shift->_fields;
49 19         65 'row '.${ $f->{row_ref} };
  19         91  
50             }
51              
52             sub Data::TableReader::Decoder::Mock::_Iter::progress {
53 0     0   0 my $f= shift->_fields;
54 0   0     0 return ${ $f->{row_ref} } / (${ $f->{rowmax_ref} } || 1);
  0         0  
55             }
56              
57             sub Data::TableReader::Decoder::Mock::_Iter::tell {
58 8     8   42 my $f= shift->_fields;
59 8         18 return [ $f->{table_idx}, ${$f->{row_ref}} ];
  8         35  
60             }
61              
62             sub Data::TableReader::Decoder::Mock::_Iter::seek {
63 3     3   20 my ($self, $to)= @_;
64 3         10 my $f= $self->_fields;
65 3   33     21 $to ||= $f->{origin};
66 3         11 my ($table_idx, $row)= @$to;
67 3         7 my $table= $f->{data}[$table_idx];
68 3 50       10 my $colmax= $table? scalar(@{$table->[0]})-1 : -1;
  3         8  
69 3 50       10 my $rowmax= $table? $#$table : -1;
70 3 100       11 $row= -1 unless defined $row;
71 3         7 $f->{table_idx}= $table_idx;
72 3         5 ${$f->{table_ref}}= $table;
  3         8  
73 3         7 ${$f->{row_ref}}= $row;
  3         7  
74 3         6 ${$f->{colmax_ref}}= $colmax;
  3         7  
75 3         6 ${$f->{rowmax_ref}}= $rowmax;
  3         5  
76 3         14 1;
77             }
78              
79             sub Data::TableReader::Decoder::Mock::_Iter::next_dataset {
80 1     1   3 my $self= shift;
81 1         4 my $f= $self->_fields;
82             return defined $f->{data}[ $f->{table_idx}+1 ]
83 1   33     10 && $self->seek([ $f->{table_idx}+1 ]);
84             }
85              
86             1;
87              
88             __END__