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.009';
3 2     2   978 use Moo 2;
  2         32  
  2         11  
4 2     2   549 use Carp 'croak';
  2         3  
  2         109  
5 2     2   939 use IO::Handle;
  2         10316  
  2         378  
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 2760 my $self= shift;
16 10         24 my $data= $self->data;
17 10         16 my $table= $data->[0];
18 10 50       25 my $colmax= $table? scalar(@{$table->[0]})-1 : -1;
  10         21  
19 10 50       21 my $rowmax= $table? $#$table : -1;
20 10         18 my $row= -1;
21             Data::TableReader::Decoder::Mock::_Iter->new(
22             sub {
23 43     43   62 my $slice= shift;
24 43 100       122 return undef unless $row < $rowmax;
25 33         59 ++$row;
26 33         50 my $datarow= $table->[$row];
27 33 100       70 return [ @{$datarow}[@$slice] ] if $slice;
  16         79  
28 17         34 return $datarow;
29             },
30             {
31 10         111 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   13 use Data::TableReader::Iterator;
  2         4  
  2         79  
45 2     2   616 BEGIN { @Data::TableReader::Decoder::Mock::_Iter::ISA= ('Data::TableReader::Iterator'); }
46              
47             sub Data::TableReader::Decoder::Mock::_Iter::position {
48 19     19   57 my $f= shift->_fields;
49 19         29 'row '.${ $f->{row_ref} };
  19         68  
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   33 my $f= shift->_fields;
59 8         14 return [ $f->{table_idx}, ${$f->{row_ref}} ];
  8         24  
60             }
61              
62             sub Data::TableReader::Decoder::Mock::_Iter::seek {
63 3     3   16 my ($self, $to)= @_;
64 3         8 my $f= $self->_fields;
65 3   33     8 $to ||= $f->{origin};
66 3         15 my ($table_idx, $row)= @$to;
67 3         6 my $table= $f->{data}[$table_idx];
68 3 50       8 my $colmax= $table? scalar(@{$table->[0]})-1 : -1;
  3         6  
69 3 50       7 my $rowmax= $table? $#$table : -1;
70 3 100       8 $row= -1 unless defined $row;
71 3         5 $f->{table_idx}= $table_idx;
72 3         4 ${$f->{table_ref}}= $table;
  3         6  
73 3         5 ${$f->{row_ref}}= $row;
  3         5  
74 3         5 ${$f->{colmax_ref}}= $colmax;
  3         4  
75 3         4 ${$f->{rowmax_ref}}= $rowmax;
  3         5  
76 3         11 1;
77             }
78              
79             sub Data::TableReader::Decoder::Mock::_Iter::next_dataset {
80 1     1   3 my $self= shift;
81 1         3 my $f= $self->_fields;
82             return defined $f->{data}[ $f->{table_idx}+1 ]
83 1   33     7 && $self->seek([ $f->{table_idx}+1 ]);
84             }
85              
86             1;
87              
88             __END__