blib/lib/Langertha/Holmgang/Excerpt.pm

filter: Statementstmt 56.7% 114 / 201 Branchbran 45.3% 49 / 108 Conditioncond 34.0% 30 / 88 MC/DCmcdc 20.3% 11 / 54 Subroutinesub 68.1% 15 / 22 Podpod 100% 8 / 8 totaltotal 47.1% 227 / 481 CC 91 SCAR 35.3
CC
91
Cov
49%
source
63.6
source
63.6
_parse
43.2
CRAP 34.0
« blib/lib/Langertha/Holmgang/Sim/Linux/Helm.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Linux/Accounts.pm »
1 package Langertha::Holmgang::Excerpt;
2 # ABSTRACT: Excerpts from a suite's statute texts under quellen/, set into messages
3  
4 36 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 36 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 36 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 36 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 36 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 36 ▶use Langertha::Holmgang::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 36 our $VERSION = '0.001';
14  
15 # the file of an excerpt, relative to the suite directory; a true constant
16 36 my $FILE_RE = qr{\Aquellen/[a-z0-9][a-z0-9-]*\.txt\z};
17  
18 # parsed sources kept at most, by the SHA-256 of their bytes
19 36 my $CACHE_MAX = 16;
20  
21  
22 74 ▶has _cache => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 21 ▶sub _refuse {
Subroutine _refuse: called CC=1 SCAR=1.2
25 0 ▶ my ( $self, $field, $reason ) = @_;
Subroutine _refuse: not called CC=1 SCAR=1.2
26 0 Langertha::Holmgang::Error->throw( $field, $reason );
27 }
28  
29 55 ▶sub source {
Subroutine source: called CC=15 SCAR=41.5
Pod documented
30 0 ▶ my ( $self, $dir, $file, $field ) = @_;
Subroutine source: not called CC=15 SCAR=41.5
Pod documented
31 0 ▶ $self->_refuse( $field, 'path quellen/<name>.txt (lower-case letters, digits, dashes)' )
Branch: 2 branches
BranchTrueFalseTotal
if not defined $file or ref $file or not $file =~ /$FILE_RE/ 4 51 55
if not defined $file or ref $file or not $file =~ /$FILE_RE/ 0 0 0
Condition25%3/12
not defined $file or ref $file or not $file =~ /$FILE_RE/
l!l&&r!l&&!r
0451
not defined $file or ref $file
l!l&&r!l&&!r
0055
not defined $file or ref $file or not $file =~ /$FILE_RE/
l!l&&r!l&&!r
000
not defined $file or ref $file
l!l&&r!l&&!r
000
MC/DC33%1/3
not defined $file or ref $file or not $file =~ /$FILE_RE/
not defined $file ref $file not $file =~ /$FILE_RE/
MC/DC0%0/3
not defined $file or ref $file or not $file =~ /$FILE_RE/
not defined $file ref $file not $file =~ /$FILE_RE/
Truth table50%2/4
not(A) or B or not(C)
A= not defined $fileB= ref $fileC= not $file =~ /$FILE_RE/
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
not(A) or B or not(C)
A= not defined $fileB= ref $fileC= not $file =~ /$FILE_RE/
ABCresult
1XX1
01X1
0011
0000
32 if !defined $file || ref $file || $file !~ $FILE_RE;
33 0 ▶ $self->_refuse( $field, 'an excerpt needs the suite directory to be read from' ) unless defined $dir && length $dir;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $dir and length $dir 1 50 51
unless defined $dir and length $dir 0 0 0
Condition33%2/6
defined $dir and length $dir
!ll&&!rl&&r
1050
defined $dir and length $dir
!ll&&!rl&&r
000
MC/DC50%1/2
defined $dir and length $dir
defined $dir length $dir
MC/DC0%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table66%2/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
34 0 my $root = eval { path($dir)->realpath };
35 0 ▶ $self->_refuse( $field, 'suite directory not readable: '.$dir ) unless $root && $root->is_dir;
Branch: 2 branches
BranchTrueFalseTotal
unless $root and $root->is_dir 0 50 50
unless $root and $root->is_dir 0 0 0
Condition16%1/6
$root and $root->is_dir
!ll&&!rl&&r
0050
$root and $root->is_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$root and $root->is_dir
$root $root->is_dir
MC/DC0%0/2
$root and $root->is_dir
$root $root->is_dir
Truth table33%1/3
A and B
A= $rootB= $root->is_dir
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $rootB= $root->is_dir
ABresult
0X0
100
111
36 0 my $p = path( $root, $file );
37 0 ▶ $self->_refuse( $field, $file.' is missing in the suite' ) unless -e $p;
Branch: 2 branches
BranchTrueFalseTotal
unless -e $p 1 49 50
unless -e $p 0 0 0
38 0 my $real = eval { $p->realpath };
39 0 ▶ $self->_refuse( $field, 'outside the suite directory' ) unless $real && $root->subsumes($real);
Branch: 2 branches
BranchTrueFalseTotal
unless $real and $root->subsumes($real) 1 48 49
unless $real and $root->subsumes($real) 0 0 0
Condition33%2/6
$real and $root->subsumes($real)
!ll&&!rl&&r
0148
$real and $root->subsumes($real)
!ll&&!rl&&r
000
MC/DC50%1/2
$real and $root->subsumes($real)
$real $root->subsumes($real)
MC/DC0%0/2
$real and $root->subsumes($real)
$real $root->subsumes($real)
Truth table66%2/3
A and B
A= $realB= $root->subsumes($real)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $realB= $root->subsumes($real)
ABresult
0X0
100
111
40 0 ▶ $self->_refuse( $field, 'not a plain file' ) unless -f $real;
Branch: 2 branches
BranchTrueFalseTotal
unless -f $real 1 47 48
unless -f $real 0 0 0
41 0 my $raw = eval { $real->slurp_raw };
42 0 ▶ $self->_refuse( $field, 'not readable' ) unless defined $raw;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $raw 0 47 47
unless defined $raw 0 0 0
43 0 my $sha = Digest::SHA::sha256_hex($raw);
44 0 my $c = $self->_cache;
45 0 ▶ my $src = $c->{$sha} //= do {
Condition33%2/6
$c->{$sha} //= do { %$c = () if keys %$c >= $CACHE_MAX; $self->_parse($raw, $file, $field) }
l!l&&r!l&&!r
35120
$$c{$sha} //= do { %$c = () if keys %$c >= $CACHE_MAX; $self->_parse($raw, $file, $field) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{$sha} //= do { %$c = () if keys %$c >= $CACHE_MAX; $self->_parse($raw, $file, $field) }
$c->{$sha} do { %$c = () if keys %$c >= $CACHE_MAX; $self->_parse($raw, $file, $field) }
MC/DC0%0/2
$$c{$sha} //= do { %$c = () if keys %$c >= $CACHE_MAX; $self->_parse($raw, $file, $field) }
$$c{$sha} do { %$c = () if keys %$c >= $CACHE_MAX; $self->_parse($raw, $file, $field) }
Truth table66%2/3
A //= B
A= $c->{$sha}B= do { %$c = () if keys %$c >= $CACHE_MAX; $self->_parse($raw, $file, $field) }
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $$c{$sha}B= do { %$c = () if keys %$c >= $CACHE_MAX; $self->_parse($raw, $file, $field) }
ABresult
1X1
011
000
46 0 ▶ %$c = () if keys %$c >= $CACHE_MAX;
Branch: 2 branches
BranchTrueFalseTotal
if keys %$c >= $CACHE_MAX 0 12 12
if keys %$c >= $CACHE_MAX 0 0 0
47 0 $self->_parse( $raw, $file, $field );
48 };
49 0 return { %$src, path => "$real", sha256 => $sha };
50 }
51  
52  
53 12 ▶sub _parse {
Subroutine _parse: called CC=14 SCAR=37.7
54 0 ▶ my ( $self, $raw, $file, $field ) = @_;
Subroutine _parse: not called CC=14 SCAR=37.7
55 0 my $text = eval { Encode::decode( 'UTF-8', $raw, Encode::FB_CROAK ) };
56 0 ▶ $self->_refuse( $field, $file.': not UTF-8' ) unless defined $text;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $text 1 11 12
unless defined $text 0 0 0
57 0 my ( $headtext, $body ) = split /^---\n/m, $text, 2;
58 0 ▶ $self->_refuse( $field, $file.": head (lines 'Key: value') and '---' missing" ) unless defined $body;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $body 1 10 11
unless defined $body 0 0 0
59 0 my %head;
60 0 for ( split /\n/, $headtext ) {
61 0 ▶ next unless /\S/;
Branch: 2 branches
BranchTrueFalseTotal
unless /\S/ 0 18 18
unless /\S/ 0 0 0
62 0 ▶ /\A([^:]+):\s*(.*)\z/ or $self->_refuse( $field, $file.': head line without a colon' );
Branch: 2 branches
BranchTrueFalseTotal
/\A([^:]+):\s*(.*)\z/ or $self->_refuse($field, $file . ': head line without a colon') 17 1 18
/\A([^:]+):\s*(.*)\z/ or $self->_refuse($field, $file . ': head line without a colon') 0 0 0
63 0 $head{$1} = $2;
64 }
65 0 $body =~ s/\A\n+//;
66 0 my ( @norms, $pending );
67 0 ▶ while ( $body =~ /\G((?:[^\n]+\n)+)(?:\n+|\z)/gc ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body =~ /\G((?:[^\n]+\n)+)(?:\n+|\z)/cg) 36 9 45
if ($body =~ /\G((?:[^\n]+\n)+)(?:\n+|\z)/cg) 0 0 0
68 0 my $block = $1;
69 0 my $at = $-[1];
70 0 my $end = $+[1];
71 0 ▶ if ( $block =~ /\A((?:§|Artikel)\s+\d+[a-z]*)\b([^\n]*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($block =~ /\A((?:\247|Artikel)\s+\d+[a-z]*)\b([^\n]*)/) { } 29 7 36
if ($block =~ /\A((?:\247|Artikel)\s+\d+[a-z]*)\b([^\n]*)/) { } 0 0 0
72 0 ▶ push @norms, { label => $1, title => "$1$2", start => $pending // $at, end => $end };
Condition33%2/6
$pending // $at
l!l&&r!l&&!r
5240
$pending // $at
l!l&&r!l&&!r
000
MC/DC0%0/2
$pending // $at
$pending $at
MC/DC0%0/2
$pending // $at
$pending $at
Truth table66%2/3
A // B
A= $pendingB= $at
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $pendingB= $at
ABresult
1X1
011
000
73 0 undef $pending;
74 }
75 else {
76 0 ▶ $pending //= $at;
Condition33%2/6
$pending //= $at
l!l&&r!l&&!r
250
$pending //= $at
l!l&&r!l&&!r
000
MC/DC0%0/2
$pending //= $at
$pending $at
MC/DC0%0/2
$pending //= $at
$pending $at
Truth table66%2/3
A //= B
A= $pendingB= $at
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $pendingB= $at
ABresult
1X1
011
000
77 }
78 }
79 0 ▶ my $pos = pos($body) // 0;
Condition50%2/4
pos $body // 0
l!l
81
pos $body // 0
l!l
00
MC/DC100%1/1
pos $body // 0
pos $body
MC/DC0%0/1
pos $body // 0
pos $body
Truth table100%2/2
A
A= pos $body
Aresult
11
00
Truth table0%0/2
A
A= pos $body
Aresult
11
00
80 0 ▶ $self->_refuse( $field, $file.': the rest after position '.$pos.' is no block' ) if $pos < length $body;
Branch: 2 branches
BranchTrueFalseTotal
if $pos < length $body 1 8 9
if $pos < length $body 0 0 0
81 0 my %seen;
82 0 ▶ $seen{ $_->{label} }++ and $self->_refuse( $field, $file.': provision '.$_->{label}.' twice' ) for @norms;
Branch: 2 branches
BranchTrueFalseTotal
$seen{$_->{"label"}}++ and $self->_refuse($field, $file . ': provision ' . $_->{"label"} . ' twice') 1 28 29
$seen{$$_{"label"}}++ and $self->_refuse($field, $file . ': provision ' . $$_{"label"} . ' twice') 0 0 0
83 0 return { head => \%head, body => $body, norms => \@norms };
84 }
85  
86 45 ▶sub text {
Subroutine text: called CC=10 SCAR=32.8
Pod documented
87 0 ▶ my ( $self, $spec, $f, $dir ) = @_;
Subroutine text: not called CC=10 SCAR=32.8
Pod documented
88 0 ▶ $self->_refuse( $f, 'auszug: { datei, von, bis } expected' ) unless ref $spec eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $spec eq "HASH" 1 44 45
unless ref $spec eq "HASH" 0 0 0
89 0 my @extra = grep { !/\A(?:datei|von|bis)\z/ } sort keys %$spec;
90 0 ▶ $self->_refuse( $f.'.'.$extra[0], 'unknown key' ) if @extra;
Branch: 2 branches
BranchTrueFalseTotal
if @extra 1 43 44
if @extra 0 0 0
91 0 my $datei = $spec->{datei};
92 0 my $q = $self->source( $dir, $datei, $f.'.datei' );
93 0 my %pos;
94 0 @pos{ map { $_->{label} } @{ $q->{norms} } } = 0 .. $#{ $q->{norms} };
95 0 for my $k (qw( von bis )) {
96 0 my $v = $spec->{$k};
97 0 ▶ $self->_refuse( $f.'.'.$k, 'the label of a provision (such as "§ 305" or "Artikel 28")' ) if !defined $v || ref $v;
Branch: 2 branches
BranchTrueFalseTotal
if not defined $v or ref $v 1 72 73
if not defined $v or ref $v 0 0 0
Condition33%2/6
not defined $v or ref $v
l!l&&r!l&&!r
0172
not defined $v or ref $v
l!l&&r!l&&!r
000
MC/DC50%1/2
not defined $v or ref $v
not defined $v ref $v
MC/DC0%0/2
not defined $v or ref $v
not defined $v ref $v
Truth table66%2/3
not(A) or B
A= not defined $vB= ref $v
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $vB= ref $v
ABresult
1X1
011
000
98 0 ▶ $self->_refuse( $f.'.'.$k, $v.' is not in '.$datei ) unless exists $pos{$v};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $pos{$v} 3 69 72
unless exists $pos{$v} 0 0 0
99 }
100 0 my ( $from_i, $to_i ) = @pos{ $spec->{von}, $spec->{bis} };
101 0 ▶ $self->_refuse( $f.'.bis', $spec->{bis}.' comes before '.$spec->{von} ) if $to_i < $from_i;
Branch: 2 branches
BranchTrueFalseTotal
if $to_i < $from_i 1 32 33
if $to_i < $from_i 0 0 0
102 0 my $n = $q->{norms};
103 0 my $from = $n->[$from_i]{start};
104 0 my $to = $n->[$to_i]{end};
105 0 my $head = $q->{head};
106 return join '', ( $head->{Titel} // $datei ), "\n",
107 'Auszug: '.$spec->{von}.' bis '.$spec->{bis}."\n",
108 ( defined $head->{Stand} ? 'Stand: '.$head->{Stand}."\n" : () ),
109 0 ▶ "\n", substr( $q->{body}, $from, $to - $from );
Branch: 2 branches
BranchTrueFalseTotal
defined $head->{'Stand'} ? : 32 0 32
defined $$head{'Stand'} ? : 0 0 0
Condition16%1/6
$head->{"Titel"} // $datei
l!l&&r!l&&!r
3200
$$head{"Titel"} // $datei
l!l&&r!l&&!r
000
MC/DC0%0/2
$head->{"Titel"} // $datei
$head->{"Titel"} $datei
MC/DC0%0/2
$$head{"Titel"} // $datei
$$head{"Titel"} $datei
Truth table33%1/3
A // B
A= $head->{"Titel"}B= $datei
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $$head{"Titel"}B= $datei
ABresult
1X1
011
000
110 }
111  
112  
113 909 ▶sub expand {
Subroutine expand: called CC=4 SCAR=17.9
Pod documented
114 0 ▶ my ( $self, $v, $f, $dir ) = @_;
Subroutine expand: not called CC=4 SCAR=17.9
Pod documented
115 143 ▶ my $one = sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
116 0 ▶ my ( $p, $pf ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=17.9
117 0 ▶ return $p unless ref $p eq 'HASH' && exists $p->{auszug};
Branch: 2 branches
BranchTrueFalseTotal
unless ref $p eq "HASH" and exists $p->{"auszug"} 109 34 143
unless ref $p eq "HASH" and exists $$p{"auszug"} 0 0 0
Condition50%3/6
ref $p eq "HASH" and exists $p->{"auszug"}
!ll&&!rl&&r
674234
ref $p eq "HASH" and exists $$p{"auszug"}
!ll&&!rl&&r
000
MC/DC100%2/2
ref $p eq "HASH" and exists $p->{"auszug"}
ref $p eq "HASH" exists $p->{"auszug"}
MC/DC0%0/2
ref $p eq "HASH" and exists $$p{"auszug"}
ref $p eq "HASH" exists $$p{"auszug"}
Truth table100%3/3
A and B
A= ref $p eq "HASH"B= exists $p->{"auszug"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $p eq "HASH"B= exists $$p{"auszug"}
ABresult
0X0
100
111
118 0 ▶ $self->_refuse( $pf, 'auszug: { ... } alone, without other keys' ) unless keys %$p == 1;
Branch: 2 branches
BranchTrueFalseTotal
unless keys %$p == 1 1 33 34
unless keys %$p == 1 0 0 0
119 0 return $self->text( $p->{auszug}, $pf.'.auszug', $dir );
120 0 };
121 0 ▶ return [ $one->( $v, $f ) ] if ref $v eq 'HASH' && exists $v->{auszug};
Branch: 2 branches
BranchTrueFalseTotal
if ref $v eq "HASH" and exists $v->{"auszug"} 15 894 909
if ref $v eq "HASH" and exists $$v{"auszug"} 0 0 0
Condition50%3/6
ref $v eq "HASH" and exists $v->{"auszug"}
!ll&&!rl&&r
893115
ref $v eq "HASH" and exists $$v{"auszug"}
!ll&&!rl&&r
000
MC/DC100%2/2
ref $v eq "HASH" and exists $v->{"auszug"}
ref $v eq "HASH" exists $v->{"auszug"}
MC/DC0%0/2
ref $v eq "HASH" and exists $$v{"auszug"}
ref $v eq "HASH" exists $$v{"auszug"}
Truth table100%3/3
A and B
A= ref $v eq "HASH"B= exists $v->{"auszug"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $v eq "HASH"B= exists $$v{"auszug"}
ABresult
0X0
100
111
122 0 ▶ return $v unless ref $v eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 834 60 894
unless ref $v eq "ARRAY" 0 0 0
123 0 return [ map { $one->( $v->[$_], $f.'['.$_.']' ) } 0 .. $#$v ];
124 }
125  
126  
127 141 ▶sub files {
Subroutine files: called CC=4 SCAR=18.7
Pod documented
128 0 ▶ my ( $self, $v ) = @_;
Subroutine files: not called CC=4 SCAR=18.7
Pod documented
129 0 ▶ my @parts = ref $v eq 'ARRAY' ? @$v : ($v);
Branch: 2 branches
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 11 130 141
ref $v eq 'ARRAY' ? : 0 0 0
130 0 return map { $_->{auszug}{datei} }
131 0 ▶ grep { ref $_ eq 'HASH' && ref $_->{auszug} eq 'HASH' && defined $_->{auszug}{datei} } @parts;
Condition41%5/12
ref $_ eq 'HASH' && ref $_->{'auszug'} eq 'HASH' && defined $_->{'auszug'}{'datei'}
!ll&&!rl&&r
15006
ref $_ eq "HASH" and ref $_->{"auszug"} eq "HASH"
!ll&&!rl&&r
14196
ref $_ eq 'HASH' && ref $$_{'auszug'} eq 'HASH' && defined $$_{'auszug'}{'datei'}
!ll&&!rl&&r
000
ref $_ eq "HASH" and ref $$_{"auszug"} eq "HASH"
!ll&&!rl&&r
000
MC/DC66%2/3
ref $_ eq 'HASH' && ref $_->{'auszug'} eq 'HASH' && defined $_->{'auszug'}{'datei'}
ref $_ eq "HASH" ref $_->{"auszug"} eq "HASH" defined $_->{'auszug'}{'datei'}
MC/DC0%0/3
ref $_ eq 'HASH' && ref $$_{'auszug'} eq 'HASH' && defined $$_{'auszug'}{'datei'}
ref $_ eq "HASH" ref $$_{"auszug"} eq "HASH" defined $$_{'auszug'}{'datei'}
Truth table75%3/4
A and B && C
A= ref $_ eq "HASH"B= ref $_->{"auszug"} eq "HASH"C= defined $_->{'auszug'}{'datei'}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B && C
A= ref $_ eq "HASH"B= ref $$_{"auszug"} eq "HASH"C= defined $$_{'auszug'}{'datei'}
ABCresult
0XX0
10X0
1100
1111
132 }
133  
134  
135 36 __PACKAGE__->meta->make_immutable;
136  
137 __END__
« blib/lib/Langertha/Holmgang/Sim/Linux/Helm.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Linux/Accounts.pm »