blib/lib/Data/Smile/XS.pm

filter: Statementstmt 88.8% 64 / 72 Branchbran 66.0% 33 / 50 Conditioncond 38.1% 8 / 21 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 71.6% 119 / 166 CC 33 SCAR 22.9
CC
33
Cov
73%
_open_for_read
18.4
dump_smile
11.0
load_smile
9.7
CRAP 9.9
↑ Summary XS.xs »
1 10 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Data::Smile::XS;
6  
7 10 our $AUTHORITY = 'cpan:TOBYINK';
8 10 our $VERSION = '0.001000';
9  
10 10 require XSLoader;
11 10 XSLoader::load( __PACKAGE__, $VERSION );
12  
13 10 ▶use Exporter::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 our @ISA = qw( Exporter::Tiny );
15 10 our @EXPORT_OK = qw( encode_smile decode_smile dump_smile load_smile );
16  
17 24 ▶sub _is_pathish_filename {
Subroutine _is_pathish_filename: called CC=4 SCAR=13.9
18 24 my ( $x ) = @_;
19 24 ▶ return 0 if not ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 5 19 24
20 19 ▶ return 1 if eval { $x->isa( 'Path::Tiny' ) } or eval { $x->isa( 'Path::Class::File' ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $x->isa("Path::Tiny") } } or eval { do { $x->isa("Path::Class::File") } } 18 1 19
Condition66%2/3
eval { do { $x->isa("Path::Tiny") } } or eval { do { $x->isa("Path::Class::File") } }
l!l&&r!l&&!r
1801
MC/DC50%1/2
eval { do { $x->isa("Path::Tiny") } } or eval { do { $x->isa("Path::Class::File") } }
eval { do { $x->isa("Path::Tiny") } } eval { do { $x->isa("Path::Class::File") } }
Truth table66%2/3
A or B
A= eval { do { $x->isa("Path::Tiny") } }B= eval { do { $x->isa("Path::Class::File") } }
ABresult
1X1
011
000
21 1 return 0;
22 }
23  
24 19 ▶sub _open_for_read {
Subroutine _open_for_read: called CC=7 SCAR=29.1
25 19 my ( $file ) = @_;
26  
27 19 ▶ if ( _is_pathish_filename( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_pathish_filename($file)) 17 2 19
28 17 $file = "$file";
29 }
30  
31 19 ▶ if ( not ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 19 0 19
32 19 ▶ open my $fh, '<:raw', $file or die "open($file) for read: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "open($file) for read: $!" 19 0 19
33 19 return ( $fh, 1 );
34 }
35  
36 0 ▶ if ( eval { $file->can( 'read' ) } or eval { $file->can( 'getline' ) } or ref( $file ) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $file->can("read") } } or eval { do { $file->can("getline") } } or ref $file eq "GLOB") 0 0 0
Condition0%0/6
eval { do { $file->can("read") } } or eval { do { $file->can("getline") } } or ref $file eq "GLOB"
l!l&&r!l&&!r
000
eval { do { $file->can("read") } } or eval { do { $file->can("getline") } }
l!l&&r!l&&!r
000
MC/DC0%0/3
eval { do { $file->can("read") } } or eval { do { $file->can("getline") } } or ref $file eq "GLOB"
eval { do { $file->can("read") } } eval { do { $file->can("getline") } } ref $file eq "GLOB"
Truth table0%0/4
A or B or C
A= eval { do { $file->can("read") } }B= eval { do { $file->can("getline") } }C= ref $file eq "GLOB"
ABCresult
1XX1
01X1
0011
0000
37 0 binmode( $file, ':raw' );
38 0 return ( $file, 0 );
39 }
40  
41 0 die "Unsupported file argument for read";
42 }
43  
44 5 ▶sub _open_for_write {
Subroutine _open_for_write: called CC=7 SCAR=21.3
45 5 my ( $file ) = @_;
46  
47 5 ▶ if ( _is_pathish_filename( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_pathish_filename($file)) 1 4 5
48 1 $file = "$file";
49 }
50  
51 5 ▶ if ( not ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 4 1 5
52 4 ▶ open my $fh, '>:raw', $file or die "open($file) for write: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $file or die "open($file) for write: $!" 4 0 4
53 4 return ( $fh, 1 );
54 }
55  
56 0 ▶ if ( eval { $file->can( 'print' ) } or eval { $file->can( 'write' ) } or ref( $file ) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $file->can("print") } } or eval { do { $file->can("write") } } or ref $file eq "GLOB") 1 0 1
Condition33%2/6
eval { do { $file->can("print") } } or eval { do { $file->can("write") } } or ref $file eq "GLOB"
l!l&&r!l&&!r
100
eval { do { $file->can("print") } } or eval { do { $file->can("write") } }
l!l&&r!l&&!r
100
MC/DC0%0/3
eval { do { $file->can("print") } } or eval { do { $file->can("write") } } or ref $file eq "GLOB"
eval { do { $file->can("print") } } eval { do { $file->can("write") } } ref $file eq "GLOB"
Truth table25%1/4
A or B or C
A= eval { do { $file->can("print") } }B= eval { do { $file->can("write") } }C= ref $file eq "GLOB"
ABCresult
1XX1
01X1
0011
0000
57 1 binmode( $file, ':raw' );
58 1 return ( $file, 0 );
59 }
60  
61 0 die "Unsupported file argument for write";
62 }
63  
64 5 ▶sub dump_smile {
Subroutine dump_smile: called CC=10 SCAR=24.0
Pod documented
65 5 my ( $file, $data, $opts ) = @_;
66 5 ▶ die 'dump_smile expects at most 3 arguments' if @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 3 0 5 5
67  
68 5 ▶ if ( @_ >= 3 and defined $opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 3 and defined $opts) 2 3 5
Condition66%2/3
@_ >= 3 and defined $opts
!ll&&!rl&&r
302
MC/DC50%1/2
@_ >= 3 and defined $opts
@_ >= 3 defined $opts
Truth table66%2/3
A and B
A= @_ >= 3B= defined $opts
ABresult
0X0
100
111
69 2 ▶ die 'Options must be a hash reference' if ref( $opts ) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts ne "HASH" 0 2 2
70 }
71  
72 5 my ( $fh, $close ) = _open_for_write( $file );
73  
74 5 my $bytes = encode_smile( $data, $opts );
75 4 ▶ my $ok = eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { print {$fh;} $bytes or die "write: $!"; 1 } } ? : 4 0 4
76 4 ▶ print {$fh} $bytes or die "write: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $bytes or die "write: $!" 4 0 4
77 4 1;
78 } ? 1 : 0;
79  
80 4 ▶ if ( $close ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close) 3 1 4
81 3 ▶ close $fh or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $ok = 0 3 0 3
82 }
83  
84 4 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 4 0 4
85 }
86  
87 19 ▶sub load_smile {
Subroutine load_smile: called CC=9 SCAR=22.7
Pod documented
88 19 my ( $file, $opts ) = @_;
89 19 ▶ die 'load_smile expects at most 2 arguments' if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 19 19
90  
91 19 ▶ if ( @_ >= 2 and defined $opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2 and defined $opts) 1 18 19
Condition66%2/3
@_ >= 2 and defined $opts
!ll&&!rl&&r
1801
MC/DC50%1/2
@_ >= 2 and defined $opts
@_ >= 2 defined $opts
Truth table66%2/3
A and B
A= @_ >= 2B= defined $opts
ABresult
0X0
100
111
92 1 ▶ die 'Options must be a hash reference' if ref( $opts ) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts ne "HASH" 0 1 1
93 }
94  
95 19 my ( $fh, $close ) = _open_for_read( $file );
96  
97 19 my $buf = do { local $/; <$fh> };
98 19 ▶ die "read failed" if not defined $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 19 19
99  
100 19 ▶ if ( $close ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close) 19 0 19
101 19 ▶ close $fh or die "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close: $!" 19 0 19
102 }
103  
104 19 ▶ return defined $opts
Branch: 1 branch
BranchTrueFalseTotal
defined $opts ? : 1 18 19
105 ? decode_smile( $buf, $opts )
106 : decode_smile( $buf );
107 }
108  
109 __END__
↑ Summary XS.xs »