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 Backgammon/Terminal.pm »
1 package Game::Backgammon::Notation;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Game::Backgammon::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Game::Backgammon::Turn;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 our $VERSION = '0.04';
11 10 our @EXPORT_OK = qw(parse_turn print_turn);
12  
13 2980 ▶sub _place {
Subroutine _place: called CC=6 SCAR=17.9
14 2980 my ($text) = @_;
15 2980 ▶ return 'bar' if lc $text eq 'bar';
Branch: 1 branch
BranchTrueFalseTotal
if lc $text eq "bar" 97 2883 2980
16 2883 ▶ return 'off' if lc $text eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if lc $text eq "off" 100 2783 2883
17 2783 ▶ 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 2781 2783
Condition83%5/6
$text =~ /\A\d+\z/ and $text >= 1 and $text <= 24
!ll&&!rl&&r
112781
$text =~ /\A\d+\z/ and $text >= 1
!ll&&!rl&&r
102782
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 2781 return $text + 0;
19 }
20  
21 809 ▶sub parse_turn {
Subroutine parse_turn: called CC=20 SCAR=30.0
Pod documented
22 809 my ($line, %o) = @_;
23 809 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 809 809
24 809 $line =~ s/\A\s+|\s+\z//g;
25  
26 809 my @moves;
27 809 ▶ 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 757 809
Condition80%4/5
$line eq "" or lc $line eq "(no play)"
l!l&&r!l&&!r
250757
$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 757 for my $group (split /\s+/, $line) {
32 1493 my $times = 1;
33 1493 ▶ if ($group =~ s/\(([^)]*)\)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group =~ s/\(([^)]*)\)\z//) 167 1326 1493
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 1491 my @hops = split m{/}, $group, -1;
41 1491 ▶ return _bad("'$group' is not a move") if @hops < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @hops < 2 2 1489 1491
42  
43 1489 my @pair;
44 1489 for my $i (0 .. $#hops - 1) {
45 1490 my ($from_text, $to_text) = ($hops[$i], $hops[$i + 1]);
46 1490 ▶ my $hit = ($to_text =~ s/\*\z//) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$to_text =~ s/\*\z// ? : 115 1375 1490
47 1490 $from_text =~ s/\*\z//;
48 1490 my $from = _place($from_text);
49 1490 my $to = _place($to_text);
50 1490 ▶ 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 1488 1490
Condition100%3/3
defined $from and defined $to
!ll&&!rl&&r
111488
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 1488 ▶ return _bad("'$group' moves from off the board") if $from eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $from eq "off" 1 1487 1488
53 1487 ▶ return _bad("'$group' moves onto the bar") if $to eq 'bar';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "bar" 1 1486 1487
54 1486 ▶ my $die = ($from ne 'bar' && $to ne 'off') ? $from - $to : undef;
Branch: 1 branch
BranchTrueFalseTotal
$from ne 'bar' && $to ne 'off' ? : 1291 195 1486
Condition100%3/3
$from ne "bar" and $to ne "off"
!ll&&!rl&&r
96991291
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 1486 ▶ return _bad("'$group' moves backwards") if defined $die && $die <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $die and $die <= 0 1 1485 1486
Condition100%3/3
defined $die and $die <= 0
!ll&&!rl&&r
19512901
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 1485 player => $o{player}, from => $from, to => $to, die => $die, hit => $hit);
58 }
59 1484 push @moves, (@pair) x $times;
60 }
61 return Game::Backgammon::Turn->new(player => $o{player},
62 748 ▶ dice => $o{dice} || [], moves => \@moves);
Condition50%1/2
$o{'dice'} || []
l!l
0748
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 10 1;
70  
71 __END__
↑ Summary Backgammon/Terminal.pm »