blib/lib/MARC/Spec/Structure.pm

filter: Statementstmt 92.3% 132 / 143 Branchbran 82.2% 51 / 62 Conditioncond 71.4% 15 / 21 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 91.3% 21 / 23 Podpod 40.0% 2 / 5 totaltotal 84.5% 225 / 266 SCAR 38.1
CC
42
Cov
88%
_base
11.0
_calculate_length
7.4
__ANON__
6.1
CRAP 45.4
« blib/lib/MARC/Spec/Parser.pm ↑ Summary blib/lib/MARC/Spec.pm »
1 package MARC::Spec::Structure;
2  
3 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 our $VERSION = '1.0.0';
9  
10 13 const my $NO_LENGTH => -1;
11 13 const my $LAST => '#';
12  
13 13 has base => (
14 is => 'rwp',
15 lazy => 1,
16 builder => '_base'
17 );
18  
19 sub _base {
20 122 ▶ my ($self) = @_;
Subroutine _base: called CC=11 SCAR=24.0
21  
22 122 ▶ my $base = ($self->can('tag')) ? $self->tag : '$'.$self->code;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('tag') ? : 81 41 122
23  
24 122 $base .= '['.$self->index_start;
25 122 ▶ if($self->index_start ne $self->index_end) { $base .= '-'.$self->index_end }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->index_start ne $self->index_end) 116 6 122
26 122 $base .= ']';
27  
28 122 ▶ if(defined $self->char_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->char_start) 34 88 122
29 34 my $char_start = $self->char_start;
30 34 my $char_end = $self->char_end;
31 34 ▶ unless($char_start eq '0' && $char_end eq '#') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($char_start eq 0 and $char_end eq "#") 32 2 34
Condition100%3/3
$char_start eq 0 and $char_end eq "#"
!ll&&!rl&&r
21112
MC/DC100%2/2
$char_start eq 0 and $char_end eq "#"
$char_start eq 0 $char_end eq "#"
Truth table100%3/3
A and B
A= $char_start eq 0B= $char_end eq "#"
ABresult
0X0
100
111
32 32 $base .= '/'.$char_start;
33 32 ▶ if($char_end ne $char_start) { $base .= '-'.$char_end }
Branch: 1 branch
BranchTrueFalseTotal
if ($char_end ne $char_start) 8 24 32
34 }
35 }
36 122 ▶ if($self->can('indicator1')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("indicator1")) 81 41 122
37 81 ▶ my $indicators = (defined $self->indicator1) ? $self->indicator1 : '_';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->indicator1 ? : 9 72 81
38 81 ▶ $indicators .= (defined $self->indicator2) ? $self->indicator2 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->indicator2 ? : 9 72 81
39 81 ▶ if($indicators ne '_') { $base .= '_'.$indicators }
Branch: 1 branch
BranchTrueFalseTotal
if ($indicators ne "_") 9 72 81
40 }
41  
42 122 return $base;
43 }
44  
45  
46 has index_start => (
47 is => 'rw',
48 97 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
49 trigger => sub {
50 17 ▶ my ($self, $index_start) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.0
51 17 ▶ if ($LAST ne $self->index_end && $LAST ne $index_start && $self->index_end < $index_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST ne $self->index_end and $LAST ne $index_start and $self->index_end < $index_start) { } 1 16 17
Condition66%4/6
$LAST ne $self->index_end and $LAST ne $index_start and $self->index_end < $index_start
!ll&&!rl&&r
1601
$LAST ne $self->index_end and $LAST ne $index_start
!ll&&!rl&&r
1601
MC/DC33%1/3
$LAST ne $self->index_end and $LAST ne $index_start and $self->index_end < $index_start
$LAST ne $self->index_end $LAST ne $index_start $self->index_end < $index_start
Truth table50%2/4
A and B and C
A= $LAST ne $self->index_endB= $LAST ne $index_startC= $self->index_end < $index_start
ABCresult
0XX0
10X0
1100
1111
52 1 $self->index_end($index_start);
53 } else {
54 16 $self->_set_base( $self->_base() );
55 }
56 17 $self->_set_index_length( _calculate_length($index_start, $self->index_end) )
57 }
58 13 );
59  
60 has index_end => (
61 is => 'rw',
62 97 ▶ default => sub {$LAST},
Subroutine __ANON__: called CC=1 SCAR=0.0
63 trigger => sub {
64 19 ▶ my ($self, $index_end) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.3
65 19 ▶ if ($LAST ne $self->index_start && $LAST ne $index_end && $self->index_start > $index_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST ne $self->index_start and $LAST ne $index_end and $self->index_start > $index_end) { } 0 19 19
Condition83%5/6
$LAST ne $self->index_start and $LAST ne $index_end and $self->index_start > $index_end
!ll&&!rl&&r
1360
$LAST ne $self->index_start and $LAST ne $index_end
!ll&&!rl&&r
2116
MC/DC0%0/3
$LAST ne $self->index_start and $LAST ne $index_end and $self->index_start > $index_end
$LAST ne $self->index_start $LAST ne $index_end $self->index_start > $index_end
Truth table75%3/4
A and B and C
A= $LAST ne $self->index_startB= $LAST ne $index_endC= $self->index_start > $index_end
ABCresult
0XX0
10X0
1100
1111
66 0 $self->index_start($index_end);
67 } else {
68 19 $self->_set_base( $self->_base() );
69 }
70 19 $self->_set_index_length( _calculate_length($self->index_start, $index_end) )
71 }
72 13 );
73
74 has index_length => (
75 is => 'rwp',
76 97 ▶ default => sub {$NO_LENGTH}
Subroutine __ANON__: called CC=1 SCAR=0.0
77 13 );
78  
79 has char_start => (
80 is => 'rw',
81 trigger => sub {
82 11 ▶ my ($self, $char_start) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
83 11 ▶ if(!defined $self->char_end) { $self->char_end($char_start) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->char_end) 10 1 11
84 11 $self->_set_char_pos($self->char_start.'-'.$self->char_end);
85 11 $self->_set_char_length( _calculate_length($char_start, $self->char_end) );
86 11 $self->_set_base( $self->_base() )
87 },
88 13 predicate => 1
89 );
90  
91 has char_end => (
92 is => 'rw',
93 trigger => sub {
94 22 ▶ my ($self, $char_end) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
95 22 ▶ if(!defined $self->char_start) { $self->char_start($char_end) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->char_start) 0 22 22
96 22 $self->_set_char_pos($self->char_start.'-'.$self->char_end);
97 22 $self->_set_char_length( _calculate_length($self->char_start, $char_end) );
98 21 $self->_set_base( $self->_base() )
99 },
100 13 predicate => 1
101 );
102  
103 13 has char_pos => (
104 is => 'rwp',
105 predicate => 1
106 );
107  
108 has char_length => (
109 is => 'rwp',
110 lazy => 1,
111 builder => sub {
112 0 ▶ my ($self) = @_;
Subroutine _build_char_length: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 return _calculate_length($self->char_start, $self->char_end)
114 }
115 13 );
116  
117 has subspecs => (
118 is => 'rwp',
119 isa => sub {
120 67 ▶ foreach my $and (@{$_[0]}) {
Subroutine __ANON__: called CC=6 SCAR=18.1
121 104 ▶ if(ref $and eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $and eq 'ARRAY') { } 59 45 104
122 59 foreach my $or (@{$and}) {
123 118 ▶ croak('Subspec is not an instance of MARC::Spec::Subspec.')
Branch: 1 branch
BranchTrueFalseTotal
if ref $or ne "MARC::Spec::Subspec" 0 118 118
124 if(ref $or ne 'MARC::Spec::Subspec')
125 }
126 } else {
127 45 ▶ croak('Subspec is not an instance of MARC::Spec::Subspec.')
Branch: 1 branch
BranchTrueFalseTotal
if ref $and ne "MARC::Spec::Subspec" 0 45 45
128 if(ref $and ne 'MARC::Spec::Subspec')
129 }
130 }
131 },
132 13 predicate => 1
133 );
134  
135 sub set_index_start_end {
136 13 ▶ my ($self, $indizes) = @_;
Subroutine set_index_start_end: called CC=2 SCAR=7.0
Pod documented
137 13 my @pos = _validate_pos($indizes);
138 13 $self->index_start($pos[0]);
139 13 ▶ defined $pos[1] ? $self->index_end($pos[1]) : $self->index_end($pos[0])
Branch: 1 branch
BranchTrueFalseTotal
defined $pos[1] ? : 13 0 13
140 }
141  
142 sub set_char_start_end {
143 9 ▶ my ($self, $charpos) = @_;
Subroutine set_char_start_end: called CC=2 SCAR=7.0
Pod documented
144 9 my @pos = _validate_pos($charpos);
145 9 $self->char_start($pos[0]);
146 9 ▶ defined $pos[1] ? $self->char_end($pos[1]) : $self->char_end($pos[0])
Branch: 1 branch
BranchTrueFalseTotal
defined $pos[1] ? : 9 0 9
147 }
148  
149 sub add_subspec {
150 34 ▶ my ($self, $subspec) = @_;
Subroutine add_subspec: called CC=2 SCAR=6.9
Pod undocumented
151 34 ▶ if(!$self->has_subspecs) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_subspecs) { } 6 28 34
152 6 $self->_set_subspecs([$subspec]);
153 } else {
154 28 my @subspecs = ( @{$self->subspecs}, $subspec );
155 28 $self->_set_subspecs( \@subspecs )
156 }
157 }
158  
159 sub add_subspecs {
160 33 ▶ my ($self, $subspecs) = @_;
Subroutine add_subspecs: called CC=3 SCAR=11.1
Pod undocumented
161 33 ▶ if (ref $subspecs ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subspecs ne "ARRAY") 0 33 33
162 0 croak('Subspecs is not an ARRAYRef!')
163 }
164 33 ▶ if(!$self->has_subspecs) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_subspecs) { } 29 4 33
165 29 $self->_set_subspecs($subspecs)
166 } else {
167 4 my @merged = @{$self->subspecs};
168 4 push @merged, @{$subspecs};
169 4 $self->_set_subspecs( \@merged )
170 }
171 }
172  
173 sub to_string {
174 21 ▶ my ($self) = @_;
Subroutine to_string: called CC=4 SCAR=13.9
Pod undocumented
175 21 my $string = $self->base;
176 21 ▶ if($self->has_subspecs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_subspecs) 2 19 21
177 2 my @outer = ();
178 2 foreach my $ss (@{$self->subspecs}) {
179 3 ▶ if(ref $ss eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ss eq 'ARRAY') { } 1 2 3
180 1 my $inner = join '|', map {$_->to_string()} @{$ss};
181 1 push @outer, $inner;
182 } else {
183 2 push @outer, $ss->to_string();
184 }
185 }
186 2 my $joined = join '}{', @outer;
187 2 $string .= '{'. $joined .'}';
188 }
189 21 return $string;
190 }
191  
192 sub _calculate_length {
193 69 ▶ my ($start, $end) = @_;
Subroutine _calculate_length: called CC=7 SCAR=20.0
194  
195 # start eq end
196 69 ▶ if ($start eq $end) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($start eq $end) 31 38 69
197  
198 # start = #, end != #
199 38 ▶ if($LAST eq $start && $LAST ne $end) { return $end + 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST eq $start and $LAST ne $end) 0 38 38
Condition33%1/3
$LAST eq $start and $LAST ne $end
!ll&&!rl&&r
3800
MC/DC0%0/2
$LAST eq $start and $LAST ne $end
$LAST eq $start $LAST ne $end
Truth table33%1/3
A and B
A= $LAST eq $startB= $LAST ne $end
ABresult
0X0
100
111
200  
201 # start != #, end = #
202 38 ▶ if($LAST ne $start && $LAST eq $end) { return $NO_LENGTH }
Branch: 1 branch
BranchTrueFalseTotal
if ($LAST ne $start and $LAST eq $end) 29 9 38
Condition66%2/3
$LAST ne $start and $LAST eq $end
!ll&&!rl&&r
0929
MC/DC50%1/2
$LAST ne $start and $LAST eq $end
$LAST ne $start $LAST eq $end
Truth table66%2/3
A and B
A= $LAST ne $startB= $LAST eq $end
ABresult
0X0
100
111
203  
204 9 my $length = $end - $start + 1;
205  
206 9 ▶ if(1 > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 > $length) 1 8 9
207 1 _throw("Ending character or index position must be equal or higher than starting character or index position.", "$start-$end");
208 }
209  
210 8 return $length;
211 }
212  
213 sub _validate_pos {
214 22 ▶ my ($charpos) = @_;
Subroutine _validate_pos: called CC=5 SCAR=17.6
215  
216 22 ▶ if($charpos =~ /[^0-9\-#]/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charpos =~ /[^0-9\-#]/s) 0 22 22
217 0 _throw("Assuming index or character position or range. Only digits, the character '#' and one '-' is allowed.", $charpos);
218 }
219  
220 # something like 123- is not valid
221 22 ▶ if('-' eq substr $charpos, -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ("-" eq substr($charpos, -1)) 0 22 22
222 0 _throw("Assuming index or character range. At least two digits or the character '#' must be present." ,$charpos);
223 }
224  
225 # something like -123 is not valid
226 22 ▶ if('-' eq substr $charpos, 0, 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ("-" eq substr($charpos, 0, 1)) 0 22 22
227 0 _throw("Assuming index or character position or range. First character must not be '-'.", $charpos);
228 }
229  
230 22 my @pos = split /\-/, $charpos, 2;
231  
232 # set end pos to start pos if no end pos
233 22 ▶ if(!defined $pos[1]) { push (@pos, $pos[0]) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos[1]) 6 16 22
234  
235 22 return @pos;
236 }
237  
238 sub _throw {
239 1 ▶ my ($message, $hint) = @_;
Subroutine _throw: called CC=1 SCAR=0.0
240 1 croak 'MARCspec Parser exception. '.$message.' Tried to parse: '.$hint;
241 }
242 13 1;
243 __END__
« blib/lib/MARC/Spec/Parser.pm ↑ Summary blib/lib/MARC/Spec.pm »