blib/lib/Game/Senet/Variant.pm

filter: Statementstmt 100% 81 / 81 Branchbran 92.8% 26 / 28 Conditioncond 54.8% 17 / 31 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 100% 19 / 19 Podpod 90.9% 10 / 11 totaltotal 84.4% 157 / 186 CC 31 SCAR 15.0
CC
31
Cov
89%
named
7.5
BUILD
6.7
of
6.1
CRAP 4.5
« blib/lib/Game/Senet/Result.pm ↑ Summary blib/lib/Game/Senet/Bot.pm »
1 package Game::Senet::Variant;
2  
3 62 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 62 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 62 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 62 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 62 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 62 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 62 ▶use Game::Senet::Engine ();
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 62 our $VERSION = '0.02';
14  
15 62 my (@FIELDS, @IDENTITY, %NAMED, %ALLOWED);
16 62 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 62 @FIELDS = qw(exits pieces blockade protect backward reach26 throw_on_26);
18 62 @IDENTITY = grep { $_ ne 'pieces' } @FIELDS;
19 62 %NAMED = (
20 kendall => { exits => 'kendall', pieces => 5, blockade => 3, protect => 'run',
21 backward => 1, reach26 => 'exact', throw_on_26 => 0 },
22 skyruk => { exits => 'skyruk', pieces => 5, blockade => 2, protect => 'ends',
23 backward => 1, reach26 => 'any', throw_on_26 => 1 },
24 );
25 62 %ALLOWED = (
26 exits => qr/\A(?:kendall|skyruk)\z/,
27 pieces => qr/\A[57]\z/,
28 blockade => qr/\A[023]\z/,
29 protect => qr/\A(?:run|ends|none)\z/,
30 backward => qr/\A[01]\z/,
31 reach26 => qr/\A(?:exact|any)\z/,
32 throw_on_26 => qr/\A[01]\z/,
33 );
34 }
35  
36 62 has exits => (is => 'ro', default => 'kendall');
37  
38 62 has pieces => (is => 'ro', default => 5);
39  
40 62 has blockade => (is => 'ro', default => 3);
41  
42 62 has protect => (is => 'ro', default => 'run');
43  
44 62 has backward => (is => 'ro', default => 1);
45  
46 62 has reach26 => (is => 'ro', default => 'exact');
47  
48 62 has throw_on_26 => (is => 'ro', default => 0);
49  
50 7238 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=19.0
Pod undocumented
51 7238 my ($self) = @_;
52 7238 for my $field (@FIELDS) {
53 50636 my $value = $self->$field;
54 Carp::croak("Game::Senet::Variant: $field may not be '" . ($value // 'undef') . "'")
55 50636 ▶ unless defined $value && !ref $value && $value =~ $ALLOWED{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /$ALLOWED{$field}/ 9 50627 50636
Condition50%4/8
defined $value and not ref $value and $value =~ /$ALLOWED{$field}/
!ll&&!rl&&r
0950627
defined $value and not ref $value
!ll&&!rl&&r
0050636
$value // "undef"
l!l
90
MC/DC33%1/3
defined $value and not ref $value and $value =~ /$ALLOWED{$field}/
defined $value not ref $value $value =~ /$ALLOWED{$field}/
MC/DC0%0/1
$value // "undef"
$value
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /$ALLOWED{$field}/
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $value
Aresult
11
00
56 }
57 7229 return;
58 }
59  
60 7096 ▶sub fields { @FIELDS }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
61  
62 15431 ▶sub names { sort keys %NAMED }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
63  
64 1601 ▶sub named {
Subroutine named: called CC=7 SCAR=20.2
Pod documented
65 1601 my ($class, $name, %override) = @_;
66 Carp::croak("Game::Senet::Variant: no rule set is called '" . ($name // 'undef')
67 . "'; the names are " . join(', ', $class->names))
68 1601 ▶ unless defined $name && !ref $name && exists $NAMED{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and exists $NAMED{$name} 2 1599 1601
Condition50%4/8
defined $name and not ref $name and exists $NAMED{$name}
!ll&&!rl&&r
021599
defined $name and not ref $name
!ll&&!rl&&r
001601
$name // "undef"
l!l
20
MC/DC33%1/3
defined $name and not ref $name and exists $NAMED{$name}
defined $name not ref $name exists $NAMED{$name}
MC/DC0%0/1
$name // "undef"
$name
Truth table50%2/4
A and not(B) and C
A= defined $nameB= not ref $nameC= exists $NAMED{$name}
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $name
Aresult
11
00
69 1599 for my $key (sort keys %override) {
70 106 ▶ Carp::croak("Game::Senet::Variant: a named set takes pieces and nothing else, not '$key'")
Branch: 1 branch
BranchTrueFalseTotal
unless $key eq "pieces" 1 105 106
71 unless $key eq 'pieces';
72 }
73 1598 return $class->new(%{ $NAMED{$name} }, %override);
74 }
75  
76 5641 ▶sub custom {
Subroutine custom: called CC=3 SCAR=11.0
Pod documented
77 5641 my ($class, %given) = @_;
78 5641 for my $key (sort keys %given) {
79 39405 ▶ Carp::croak("Game::Senet::Variant: no rule is called '$key'") unless exists $ALLOWED{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ALLOWED{$key} 1 39404 39405
80 }
81 5640 return $class->new(%{ $NAMED{kendall} }, %given);
82 }
83  
84 89989 ▶sub of {
Subroutine of: called CC=6 SCAR=18.0
Pod documented
85 89989 my ($class, $rules) = @_;
86 89989 ▶ return $class->named('kendall') unless defined $rules;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules 104 89885 89989
87 89885 ▶ return $rules if Scalar::Util::blessed($rules) && $rules->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($rules) and $rules->isa($class) 88501 1384 89885
Condition66%2/3
builtin::blessed($rules) and $rules->isa($class)
!ll&&!rl&&r
1384088501
MC/DC50%1/2
builtin::blessed($rules) and $rules->isa($class)
builtin::blessed($rules) $rules->isa($class)
Truth table66%2/3
A and B
A= builtin::blessed($rules)B= $rules->isa($class)
ABresult
0X0
100
111
88 1384 ▶ return $class->custom(%$rules) if ref $rules eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules eq "HASH" 1 1383 1384
89 1383 ▶ Carp::croak('Game::Senet::Variant: a rule set is a name, a hash reference or a variant') if ref $rules;
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules 0 1383 1383
90 1383 return $class->named($rules);
91 }
92  
93 167851 ▶sub as_hash {
Subroutine as_hash: called CC=1 SCAR=0.0
Pod documented
94 167851 my ($self) = @_;
95 167851 return { map { $_ => $self->$_ } @FIELDS };
96 }
97  
98 2 ▶sub equals {
Subroutine equals: called CC=5 SCAR=16.8
Pod documented
99 2 my ($self, $other) = @_;
100 2 ▶ return 0 unless Scalar::Util::blessed($other) && $other->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($other) and $other->isa(__PACKAGE__) 0 2 2
Condition33%1/3
builtin::blessed($other) and $other->isa(__PACKAGE__)
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($other) $other->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($other)B= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
101 2 for my $field (@FIELDS) {
102 9 ▶ return 0 unless $self->$field eq $other->$field;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$field eq $other->$field 1 8 9
103 }
104 1 return 1;
105 }
106  
107 9800 ▶sub name {
Subroutine name: called CC=3 SCAR=11.0
Pod documented
108 9800 my ($self) = @_;
109 9800 for my $name ($self->names) {
110 15079 ▶ return $name unless grep { $self->$_ ne $NAMED{$name}{$_} } @IDENTITY;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$self->$_ ne $NAMED{$name}{$_};} @IDENTITY 9794 5285 15079
111 }
112 6 return undef;
113 }
114  
115 9788 ▶sub describe {
Subroutine describe: called CC=3 SCAR=11.0
Pod documented
116 9788 my ($self) = @_;
117 9788 my $name = $self->name;
118 9788 ▶ return join ' ', map { $_ . '=' . $self->$_ } @FIELDS unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 3 9785 9788
119 9785 ▶ return $self->pieces == 5 ? $name : "$name pieces=" . $self->pieces;
Branch: 1 branch
BranchTrueFalseTotal
$self->pieces == 5 ? : 7215 2570 9785
120 }
121  
122 53 ▶sub exit_for {
Subroutine exit_for: called CC=5 SCAR=16.5
Pod documented
123 53 my ($self, $house, $roll) = @_;
124 53 ▶ Carp::croak('Game::Senet::Variant: an exit is asked of a house from 26 to 30 and a roll from 1 to 5')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $house and $house =~ /\A(?:2[6-9]|30)\z/ and defined $roll and $roll =~ /\A[1-5]\z/ 1 52 53
Condition66%6/9
defined $house and $house =~ /\A(?:2[6-9]|30)\z/ and defined $roll and $roll =~ /\A[1-5]\z/
!ll&&!rl&&r
1052
defined $house and $house =~ /\A(?:2[6-9]|30)\z/ and defined $roll
!ll&&!rl&&r
1052
defined $house and $house =~ /\A(?:2[6-9]|30)\z/
!ll&&!rl&&r
0152
MC/DC25%1/4
defined $house and $house =~ /\A(?:2[6-9]|30)\z/ and defined $roll and $roll =~ /\A[1-5]\z/
defined $house $house =~ /\A(?:2[6-9]|30)\z/ defined $roll $roll =~ /\A[1-5]\z/
Truth table40%2/5
A and B and C and D
A= defined $houseB= $house =~ /\A(?:2[6-9]|30)\z/C= defined $rollD= $roll =~ /\A[1-5]\z/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
125 unless defined $house && $house =~ /\A(?:2[6-9]|30)\z/ && defined $roll && $roll =~ /\A[1-5]\z/;
126 52 return [ @{ Game::Senet::Engine->rules($self->as_hash)->{exits}{$house}{$roll} } ];
127 }
128  
129 62 1;
130  
131 __END__
« blib/lib/Game/Senet/Result.pm ↑ Summary blib/lib/Game/Senet/Bot.pm »