blib/lib/Game/Backgammon/Notation.pm

filter: Statementstmt 100% 55 / 55 Branchbran 96.4% 27 / 28 Conditioncond 86.3% 19 / 22 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 94.5% 122 / 129 CC 25 SCAR 26.0
CC
25
Cov
96%
parse_turn
20.0
_place
6.0
BEGIN
1.0
CRAP 13.4
↑ Summary blib/lib/Game/Backgammon/Terminal.pm »
1 package Game::Backgammon::Notation;
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 ▶use Game::Backgammon::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Game::Backgammon::Turn;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 our $VERSION = '0.05';
11 28 our @EXPORT_OK = qw(parse_turn print_turn);
12  
13 3116 ▶sub _place {
Subroutine _place: called CC=6 SCAR=17.9
14 3116 my ($text) = @_;
15 3116 ▶ return 'bar' if lc $text eq 'bar';
Branch: 1 branch
BranchTrueFalseTotal
if lc $text eq "bar" 99 3017 3116
16 3017 ▶ return 'off' if lc $text eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if lc $text eq "off" 100 2917 3017
17 2917 ▶ return undef unless $text =~ /\A\d+\z/ && $text >= 1 && $text <= 24;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\A\d+\z/ and $text >= 1 and $text <= 24 2 2915 2917
Condition83%5/6
$text =~ /\A\d+\z/ and $text >= 1 and $text <= 24
!ll&&!rl&&r
112915
$text =~ /\A\d+\z/ and $text >= 1
!ll&&!rl&&r
102916
MC/DC66%2/3
$text =~ /\A\d+\z/ and $text >= 1 and $text <= 24
$text =~ /\A\d+\z/ $text >= 1 $text <= 24
Truth table75%3/4
A and B and C
A= $text =~ /\A\d+\z/B= $text >= 1C= $text <= 24
ABCresult
0XX0
10X0
1100
1111
18 2915 return $text + 0;
19 }
20  
21 839 ▶sub parse_turn {
Subroutine parse_turn: called CC=20 SCAR=30.0
Pod documented
22 839 my ($line, %o) = @_;
23 839 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 839 839
24 839 $line =~ s/\A\s+|\s+\z//g;
25  
26 839 my @moves;
27 839 ▶ return Game::Backgammon::Turn->new(player => $o{player}, dice => $o{dice} || [],
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" or lc $line eq "(no play)" 52 787 839
Condition80%4/5
$line eq "" or lc $line eq "(no play)"
l!l&&r!l&&!r
250787
$o{'dice'} || []
l!l
052
MC/DC100%2/2
$line eq "" or lc $line eq "(no play)"
$line eq "" lc $line eq "(no play)"
MC/DC0%0/1
$o{'dice'} || []
$o{'dice'}
Truth table100%3/3
A or B
A= $line eq ""B= lc $line eq "(no play)"
ABresult
1X1
011
000
Truth table50%1/2
A
A= $o{'dice'}
Aresult
11
00
28 moves => [])
29 if $line eq '' || lc $line eq '(no play)';
30  
31 787 for my $group (split /\s+/, $line) {
32 1561 my $times = 1;
33 1561 ▶ if ($group =~ s/\(([^)]*)\)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group =~ s/\(([^)]*)\)\z//) 167 1394 1561
34 167 $times = $1;
35 167 ▶ return _bad("'$group($times)' repeats a move $times times, which is not 1 to 4")
Branch: 1 branch
BranchTrueFalseTotal
unless $times =~ /\A[1-4]\z/ 2 165 167
36 unless $times =~ /\A[1-4]\z/;
37 165 $times += 0;
38 }
39  
40 1559 my @hops = split m{/}, $group, -1;
41 1559 ▶ return _bad("'$group' is not a move") if @hops < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @hops < 2 2 1557 1559
42  
43 1557 my @pair;
44 1557 for my $i (0 .. $#hops - 1) {
45 1558 my ($from_text, $to_text) = ($hops[$i], $hops[$i + 1]);
46 1558 ▶ my $hit = ($to_text =~ s/\*\z//) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$to_text =~ s/\*\z// ? : 120 1438 1558
47 1558 $from_text =~ s/\*\z//;
48 1558 my $from = _place($from_text);
49 1558 my $to = _place($to_text);
50 1558 ▶ return _bad("'$group' names a place that is not a point, the bar or off")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from and defined $to 2 1556 1558
Condition100%3/3
defined $from and defined $to
!ll&&!rl&&r
111556
MC/DC100%2/2
defined $from and defined $to
defined $from defined $to
Truth table100%3/3
A and B
A= defined $fromB= defined $to
ABresult
0X0
100
111
51 unless defined $from && defined $to;
52 1556 ▶ return _bad("'$group' moves from off the board") if $from eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $from eq "off" 1 1555 1556
53 1555 ▶ return _bad("'$group' moves onto the bar") if $to eq 'bar';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "bar" 1 1554 1555
54 1554 ▶ my $die = ($from ne 'bar' && $to ne 'off') ? $from - $to : undef;
Branch: 1 branch
BranchTrueFalseTotal
$from ne 'bar' && $to ne 'off' ? : 1357 197 1554
Condition100%3/3
$from ne "bar" and $to ne "off"
!ll&&!rl&&r
98991357
MC/DC100%2/2
$from ne "bar" and $to ne "off"
$from ne "bar" $to ne "off"
Truth table100%3/3
A and B
A= $from ne "bar"B= $to ne "off"
ABresult
0X0
100
111
55 1554 ▶ return _bad("'$group' moves backwards") if defined $die && $die <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $die and $die <= 0 1 1553 1554
Condition100%3/3
defined $die and $die <= 0
!ll&&!rl&&r
19713561
MC/DC100%2/2
defined $die and $die <= 0
defined $die $die <= 0
Truth table100%3/3
A and B
A= defined $dieB= $die <= 0
ABresult
0X0
100
111
56 push @pair, Game::Backgammon::Move->new(
57 1553 player => $o{player}, from => $from, to => $to, die => $die, hit => $hit);
58 }
59 1552 push @moves, (@pair) x $times;
60 }
61 return Game::Backgammon::Turn->new(player => $o{player},
62 778 ▶ dice => $o{dice} || [], moves => \@moves);
Condition50%1/2
$o{'dice'} || []
l!l
0778
MC/DC0%0/1
$o{'dice'} || []
$o{'dice'}
Truth table50%1/2
A
A= $o{'dice'}
Aresult
11
00
63 }
64  
65 9 ▶sub _bad { $@ = "Game::Backgammon::Notation: $_[0]\n"; return undef }
Subroutine _bad: called CC=1 SCAR=0.0
66  
67 13 ▶sub print_turn { my ($turn) = @_; return $turn->notation }
Subroutine print_turn: called CC=1 SCAR=0.0
Pod documented
68  
69 28 1;
70  
71 __END__
↑ Summary blib/lib/Game/Backgammon/Terminal.pm »