Dominoes/Notation.pm

filter: Statementstmt 100% 68 / 68 Branchbran 89.5% 43 / 48 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 94.1% 145 / 154 CC 31 SCAR 19.2
CC
31
Cov
95%
move_text
11.0
parse_move
10.0
to_layout
5.1
CRAP 6.8
« Dominoes/Layout.pm ↑ Summary Dominoes/Rules.pm »
1 package Game::Dominoes::Notation;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Game::Dominoes::Tile;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Game::Dominoes::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 our $VERSION = '0.03';
12 13 our @EXPORT_OK = qw(
13 tile_text parse_tile
14 move_text parse_move
15 to_text from_text to_layout
16 );
17  
18 1 ▶sub tile_text {
Subroutine tile_text: called CC=2 SCAR=7.1
Pod documented
19 1 my ($tile) = @_;
20 1 ▶ die 'Game::Dominoes::Notation: tile_text wants a tile' unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 0 1 1
21 1 return $tile->stringify;
22 }
23  
24 46 ▶sub parse_tile {
Subroutine parse_tile: called CC=4 SCAR=14.2
Pod documented
25 46 my ($text) = @_;
26 46 ▶ die 'Game::Dominoes::Notation: not a tile: '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $text and $text =~ /\A([0-6])-([0-6])\z/ 2 44 46
defined $text ? : 2 0 2
Condition66%2/3
defined $text and $text =~ /\A([0-6])-([0-6])\z/
!ll&&!rl&&r
0244
MC/DC50%1/2
defined $text and $text =~ /\A([0-6])-([0-6])\z/
defined $text $text =~ /\A([0-6])-([0-6])\z/
Truth table66%2/3
A and B
A= defined $textB= $text =~ /\A([0-6])-([0-6])\z/
ABresult
0X0
100
111
27 . (defined $text ? "'$text'" : 'undef')
28 unless defined $text && $text =~ /\A([0-6])-([0-6])\z/;
29 44 return Game::Dominoes::Tile->of($1, $2);
30 }
31  
32 149 ▶sub move_text {
Subroutine move_text: called CC=11 SCAR=24.0
Pod documented
33 149 my ($move) = @_;
34 149 ▶ die 'Game::Dominoes::Notation: move_text wants a move' unless ref $move;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move 0 149 149
35  
36 149 ▶ if (ref $move ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move ne "HASH") 93 56 149
37 93 my $played = $move->tile->stringify . '@' . $move->arm;
38 93 ▶ $played .= '*' if $move->spinner;
Branch: 1 branch
BranchTrueFalseTotal
if $move->spinner 7 86 93
39 93 return $played;
40 }
41  
42 56 ▶ my $kind = $move->{kind} || 'play';
Condition100%2/2
$move->{'kind'} || 'play'
l!l
542
MC/DC100%1/1
$move->{'kind'} || 'play'
$move->{'kind'}
Truth table100%2/2
A
A= $move->{'kind'}
Aresult
11
00
43 56 ▶ return 'P' if $kind eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "pass" 8 48 56
44 48 ▶ return '|' if $kind eq 'hand_end';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "hand_end" 8 40 48
45 40 ▶ if ($kind eq 'draw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "draw") 22 18 40
46 22 ▶ my $n = $move->{count} || 1;
Condition100%2/2
$move->{'count'} || 1
l!l
211
MC/DC100%1/1
$move->{'count'} || 1
$move->{'count'}
Truth table100%2/2
A
A= $move->{'count'}
Aresult
11
00
47 22 ▶ return $n > 1 ? "D$n" : 'D';
Branch: 1 branch
BranchTrueFalseTotal
$n > 1 ? : 8 14 22
48 }
49 18 my $text = $move->{tile}->stringify . '@' . $move->{arm};
50 18 ▶ $text .= '*' if $move->{spinner};
Branch: 1 branch
BranchTrueFalseTotal
if $move->{"spinner"} 5 13 18
51 18 return $text;
52 }
53  
54 60 ▶sub parse_move {
Subroutine parse_move: called CC=10 SCAR=23.0
Pod documented
55 60 my ($token) = @_;
56 60 ▶ die 'Game::Dominoes::Notation: empty move' unless defined $token && length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 2 58 60
Condition100%3/3
defined $token and length $token
!ll&&!rl&&r
1158
MC/DC100%2/2
defined $token and length $token
defined $token length $token
Truth table100%3/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
57  
58 58 ▶ return { kind => 'pass' } if $token eq 'P';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "P" 5 53 58
59 53 ▶ return { kind => 'hand_end' } if $token eq '|';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "|" 3 50 53
60 50 ▶ if ($token =~ /\AD(\d*)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\AD(\d*)\z/) 7 43 50
61 7 ▶ my $n = length $1 ? $1 + 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 5 2 7
62 7 ▶ die "Game::Dominoes::Notation: a draw of $n is not a draw" if $n < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1 1 6 7
63 6 return { kind => 'draw', count => $n };
64 }
65 43 ▶ if ($token =~ /\A([0-6]-[0-6])\@([LRUD])(\*?)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A([0-6]-[0-6])\@([LRUD])(\*?)\z/) 39 4 43
66 return {
67 39 ▶ kind => 'play',
Branch: 1 branch
BranchTrueFalseTotal
$3 ? : 5 34 39
68 tile => parse_tile($1),
69 arm => $2,
70 spinner => $3 ? 1 : 0,
71 };
72 }
73 4 die "Game::Dominoes::Notation: not a move: '$token'";
74 }
75  
76 7 ▶sub to_text {
Subroutine to_text: called CC=2 SCAR=7.0
Pod documented
77 7 my ($moves) = @_;
78 7 ▶ die 'Game::Dominoes::Notation: to_text wants an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $moves eq "ARRAY" 0 7 7
79 unless ref $moves eq 'ARRAY';
80 7 return join ' ', map { move_text($_) } @$moves;
81 }
82  
83 8 ▶sub from_text {
Subroutine from_text: called CC=3 SCAR=11.0
Pod documented
84 8 my ($text) = @_;
85 8 ▶ return [] unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 7 8
86 7 $text =~ s/\A\s+//;
87 7 $text =~ s/\s+\z//;
88 7 ▶ return [] unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 2 5 7
89 5 return [ map { parse_move($_) } split /\s+/, $text ];
90 }
91  
92 1 ▶sub to_layout {
Subroutine to_layout: called CC=5 SCAR=16.3
Pod documented
93 1 my ($text) = @_;
94 1 ▶ my $moves = ref $text eq 'ARRAY' ? $text : from_text($text);
Branch: 1 branch
BranchTrueFalseTotal
ref $text eq 'ARRAY' ? : 0 1 1
95 1 my $layout = Game::Dominoes::Layout->new;
96 1 for my $move (@$moves) {
97 6 ▶ next unless ($move->{kind} || 'play') eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
unless ($move->{"kind"} || "play") eq "play" 2 4 6
Condition50%1/2
$move->{'kind'} || 'play'
l!l
60
MC/DC0%0/1
$move->{'kind'} || 'play'
$move->{'kind'}
Truth table50%1/2
A
A= $move->{'kind'}
Aresult
11
00
98 4 $layout->place($move->{tile}, $move->{arm});
99 }
100 1 return $layout;
101 }
102  
103 13 1;
104  
105 __END__
« Dominoes/Layout.pm ↑ Summary Dominoes/Rules.pm »