File Coverage

blib/lib/REFECO/Blockchain/Contract/Solidity/ABI/Type/Bytes.pm
Criterion Covered Total %
statement 36 37 97.3
branch 8 10 80.0
condition 1 5 20.0
subroutine 7 7 100.0
pod 0 2 0.0
total 52 61 85.2


line stmt bran cond sub pod time code
1              
2             use strict;
3 4     4   1873 use warnings;
  4         10  
  4         144  
4 4     4   23 no indirect;
  4         8  
  4         97  
5 4     4   17  
  4         6  
  4         19  
6             use Carp;
7 4     4   161 use parent qw(REFECO::Blockchain::Contract::Solidity::ABI::Type);
  4         8  
  4         277  
8 4     4   21  
  4         7  
  4         25  
9             my $self = shift;
10             return $self->encoded if $self->encoded;
11 28     28 0 44 # remove 0x and validates the hexadecimal value
12 28 100       71 croak 'Invalid hexadecimal value ' . $self->data // 'undef'
13             unless $self->data =~ /^(?:0x|0X)?([a-fA-F0-9]+)$/;
14 12 50 0     32 my $hex = $1;
15              
16 12         35 my $data_length = length(pack("H*", $hex));
17             unless ($self->fixed_length) {
18 12         49 # for dynamic length basic types the length must be included
19 12 100       33 $self->push_dynamic($self->encode_length($data_length));
20             $self->push_dynamic($self->pad_right($hex));
21 6         25 } else {
22 6         20 croak "Invalid data length, signature: @{[$self->fixed_length]}, data length: $data_length"
23             if $self->fixed_length && $data_length != $self->fixed_length;
24 6 50 33     14 $self->push_static($self->pad_right($hex));
  0         0  
25             }
26 6         21  
27             return $self->encoded;
28             }
29 12         34  
30             my $self = shift;
31             my @data = $self->data->@*;
32              
33 9     9 0 15 my $hex_data;
34 9         21 my $size = $self->fixed_length;
35             unless ($self->fixed_length) {
36 9         20 $size = hex shift @data;
37 9         19  
38 9 100       19 $hex_data = join('', @data);
39 4         8 } else {
40             $hex_data = $data[0];
41 4         11 }
42              
43 5         11 my $bytes = substr(pack("H*", $hex_data), 0, $size);
44             return sprintf "0x%s", unpack("H*", $bytes);
45             }
46 9         41  
47 9         76 1;
48