lib/ChordPro/Songbook.pm

filter: Statementstmt 78.0% 96 / 123 Branchbran 64.7% 22 / 34 Conditioncond 50.0% 24 / 48 MC/DCmcdc 24.0% 6 / 25 Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 5 totaltotal 65.2% 167 / 256 CC 38 SCAR 35.6
CC
38
Cov
69%
parse_file
56.7
embed_file
20.0
structurize
6.0
CRAP 35.1
« blib/lib/ChordPro/Wx/Utils.pm ↑ Summary blib/lib/ChordPro/Wx/RenderDialog_wxg.pm »
1 #!/usr/bin/perl
2  
3 package main;
4  
5 92 our $options;
6 92 our $config;
7  
8 package ChordPro::Songbook;
9  
10 92 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 92 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 92 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 92 ▶use ChordPro;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 92 ▶use ChordPro::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 92 ▶use ChordPro::Files;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 92 ▶use ChordPro::Song;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 92 ▶use ChordPro::Utils qw(progress);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 92 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 92 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 92 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 92 ▶use Ref::Util qw(is_arrayref is_plain_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 92 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 92 ▶my $regtest = defined($ENV{PERL_HASH_SEED}) && $ENV{PERL_HASH_SEED} == 0;
Condition33%1/3
defined $ENV{'PERL_HASH_SEED'} && $ENV{'PERL_HASH_SEED'} == 0
!ll&&!rl&&r
9200
MC/DC0%0/2
defined $ENV{'PERL_HASH_SEED'} && $ENV{'PERL_HASH_SEED'} == 0
defined $ENV{'PERL_HASH_SEED'} $ENV{'PERL_HASH_SEED'} == 0
Truth table33%1/3
A && B
A= defined $ENV{'PERL_HASH_SEED'}B= $ENV{'PERL_HASH_SEED'} == 0
ABresult
0X0
100
111
27  
28 221 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
29 221 my ($pkg) = @_;
30 221 bless { songs => [ ] }, $pkg;
31 }
32  
33 193 ▶sub parse_file {
Subroutine parse_file: called CC=34 SCAR=40.4
Pod undocumented
34 193 my ( $self, $filename, $opts ) = @_;
35 193 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
10489
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
36 193 ▶ my $meta = { %{$config->{meta}}, %{delete $opts->{meta}//{}} };
Condition100%2/2
delete $opts->{"meta"} // {}
l!l
77116
MC/DC100%1/1
delete $opts->{"meta"} // {}
delete $opts->{"meta"}
Truth table100%2/2
A
A= delete $opts->{"meta"}
Aresult
11
00
37 193 ▶ my $defs = { %{delete $opts->{defs}//{}} };
Condition100%2/2
delete $opts->{"defs"} // {}
l!l
77116
MC/DC100%1/1
delete $opts->{"defs"} // {}
delete $opts->{"defs"}
Truth table100%2/2
A
A= delete $opts->{"defs"}
Aresult
11
00
38  
39 # Check for PDF embedding.
40 193 ▶ if ( $filename =~ /\.pdf$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.pdf$/i) 0 193 193
41 0 return $self->embed_file( $filename, $meta, $defs );
42 }
43  
44 # fs_load sets $opts->{_filesource}.
45 193 $opts->{fail} = "soft";
46 193 ▶ my $lines = is_arrayref($filename) ? $filename
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($filename) ? : 18 175 193
47 : fs_load( $filename, $opts );
48 193 ▶ die( $filename, ": ", $opts->{error}, "\n" ) if $opts->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"error"} 0 193 193
49  
50 # Sense crd input and convert if necessary.
51 193 ▶ if ( !(defined($options->{a2crd}) && !$options->{a2crd}) and
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $main::options->{"a2crd"} && !$main::options->{"a2crd"} || $main::options->{"fragment"} and &any(sub { /\S/; } , @$lines) and $main::options->{"crd"} || !&any(sub { /^{\s*\w+/; } , @$lines)) 0 193 193
Condition46%7/15
not defined $main::options->{"a2crd"} && !$main::options->{"a2crd"} || $main::options->{"fragment"} and &any(sub { /\S/; } , @$lines) and $main::options->{"crd"} || !&any(sub { /^{\s*\w+/; } , @$lines)
!ll&&!rl&&r
11920
not defined $main::options->{"a2crd"} && !$main::options->{"a2crd"} || $main::options->{"fragment"} and &any(sub { /\S/; } , @$lines)
!ll&&!rl&&r
01192
defined $main::options->{'a2crd'} && !$main::options->{'a2crd'} || $main::options->{'fragment'}
l!l&&r!l&&!r
00193
defined $main::options->{"a2crd"} and not $main::options->{"a2crd"}
!ll&&!rl&&r
19300
$main::options->{"crd"} or not &any(sub { /^{\s*\w+/; } , @$lines)
l!l&&r!l&&!r
00192
MC/DC0%0/6
not defined $main::options->{"a2crd"} && !$main::options->{"a2crd"} || $main::options->{"fragment"} and &any(sub { /\S/; } , @$lines) and $main::options->{"crd"} || !&any(sub { /^{\s*\w+/; } , @$lines)
defined $main::options->{"a2crd"} not $main::options->{"a2crd"} $main::options->{'fragment'} &any(sub { /\S/; } , @$lines) $main::options->{"crd"} not &any(sub { /^{\s*\w+/; } , @$lines)
Truth table18%2/11
not(A and not(B) || C) and D and E or not(F)
A= defined $main::options->{"a2crd"}B= not $main::options->{"a2crd"}C= $main::options->{'fragment'}D= &any(sub { /\S/; } , @$lines)E= $main::options->{"crd"}F= not &any(sub { /^{\s*\w+/; } , @$lines)
ABCDEFresult
11XXXX0
0X1XXX0
101XXX0
0X00XX0
1000XX0
0X01000
1001000
0X011X1
0X01011
10011X1
1001011
52 !$options->{fragment}
53 192 ▶ and any { /\S/ } @$lines # non-blank lines
Subroutine __ANON__: called CC=1 SCAR=0.0
54 197 ▶ and $options->{crd} || !any { /^{\s*\w+/ } @$lines ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 warn("Converting $filename to ChordPro format\n")
56 0 ▶ if $options->{verbose} || !($options->{a2crd}||$options->{crd});
Branch: 1 branch
BranchTrueFalseTotal
if $main::options->{"verbose"} or not $main::options->{"a2crd"} || $main::options->{"crd"} 0 0 0
Condition0%0/6
$main::options->{"verbose"} or not $main::options->{"a2crd"} || $main::options->{"crd"}
l!l&&r!l&&!r
000
$main::options->{'a2crd'} || $main::options->{'crd'}
l!l&&r!l&&!r
000
MC/DC0%0/3
$main::options->{"verbose"} or not $main::options->{"a2crd"} || $main::options->{"crd"}
$main::options->{"verbose"} $main::options->{'a2crd'} $main::options->{'crd'}
Truth table0%0/4
A or not(B || C)
A= $main::options->{"verbose"}B= $main::options->{'a2crd'}C= $main::options->{'crd'}
ABCresult
1XX1
0001
01X0
0010
57 0 require ChordPro::A2Crd;
58 0 $lines = ChordPro::A2Crd::a2crd( { lines => $lines } );
59 }
60  
61 193 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
1930
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
62  
63 193 for ( "transpose", "transcode" ) {
64 386 ▶ next unless exists $opts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$_} 377 9 386
65 9 $config->{settings}->{$_} = $opts->{$_};
66 }
67 193 for ( "no-substitute", "no-transpose" ) {
68 386 ▶ next unless exists $opts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$_} 385 1 386
69 1 $options->{$_} = $opts->{$_};
70 }
71 193 ▶ bless $config => ChordPro::Config:: if is_plain_hashref($config);
Branch: 1 branch
BranchTrueFalseTotal
if is_plain_hashref($main::config) 0 193 193
72  
73 193 my $linecnt = 0;
74 193 my $songs = 0;
75  
76 193 ▶ while ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 247 193 440
77 my $song = ChordPro::Song->new($opts)
78 ->parse_song( $lines, \$linecnt,
79 247 ▶ { %{dclone($meta)},
Condition100%2/2
$opts->{"bookmark"} //= sprintf("song_%d", 1 + @{$self->{"songs"};})
l!l
73174
MC/DC100%1/1
$opts->{"bookmark"} //= sprintf("song_%d", 1 + @{$self->{"songs"};})
$opts->{"bookmark"}
Truth table100%2/2
A
A= $opts->{"bookmark"}
Aresult
11
00
80 247 "bookmark" => $opts->{bookmark} //= sprintf( "song_%d", 1 + @{ $self->{songs} } ),
81 },
82 { %$defs } );
83  
84 247 $song->{meta}->{songindex} = 1 + @{ $self->{songs} };
85 247 push( @{ $self->{songs} }, $song );
86 247 $songs++;
87  
88 # Copy persistent assets to the songbook.
89 247 ▶ if ( $song->{assets} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($song->{"assets"}) 4 243 247
90 4 ▶ $self->{assets} //= {};
Condition50%1/2
$self->{"assets"} //= {}
l!l
04
MC/DC0%0/1
$self->{"assets"} //= {}
$self->{"assets"}
Truth table50%1/2
A
A= $self->{"assets"}
Aresult
11
00
91 4 ▶ while ( my ($k,$v) = each %{$song->{assets}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$song{"assets"};}) 7 4 11
92 7 ▶ next unless $v->{opts} && $v->{opts}->{persist};
Branch: 1 branch
BranchTrueFalseTotal
unless $v->{"opts"} and $v->{"opts"}{"persist"} 5 2 7
Condition100%3/3
$v->{"opts"} and $v->{"opts"}{"persist"}
!ll&&!rl&&r
232
MC/DC100%2/2
$v->{"opts"} and $v->{"opts"}{"persist"}
$v->{"opts"} $v->{"opts"}{"persist"}
Truth table100%3/3
A and B
A= $v->{"opts"}B= $v->{"opts"}{"persist"}
ABresult
0X0
100
111
93 2 $self->{assets}->{$k} = $v;
94 }
95 }
96 }
97  
98 193 ▶ if ( @{$self->{songs}} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"songs"};} > 1) 21 172 193
99 21 my $song = $self->{songs}->[-1];
100 21 ▶ unless ( $song->{body}
Branch: 1 branch
BranchTrueFalseTotal
unless ($song->{"body"} and &any(sub { $_->{"type"} ne "ignore"; } , @{$$song{"body"};})) 0 21 21
Condition33%1/3
$song->{"body"} and &any(sub { $_->{"type"} ne "ignore"; } , @{$$song{"body"};})
!ll&&!rl&&r
0021
MC/DC0%0/2
$song->{"body"} and &any(sub { $_->{"type"} ne "ignore"; } , @{$$song{"body"};})
$song->{"body"} &any(sub { $_->{"type"} ne "ignore"; } , @{$$song{"body"};})
Truth table33%1/3
A and B
A= $song->{"body"}B= &any(sub { $_->{"type"} ne "ignore"; } , @{$$song{"body"};})
ABresult
0X0
100
111
101 21 ▶ && any { $_->{type} ne "ignore" } @{$song->{body}} ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
102 0 pop( @{ $self->{songs} } );
103 0 $songs--;
104 }
105 }
106  
107 193 ▶ warn("Warning: No songs found in ", $opts->{_filesource}, "\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $songs or $main::running_under_test 0 193 193
Condition66%2/3
$songs or $main::running_under_test
l!l&&r!l&&!r
19210
MC/DC0%0/2
$songs or $main::running_under_test
$songs $main::running_under_test
Truth table66%2/3
A or B
A= $songsB= $main::running_under_test
ABresult
1X1
011
000
108 unless $songs || $::running_under_test;
109  
110 193 return 1;
111 }
112  
113 18 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod undocumented
114 18 my ( $self, $song ) = @_;
115 18 push( @{$self->{songs}}, $song );
116 18 $self;
117 }
118  
119 0 ▶sub embed_file {
Subroutine embed_file: not called CC=4 SCAR=30.0
Pod undocumented
120 0 my ( $self, $filename, $meta, $defs ) = @_;
121  
122 0 ▶ unless ( fs_test( sr => $filename ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (fs_test("sr", $filename)) 0 0 0
123 0 warn("$filename: $! (skipped)\n");
124 0 return;
125 }
126 0 my $type = "pdf";
127  
128 0 my $song = ChordPro::Song->new( { filesource => $filename } );
129 0 $song->{meta}->{songindex} = 1 + @{ $self->{songs} };
130 $song->{source} =
131 0 { file => $filename,
132 line => 1,
133 embedding => $type,
134 };
135 0 ▶ my $title = $defs->{title} // $filename;
Condition0%0/3
$defs->{"title"} // $filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$defs->{"title"} // $filename
$defs->{"title"} $filename
Truth table0%0/3
A // B
A= $defs->{"title"}B= $filename
ABresult
1X1
011
000
136 0 $song->{title} = $title;
137 0 $song->{meta}->{title} = [ $title ];
138 0 push( @{ $self->{songs} }, $song );
139 0 ▶ $song->dump(0) if $config->{debug}->{song};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"song"} 0 0 0
140 0 return 1;
141 }
142  
143 # Used by HTML backend.
144 0 ▶sub structurize {
Subroutine structurize: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my ( $self ) = @_;
146  
147 0 foreach my $song ( @{ $self->{songs} } ) {
148 0 $song->structurize;
149 }
150 }
151  
152 92 1;
« blib/lib/ChordPro/Wx/Utils.pm ↑ Summary blib/lib/ChordPro/Wx/RenderDialog_wxg.pm »