blib/lib/App/sdif/Util.pm

filter: Statementstmt 73.4% 72 / 98 Branchbran 56.8% 25 / 44 Conditioncond 58.8% 10 / 17 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 73.6% 14 / 19 Podpod 0.0% 0 / 9 totaltotal 63.5% 124 / 195 CC 29 SCAR 24.6
CC
29
Cov
67%
read_unified
24.8
read_until
12.0
nth_re
6.0
CRAP 11.7
« blib/lib/App/cdif/Command/Mecab.pm ↑ Summary blib/lib/App/watchdiff.pm »
1 package App::sdif::Util;
2  
3 25 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 our @EXPORT = qw(
10 &read_line &read_until
11 &read_unified_sub &read_unified &read_unified_2
12 &range &terminal_width
13 );
14  
15 25 our @EXPORT_OK = qw(
16 &read_unified_3
17 );
18  
19 25 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 25 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 25 our $MINILLA_CHANGES = 1;
23 25 our $NO_WARNINGS = 0;
24  
25 13 ▶sub read_line ($$;@) {
Subroutine read_line: called CC=4 SCAR=13.9
Pod undocumented
26 13 local *FH = shift;
27 13 my $c = shift;
28 13 my @buf = @_;
29 13 ▶ while ($c--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c--) 42 13 55
30 42 ▶ last if eof FH;
Branch: 1 branch
BranchTrueFalseTotal
if eof FH 0 42 42
31 42 push @buf, scalar <FH>;
32 }
33 13 ▶ wantarray ? @buf : join '', @buf;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 3 13
34 }
35  
36 0 ▶sub read_until (&$) {
Subroutine read_until: not called CC=3 SCAR=24.8
Pod undocumented
37 0 my($sub, $fh) = @_;
38 0 my @lines;
39 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
40 0 push @lines, $_;
41 0 ▶ return @lines if &$sub;
Branch: 1 branch
BranchTrueFalseTotal
if &$sub 0 0 0
42 }
43 0 (@lines, undef);
44 }
45  
46 10 ▶sub read_unified_2 {
Subroutine read_unified_2: called CC=1 SCAR=0.0
Pod undocumented
47 map {
48 10 [ $_->collect(qr/[\t ]/) ], # common
49 [ $_->collect('-') ], # old
50 [ $_->collect('+') ], # new
51 } &read_unified;
52 }
53  
54 0 ▶sub nth_re {
Subroutine nth_re: not called CC=2 SCAR=17.9
Pod undocumented
55 0 state @regex;
56 0 my $n = shift;
57 0 ▶ $regex[$n] //= do {
Condition0%0/3
$regex[$n] //= do { my $regex = sprintf("^(?:.{%d}-|(?=.*\\+).{%d}[ ])", $n, $n); qr/$regex/u }
l!l&&r!l&&!r
000
MC/DC0%0/2
$regex[$n] //= do { my $regex = sprintf("^(?:.{%d}-|(?=.*\\+).{%d}[ ])", $n, $n); qr/$regex/u }
$regex[$n] do { my $regex = sprintf("^(?:.{%d}-|(?=.*\\+).{%d}[ ])", $n, $n); qr/$regex/u }
Truth table0%0/3
A //= B
A= $regex[$n]B= do { my $regex = sprintf("^(?:.{%d}-|(?=.*\\+).{%d}[ ])", $n, $n); qr/$regex/u }
ABresult
1X1
011
000
58 0 my $regex = sprintf "^(?:.{%d}-|(?=.*\\+).{%d}[ ])", $n, $n;
59 0 qr/$regex/;
60 };
61 }
62  
63 0 ▶sub read_unified_3 {
Subroutine read_unified_3: not called CC=1 SCAR=6.9
Pod undocumented
64 map {
65 0 [ $_->collect(q/ / ) ], # common
66 [ $_->collect(nth_re(0)) ], # old ^(?:.{0}-|(?=.*\+).{0}[ ])
67 [ $_->collect(nth_re(1)) ], # new ^(?:.{1}-|(?=.*\+).{1}[ ])
68 [ $_->collect(qr/\+/ ) ], # merge
69 } &read_unified;
70 }
71  
72 0 ▶sub read_unified_sub {
Subroutine read_unified_sub: not called CC=1 SCAR=6.9
Pod undocumented
73 0 my $column = shift;
74 0 my @re = ( ' ' x ($column - 1),
75 map(nth_re($_), 0 .. $column - 2),
76 qr/\+/ );
77 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 map {
79 0 my $ent = $_;
80 0 map { [ $ent->collect($_) ] } @re;
81 } &read_unified;
82 }
83 0 }
84  
85 26 ▶sub read_unified {
Subroutine read_unified: called CC=18 SCAR=32.1
Pod undocumented
86 # Option: prefix, ORDER, NOWARN
87 26 ▶ my $opt = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 26 0 26
88 26 my $FH = shift;
89 26 my $column = @_;
90 26 my $total = sum @_;
91 26 ▶ my $prefix = $opt->{prefix} // '';
Condition100%2/2
$opt->{"prefix"} // ""
l!l
1214
MC/DC100%1/1
$opt->{"prefix"} // ""
$opt->{"prefix"}
Truth table100%2/2
A
A= $opt->{"prefix"}
Aresult
11
00
92  
93 25 ▶ use App::sdif::LabelStack;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 26 my $mark_length = $column - 1;
96 26 my $start_label = ' ' x $mark_length;
97 26 my @lsopt = do {
98 26 map { $_->[0] => $_->[1] }
99 52 grep { $_->[1] }
100 ( [ START => $start_label ],
101 26 [ ORDER => $opt->{ORDER} ] );
102 };
103  
104 343 ▶ state $marklines = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
105 343 local $_ = shift;
106 343 ▶ tr/-/-/ || tr/ / / + 1;
Branch: 1 branch
BranchTrueFalseTotal
tr/\-// or tr/ // + 1 64 279 343
107 26 };
108  
109 26 my @stack = App::sdif::LabelStack->new(@lsopt);
110 26 ▶ while (<$FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FH)) 343 0 343
111 343 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 171 172 343
112 171 ▶ s/^\Q$prefix// or do {
Branch: 1 branch
BranchTrueFalseTotal
s/^\Q$prefix\E//u or do { warn "Unexpected: $_" unless $App::sdif::Util::NO_WARNINGS } 171 0 171
113 0 ▶ warn "Unexpected: $_" unless $NO_WARNINGS;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::sdif::Util::NO_WARNINGS 0 0 0
114 };
115 171 ▶ if ($MINILLA_CHANGES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::sdif::Util::MINILLA_CHANGES) 171 0 171
116 # Minilla removes single space mark in git commit message.
117 # This is not perfect but mostly works.
118 171 s/^(?![-+ ])/ /;
119 }
120 }
121 343 ▶ /^([-+ ]{$mark_length}|\t)/p or do {
Branch: 1 branch
BranchTrueFalseTotal
/^([-+ ]{$mark_length}|\t)/pu or do { if (/^\\ /u) { [$stack[-1]->lists]->[-1][-1] =~ s/\n\z//u; } else { warn "Unexpected line: $_" unless $App::sdif::Util::NO_WARNINGS; } ; next } 343 0 343
122 # `git diff' produces message like this:
123 # "\ No newline at end of file"
124 0 ▶ if (/^\\ /) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\\ /u) { } 0 0 0
125 0 [$stack[-1]->lists]->[-1][-1] =~ s/\n\z//;
126 } else {
127 0 ▶ warn "Unexpected line: $_" unless $NO_WARNINGS;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::sdif::Util::NO_WARNINGS 0 0 0
128 }
129 0 next;
130 };
131 343 my $mark = $1;
132 343 ▶ if (($mark ne $stack[-1]->lastlabel) and
Branch: 1 branch
BranchTrueFalseTotal
if ($mark ne $stack[-1]->lastlabel and $stack[-1]->exists($mark) || !($stack[-1]->lastlabel =~ /[^+]/u) || $stack[-1]->lastlabel =~ /[+]/u && !($mark =~ /[+]/u)) 30 313 343
Condition66%8/12
$mark ne $stack[-1]->lastlabel and $stack[-1]->exists($mark) || !($stack[-1]->lastlabel =~ /[^+]/u) || $stack[-1]->lastlabel =~ /[+]/u && !($mark =~ /[+]/u)
!ll&&!rl&&r
2456830
$stack[-1]->exists($mark) or not $stack[-1]->lastlabel =~ /[^+]/u or $stack[-1]->lastlabel =~ /[+]/u and not $mark =~ /[+]/u
l!l&&r!l&&!r
30068
$stack[-1]->exists($mark) or not $stack[-1]->lastlabel =~ /[^+]/u
l!l&&r!l&&!r
30068
$stack[-1]->lastlabel =~ /[+]/u and not $mark =~ /[+]/u
!ll&&!rl&&r
6800
MC/DC40%2/5
$mark ne $stack[-1]->lastlabel and $stack[-1]->exists($mark) || !($stack[-1]->lastlabel =~ /[^+]/u) || $stack[-1]->lastlabel =~ /[+]/u && !($mark =~ /[+]/u)
$mark ne $stack[-1]->lastlabel $stack[-1]->exists($mark) not $stack[-1]->lastlabel =~ /[^+]/u $stack[-1]->lastlabel =~ /[+]/u not $mark =~ /[+]/u
Truth table50%3/6
A and B or not(C) or D and not(E)
A= $mark ne $stack[-1]->lastlabelB= $stack[-1]->exists($mark)C= not $stack[-1]->lastlabel =~ /[^+]/uD= $stack[-1]->lastlabel =~ /[+]/uE= not $mark =~ /[+]/u
ABCDEresult
0XXXX0
1000X0
100100
11XXX1
101XX1
100111
133 ($stack[-1]->exists($mark)
134 # all +
135 or $stack[-1]->lastlabel !~ /[^+]/
136 # no + after +
137 or ($stack[-1]->lastlabel =~ /[+]/ and $mark !~ /[+]/))) {
138 30 push @stack, App::sdif::LabelStack->new(@lsopt);
139 }
140 343 $stack[-1]->append($mark, $_);
141 343 ▶ $total -= $mark =~ /^\t/ ? $column : $marklines->($mark);
Branch: 1 branch
BranchTrueFalseTotal
$mark =~ /^\t/u ? : 0 343 343
142 343 ▶ last if $total <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $total <= 0 26 317 343
143 }
144 26 @stack;
145 }
146  
147 928 ▶sub range {
Subroutine range: called CC=3 SCAR=11.0
Pod undocumented
148 928 local $_ = shift;
149 928 ▶ my($from, $to) = /,/ ? split(/,/) : ($_, $_);
Branch: 1 branch
BranchTrueFalseTotal
/,/u ? : 66 862 928
150 928 ▶ wantarray ? ($from, $to) : $to - $from + 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 906 22 928
151 }
152  
153 1 ▶sub terminal_width {
Subroutine terminal_width: called CC=3 SCAR=11.4
Pod undocumented
154 25 ▶ use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 my $default = 80;
156 1 my @size;
157 1 ▶ if (open my $tty, ">", "/dev/tty") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $tty, ">", "/dev/tty") 0 1 1
158 # Term::ReadKey 2.31 on macOS 10.15 has a bug in argument handling
159 # and the latest version 2.38 fails to install.
160 # This code should work on both versions.
161 0 @size = GetTerminalSize $tty, $tty;
162 }
163 1 ▶ $size[0] or $default;
Branch: 1 branch
BranchTrueFalseTotal
$size[0] or $default 0 1 1
164 }
165  
166 25 1;
« blib/lib/App/cdif/Command/Mecab.pm ↑ Summary blib/lib/App/watchdiff.pm »