blib/lib/Game/RoyalUr/Variant.pm

filter: Statementstmt 100% 65 / 65 Branchbran 100% 20 / 20 Conditioncond 86.3% 19 / 22 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 100% 17 / 17 Podpod 90.0% 9 / 10 totaltotal 95.2% 139 / 146 CC 23 SCAR 13.5
CC
23
Cov
97%
of
6.0
BUILD
6.0
equals
5.0
CRAP 3.9
« blib/lib/Game/RoyalUr/Bot.pm ↑ Summary blib/lib/Game/RoyalUr/Engine.pm »
1 package Game::RoyalUr::Variant;
2  
3 58 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 58 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 58 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 58 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 58 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 58 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 58 our $VERSION = '0.02';
12  
13 58 my (@FIELDS, %NAMED, %ALLOWED);
14 58 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 58 @FIELDS = qw(route dice zero_rolls safe_rosettes pieces);
16 58 %NAMED = (
17 finkel => { route => 'short', dice => 4, zero_rolls => 0, safe_rosettes => 1, pieces => 7 },
18 masters => { route => 'long', dice => 3, zero_rolls => 4, safe_rosettes => 0, pieces => 7 },
19 );
20 58 %ALLOWED = (
21 route => qr/\A(?:short|long)\z/,
22 dice => qr/\A[34]\z/,
23 zero_rolls => qr/\A[04]\z/,
24 safe_rosettes => qr/\A[01]\z/,
25 pieces => qr/\A[1-7]\z/,
26 );
27 }
28  
29 58 has route => (is => 'ro', default => 'short');
30  
31 58 has dice => (is => 'ro', default => 4);
32  
33 58 has zero_rolls => (is => 'ro', default => 0);
34  
35 58 has safe_rosettes => (is => 'ro', default => 1);
36  
37 58 has pieces => (is => 'ro', default => 7);
38  
39 1549 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
40 1549 my ($self) = @_;
41 1549 for my $field (@FIELDS) {
42 7693 my $value = $self->$field;
43 Carp::croak("Game::RoyalUr::Variant: $field may not be '" . ($value // 'undef') . "'")
44 7693 ▶ unless defined $value && !ref $value && $value =~ $ALLOWED{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /$ALLOWED{$field}/ 24 7669 7693
Condition100%8/8
defined $value and not ref $value and $value =~ /$ALLOWED{$field}/
!ll&&!rl&&r
4207669
defined $value and not ref $value
!ll&&!rl&&r
227689
$value // "undef"
l!l
222
MC/DC100%3/3
defined $value and not ref $value and $value =~ /$ALLOWED{$field}/
defined $value not ref $value $value =~ /$ALLOWED{$field}/
MC/DC100%1/1
$value // "undef"
$value
Truth table100%4/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /$ALLOWED{$field}/
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $value
Aresult
11
00
45 }
46 1525 return;
47 }
48  
49 3 ▶sub fields { @FIELDS }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
50  
51 2430 ▶sub names { sort keys %NAMED }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
52  
53 1500 ▶sub named {
Subroutine named: called CC=5 SCAR=16.1
Pod documented
54 1500 my ($class, $name) = @_;
55 Carp::croak("Game::RoyalUr::Variant: no rule set is called '" . ($name // 'undef')
56 . "'; the names are " . join(', ', $class->names))
57 1500 ▶ unless defined $name && !ref $name && exists $NAMED{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and exists $NAMED{$name} 5 1495 1500
Condition87%7/8
defined $name and not ref $name and exists $NAMED{$name}
!ll&&!rl&&r
141495
defined $name and not ref $name
!ll&&!rl&&r
101499
$name // "undef"
l!l
41
MC/DC66%2/3
defined $name and not ref $name and exists $NAMED{$name}
defined $name not ref $name exists $NAMED{$name}
MC/DC100%1/1
$name // "undef"
$name
Truth table75%3/4
A and not(B) and C
A= defined $nameB= not ref $nameC= exists $NAMED{$name}
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $name
Aresult
11
00
58 1495 return $class->new(%{ $NAMED{$name} });
59 }
60  
61 44 ▶sub custom {
Subroutine custom: called CC=3 SCAR=11.0
Pod documented
62 44 my ($class, %given) = @_;
63 44 for my $key (sort keys %given) {
64 78 ▶ Carp::croak("Game::RoyalUr::Variant: no rule is called '$key'") unless exists $ALLOWED{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ALLOWED{$key} 2 76 78
65 }
66 42 return $class->new(%given);
67 }
68  
69 13098 ▶sub of {
Subroutine of: called CC=6 SCAR=17.9
Pod documented
70 13098 my ($class, $rules) = @_;
71 13098 ▶ return $class->named('finkel') unless defined $rules;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules 135 12963 13098
72 12963 ▶ return $rules if Scalar::Util::blessed($rules) && $rules->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($rules) and $rules->isa($class) 11584 1379 12963
Condition66%2/3
builtin::blessed($rules) and $rules->isa($class)
!ll&&!rl&&r
1379011584
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
73 1379 ▶ return $class->custom(%$rules) if ref $rules eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules eq "HASH" 26 1353 1379
74 1353 ▶ Carp::croak('Game::RoyalUr::Variant: a rule set is a name, a hash reference or a variant') if ref $rules;
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules 1 1352 1353
75 1352 return $class->named($rules);
76 }
77  
78 7097 ▶sub as_hash {
Subroutine as_hash: called CC=1 SCAR=0.0
Pod documented
79 7097 my ($self) = @_;
80 7097 return { map { $_ => $self->$_ } @FIELDS };
81 }
82  
83 5 ▶sub equals {
Subroutine equals: called CC=5 SCAR=16.1
Pod documented
84 5 my ($self, $other) = @_;
85 5 ▶ return 0 unless Scalar::Util::blessed($other) && $other->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($other) and $other->isa(__PACKAGE__) 2 3 5
Condition66%2/3
builtin::blessed($other) and $other->isa(__PACKAGE__)
!ll&&!rl&&r
203
MC/DC50%1/2
builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($other) $other->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
86 3 for my $field (@FIELDS) {
87 11 ▶ return 0 unless $self->$field eq $other->$field;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$field eq $other->$field 2 9 11
88 }
89 1 return 1;
90 }
91  
92 2424 ▶sub name {
Subroutine name: called CC=3 SCAR=11.0
Pod documented
93 2424 my ($self) = @_;
94 2424 for my $name ($self->names) {
95 3284 ▶ return $name unless grep { $self->$_ ne $NAMED{$name}{$_} } @FIELDS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$self->$_ ne $NAMED{$name}{$_};} @FIELDS 2415 869 3284
96 }
97 9 return undef;
98 }
99  
100 6338 ▶sub describe {
Subroutine describe: called CC=1 SCAR=0.0
Pod documented
101 6338 my ($self) = @_;
102 6338 return join ' ', map { $_ . '=' . $self->$_ } @FIELDS;
103 }
104  
105 58 1;
106  
107 __END__
« blib/lib/Game/RoyalUr/Bot.pm ↑ Summary blib/lib/Game/RoyalUr/Engine.pm »