blib/lib/Game/Brandubh/Notation.pm

filter: Statementstmt 100% 137 / 137 Branchbran 99.0% 99 / 100 Conditioncond 87.1% 34 / 39 MC/DCmcdc 80.0% 20 / 25 Subroutinesub 100% 17 / 17 Podpod 100% 10 / 10 totaltotal 96.6% 317 / 328 CC 72 SCAR 22.4
CC
72
Cov
98%
game_parse
18.0
game_string
14.0
move_display
9.0
CRAP 9.4
« blib/lib/Game/Brandubh/Variant.pm ↑ Summary blib/lib/Game/Brandubh.pm »
1 package Game::Brandubh::Notation;
2  
3 46 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 46 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 46 our $VERSION = '0.02';
10  
11 46 our @EXPORT_OK = qw(
12 SETUP
13 square_name square_parse
14 move_wire move_split move_parse move_display
15 position_ok position_cells
16 game_string game_parse
17 );
18 46 our %EXPORT_TAGS = (all => \@EXPORT_OK);
19  
20 46 ▶use constant SETUP => '3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 46 my $SQUARE = qr/[a-g][1-7]/;
23  
24 1013740 ▶sub _plain {
Subroutine _plain: called CC=3 SCAR=11.0
25 1013740 my ($text) = @_;
26 1013740 ▶ return undef if !defined $text || ref $text;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $text or ref $text 45 1013695 1013740
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
3781013695
MC/DC100%2/2
not defined $text or ref $text
not defined $text ref $text
Truth table100%3/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
27 1013695 return "$text";
28 }
29  
30 3033080 ▶sub square_name {
Subroutine square_name: called CC=5 SCAR=16.1
Pod documented
31 3033080 my ($file, $rank) = @_;
32 3033080 ▶ return undef unless defined $file && defined $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $rank 2 3033078 3033080
Condition100%3/3
defined $file and defined $rank
!ll&&!rl&&r
113033078
MC/DC100%2/2
defined $file and defined $rank
defined $file defined $rank
Truth table100%3/3
A and B
A= defined $fileB= defined $rank
ABresult
0X0
100
111
33 3033078 ▶ return undef unless $file =~ /\A[0-6]\z/ && $rank =~ /\A[0-6]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A[0-6]\z/ and $rank =~ /\A[0-6]\z/ 7 3033071 3033078
Condition100%3/3
$file =~ /\A[0-6]\z/ and $rank =~ /\A[0-6]\z/
!ll&&!rl&&r
523033071
MC/DC100%2/2
$file =~ /\A[0-6]\z/ and $rank =~ /\A[0-6]\z/
$file =~ /\A[0-6]\z/ $rank =~ /\A[0-6]\z/
Truth table100%3/3
A and B
A= $file =~ /\A[0-6]\z/B= $rank =~ /\A[0-6]\z/
ABresult
0X0
100
111
34 3033071 return chr(ord('a') + $file) . ($rank + 1);
35 }
36  
37 361982 ▶sub square_parse {
Subroutine square_parse: called CC=3 SCAR=11.0
Pod documented
38 361982 my $text = _plain($_[0]);
39 361982 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 5 361977 361982
40 361977 ▶ my ($f, $r) = lc($text) =~ /\A([a-g])([1-7])\z/ or return;
Branch: 1 branch
BranchTrueFalseTotal
() = lc($text) =~ /\A([a-g])([1-7])\z/ or return 361954 23 361977
41 361954 return (ord($f) - ord('a'), $r - 1);
42 }
43  
44 2425 ▶sub move_wire {
Subroutine move_wire: called CC=5 SCAR=16.1
Pod documented
45 2425 my $from = _plain($_[0]);
46 2425 my $to = _plain($_[1]);
47 2425 ▶ return undef unless defined $from && defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from and defined $to 1 2424 2425
Condition66%2/3
defined $from and defined $to
!ll&&!rl&&r
012424
MC/DC50%1/2
defined $from and defined $to
defined $from defined $to
Truth table66%2/3
A and B
A= defined $fromB= defined $to
ABresult
0X0
100
111
48 2424 ($from, $to) = (lc $from, lc $to);
49 2424 ▶ return undef unless $from =~ /\A$SQUARE\z/ && $to =~ /\A$SQUARE\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $from =~ /\A$SQUARE\z/ and $to =~ /\A$SQUARE\z/ 17 2407 2424
Condition100%3/3
$from =~ /\A$SQUARE\z/ and $to =~ /\A$SQUARE\z/
!ll&&!rl&&r
892407
MC/DC100%2/2
$from =~ /\A$SQUARE\z/ and $to =~ /\A$SQUARE\z/
$from =~ /\A$SQUARE\z/ $to =~ /\A$SQUARE\z/
Truth table100%3/3
A and B
A= $from =~ /\A$SQUARE\z/B= $to =~ /\A$SQUARE\z/
ABresult
0X0
100
111
50 2407 return $from . $to;
51 }
52  
53 416021 ▶sub move_split {
Subroutine move_split: called CC=3 SCAR=11.0
Pod documented
54 416021 my $text = _plain($_[0]);
55 416021 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 3 416018 416021
56 416018 ▶ my ($from, $to) = $text =~ /\A($SQUARE)($SQUARE)\z/ or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $text =~ /\A($SQUARE)($SQUARE)\z/ or return 415962 56 416018
57 415962 return ($from, $to);
58 }
59  
60 224793 ▶sub move_parse {
Subroutine move_parse: called CC=3 SCAR=11.0
Pod documented
61 224793 my $text = _plain($_[0]);
62 224793 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 5 224788 224793
63 224788 $text = lc $text;
64 224788 $text =~ s/\A\s+//;
65 224788 $text =~ s/\s+\z//;
66 224788 ▶ my ($from, $to) = $text =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
() = $text =~ /\A k? ($SQUARE) -? ($SQUARE) (?: x $SQUARE (?: , $SQUARE )* )? (?: \# | \+\+ )? \z/x or return undef 224725 63 224788
67 k?
68 ($SQUARE) -? ($SQUARE)
69 (?: x $SQUARE (?: , $SQUARE )* )?
70 (?: \# | \+\+ )?
71 \z/x or return undef;
72 224725 return $from . $to;
73 }
74  
75 219885 ▶sub move_display {
Subroutine move_display: called CC=9 SCAR=22.0
Pod documented
76 219885 my ($wire, $about) = @_;
77 219885 ▶ my ($from, $to) = move_split($wire) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = move_split($wire) or return undef 219875 10 219885
78 219875 ▶ $about = {} unless ref $about eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $about eq "HASH" 1 219874 219875
79  
80 9469 ▶ my @captures = sort grep { /\A$SQUARE\z/ } map { lc } grep { defined && !ref }
Condition100%3/3
defined $_ && !ref($_)
!ll&&!rl&&r
119469
MC/DC100%2/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table100%3/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
81 219875 ▶ @{ ref $about->{captures} eq 'ARRAY' ? $about->{captures} : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $about->{'captures'} eq 'ARRAY' ? : 219869 6 219875
82  
83 219875 ▶ my $text = ($about->{king} ? 'K' : '') . $from . '-' . $to;
Branch: 1 branch
BranchTrueFalseTotal
$about->{'king'} ? : 27433 192442 219875
84 219875 ▶ $text .= 'x' . join(',', @captures) if @captures;
Branch: 1 branch
BranchTrueFalseTotal
if @captures 9375 210500 219875
85 219875 ▶ if ($about->{king_home}) { $text .= '++' }
Branch: 2 branches
BranchTrueFalseTotal
if ($about->{'king_home'}) { } 1783 218092 219875
elsif ($about->{'king_taken'}) { } 643 217449 218092
86 643 elsif ($about->{king_taken}) { $text .= '#' }
87 219875 return $text;
88 }
89  
90 4872 ▶sub position_ok {
Subroutine position_ok: called CC=7 SCAR=19.5
Pod documented
91 4872 my $text = _plain($_[0]);
92 4872 ▶ return 0 unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 2 4870 4872
93 4870 ▶ return 0 unless $text =~ m{\A ([adk1-7]+ (?: / [adk1-7]+ ){6}) [ ] [ad] \z}x;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ m[\A ([adk1-7]+ (?: / [adk1-7]+ ){6}) [ ] [ad] \z]x 640 4230 4870
94 4230 for my $row (split m{/}, $1) {
95 28562 my $width = 0;
96 28562 ▶ $width += /[1-7]/ ? $_ : 1 for split //, $row;
Branch: 1 branch
BranchTrueFalseTotal
/[1-7]/ ? : 48646 64749 113395
97 28562 ▶ return 0 unless $width == 7;
Branch: 1 branch
BranchTrueFalseTotal
unless $width == 7 333 28229 28562
98 }
99 3897 return 1;
100 }
101  
102 4 ▶sub position_cells {
Subroutine position_cells: called CC=5 SCAR=16.1
Pod documented
103 4 my $text = _plain($_[0]);
104 4 ▶ return unless position_ok($text);
Branch: 1 branch
BranchTrueFalseTotal
unless position_ok($text) 1 3 4
105 3 my ($rows, $side) = split / /, $text;
106 3 my @board;
107 3 for my $row (split m{/}, $rows) {
108 21 my @cells;
109 21 for my $ch (split //, $row) {
110 59 ▶ if ($ch =~ /[1-7]/) { push @cells, ('') x $ch }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[1-7]/) { } 31 28 59
111 28 else { push @cells, $ch }
112 }
113 21 push @board, \@cells;
114 }
115 3 return (\@board, $side);
116 }
117  
118 470 ▶sub game_string {
Subroutine game_string: called CC=14 SCAR=26.4
Pod documented
119 470 my ($game) = @_;
120 470 ▶ return undef unless ref $game eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $game eq "HASH" 1 469 470
121  
122 469 my $variant = _plain($game->{variant});
123 469 ▶ $variant = 'brandubh' unless defined $variant && length $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variant and length $variant 18 451 469
Condition66%2/3
defined $variant and length $variant
!ll&&!rl&&r
180451
MC/DC50%1/2
defined $variant and length $variant
defined $variant length $variant
Truth table66%2/3
A and B
A= defined $variantB= length $variant
ABresult
0X0
100
111
124 469 ▶ return undef if $variant =~ /[\r\n]/ || $variant !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $variant =~ /[\r\n]/ or not $variant =~ /\S/ 1 468 469
Condition66%2/3
$variant =~ /[\r\n]/ or not $variant =~ /\S/
l!l&&r!l&&!r
10468
MC/DC50%1/2
$variant =~ /[\r\n]/ or not $variant =~ /\S/
$variant =~ /[\r\n]/ not $variant =~ /\S/
Truth table66%2/3
A or not(B)
A= $variant =~ /[\r\n]/B= not $variant =~ /\S/
ABresult
1X1
011
000
125  
126 468 my @moves;
127 468 ▶ for my $move (@{ $game->{moves} || [] }) {
Condition50%1/2
$game->{'moves'} || []
l!l
4680
MC/DC0%0/1
$game->{'moves'} || []
$game->{'moves'}
Truth table50%1/2
A
A= $game->{'moves'}
Aresult
11
00
128 8585 ▶ my ($from, $to) = move_split($move) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = move_split($move) or return undef 8576 9 8585
129 8576 push @moves, $from . $to;
130 }
131  
132 459 my @lines = ("variant $variant");
133 459 my $start = _plain($game->{start});
134 459 ▶ if (defined $start && $start ne SETUP) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start and $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a") 202 257 459
Condition100%3/3
defined $start and $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a"
!ll&&!rl&&r
9248202
MC/DC100%2/2
defined $start and $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a"
defined $start $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a"
Truth table100%3/3
A and B
A= defined $startB= $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a"
ABresult
0X0
100
111
135 202 ▶ return undef unless position_ok($start);
Branch: 1 branch
BranchTrueFalseTotal
unless position_ok($start) 1 201 202
136 201 push @lines, "start $start";
137 }
138 458 push @lines, join(' ', 'moves', @moves);
139  
140 458 my $result = $game->{result};
141 458 ▶ if (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) 105 353 458
142 105 my $line = _result_line($result);
143 105 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 3 102 105
144 102 push @lines, "result $line";
145 }
146 455 return join("\n", @lines) . "\n";
147 }
148  
149 105 ▶sub _result_line {
Subroutine _result_line: called CC=8 SCAR=20.8
150 105 my ($result) = @_;
151 105 ▶ return undef unless ref $result eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "HASH" 1 104 105
152 104 ▶ my $how = $result->{how} // '';
Condition50%1/2
$result->{"how"} // ""
l!l
1040
MC/DC0%0/1
$result->{"how"} // ""
$result->{"how"}
Truth table50%1/2
A
A= $result->{"how"}
Aresult
11
00
153 104 ▶ return 'agreed' if $how eq 'agreed';
Branch: 1 branch
BranchTrueFalseTotal
if $how eq "agreed" 33 71 104
154 71 ▶ if ($how eq 'resign') {
Branch: 1 branch
BranchTrueFalseTotal
if ($how eq "resign") 70 1 71
155 70 ▶ my $by = $result->{by} // '';
Condition100%2/2
$result->{"by"} // ""
l!l
691
MC/DC100%1/1
$result->{"by"} // ""
$result->{"by"}
Truth table100%2/2
A
A= $result->{"by"}
Aresult
11
00
156 70 ▶ return "resign $by" if $by eq 'attackers' || $by eq 'defenders';
Branch: 1 branch
BranchTrueFalseTotal
if $by eq "attackers" or $by eq "defenders" 69 1 70
Condition100%3/3
$by eq "attackers" or $by eq "defenders"
l!l&&r!l&&!r
50191
MC/DC100%2/2
$by eq "attackers" or $by eq "defenders"
$by eq "attackers" $by eq "defenders"
Truth table100%3/3
A or B
A= $by eq "attackers"B= $by eq "defenders"
ABresult
1X1
011
000
157 }
158 2 return undef;
159 }
160  
161 290 ▶sub game_parse {
Subroutine game_parse: called CC=18 SCAR=28.9
Pod documented
162 290 my $text = _plain($_[0]);
163 290 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 2 288 290
164  
165 288 my @lines = grep { /\S/ } split /\r?\n/, $text;
166 288 my %game = (start => SETUP, moves => [], result => undef);
167 288 my %seen;
168  
169 288 for my $line (@lines) {
170 720 ▶ my ($word, $rest) = $line =~ /\A(\w+)(?:[ ](.*))?\z/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A(\w+)(?:[ ](.*))?\z/ or return undef 720 0 720
171 720 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 13 707 720
172 720 ▶ return undef if $seen{$word}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$word}++ 1 719 720
173  
174 719 ▶ if ($word eq 'variant') {
Branch: 4 branches
BranchTrueFalseTotal
if ($word eq 'variant') { } 281 438 719
elsif ($word eq 'start') { } 102 336 438
elsif ($word eq 'moves') { } 277 59 336
elsif ($word eq 'result') { } 55 4 59
175 281 ▶ return undef unless $rest =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ /\S/ 1 280 281
176 280 $game{variant} = $rest;
177 }
178 elsif ($word eq 'start') {
179 102 ▶ return undef unless position_ok($rest);
Branch: 1 branch
BranchTrueFalseTotal
unless position_ok($rest) 1 101 102
180 101 $game{start} = $rest;
181 }
182 elsif ($word eq 'moves') {
183 277 for my $move (split ' ', $rest) {
184 5486 ▶ my ($from, $to) = move_split($move) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = move_split($move) or return undef 5474 12 5486
185 5474 push @{ $game{moves} }, $from . $to;
186 }
187 }
188 elsif ($word eq 'result') {
189 55 ▶ if ($rest eq 'agreed') { $game{result} = { how => 'agreed' } }
Branch: 2 branches
BranchTrueFalseTotal
if ($rest eq 'agreed') { } 17 38 55
elsif ($rest =~ /\Aresign (attackers|defenders)\z/) { } 36 2 38
190 36 elsif ($rest =~ /\Aresign (attackers|defenders)\z/) { $game{result} = { how => 'resign', by => $1 } }
191 2 else { return undef }
192 }
193 else {
194 4 return undef;
195 }
196 }
197 267 ▶ return undef unless $seen{variant} && $seen{moves};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{"variant"} and $seen{"moves"} 7 260 267
Condition100%3/3
$seen{"variant"} and $seen{"moves"}
!ll&&!rl&&r
43260
MC/DC100%2/2
$seen{"variant"} and $seen{"moves"}
$seen{"variant"} $seen{"moves"}
Truth table100%3/3
A and B
A= $seen{"variant"}B= $seen{"moves"}
ABresult
0X0
100
111
198 260 return \%game;
199 }
200  
201 46 1;
202  
203 __END__
« blib/lib/Game/Brandubh/Variant.pm ↑ Summary blib/lib/Game/Brandubh.pm »