blib/lib/Game/Brandubh/Variant.pm

filter: Statementstmt 100% 101 / 101 Branchbran 97.7% 43 / 44 Conditioncond 96.3% 26 / 27 MC/DCmcdc 94.1% 16 / 17 Subroutinesub 100% 17 / 17 Podpod 88.8% 8 / 9 totaltotal 98.1% 211 / 215 CC 37 SCAR 23.1
CC
37
Cov
99%
__ANON__
20.0
from_string
9.0
named
4.0
CRAP 10.1
« blib/lib/Game/Brandubh/Terminal.pm ↑ Summary blib/lib/Game/Brandubh/Notation.pm »
1 package Game::Brandubh::Variant;
2  
3 45 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 45 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 45 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 45 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 45 our $VERSION = '0.02';
11  
12 45 our (@FIELDS, %DEFAULT, %NAMED);
13 45 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 45 @FIELDS = qw(
15 escape
16 king_everywhere_two
17 king_strong
18 throne_reentry
19 throne_pass
20 repeat
21 ply_cap
22 );
23 45 %DEFAULT = (
24 escape => 'corner',
25 king_everywhere_two => 0,
26 king_strong => 0,
27 throne_reentry => 0,
28 throne_pass => 1,
29 repeat => 3,
30 ply_cap => 400,
31 );
32 45 %NAMED = (
33 brandubh => {},
34 );
35 }
36  
37 45 ▶use constant PLY_CAP_MAX => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 45 has name => (is => 'ro', default => 'brandubh');
40  
41 45 has escape => (is => 'ro', default => 'corner');
42  
43 45 has king_everywhere_two => (is => 'ro', default => 0);
44  
45 45 has king_strong => (is => 'ro', default => 0);
46  
47 45 has throne_reentry => (is => 'ro', default => 0);
48  
49 45 has throne_pass => (is => 'ro', default => 1);
50  
51 45 has repeat => (is => 'ro', default => 3);
52  
53 45 has ply_cap => (is => 'ro', default => 400);
54  
55 45 my $MAKING = 0;
56  
57 2086 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
58 2086 my ($self) = @_;
59 2086 ▶ Carp::croak('Game::Brandubh::Variant: a rule set is made by named, custom or from_string')
Branch: 1 branch
BranchTrueFalseTotal
unless $MAKING 3 2083 2086
60 unless $MAKING;
61 2083 return;
62 }
63  
64 2105 ▶my $checked = sub {
Subroutine __ANON__: called CC=20 SCAR=30.0
65 2105 my ($class, %fields) = @_;
66  
67 2105 for my $field (sort keys %fields) {
68 Carp::croak("Game::Brandubh::Variant: no field is called '$field'")
69 1796 ▶ unless exists $DEFAULT{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Game::Brandubh::Variant::DEFAULT{$field} 6 1790 1796
70 Carp::croak("Game::Brandubh::Variant: $field has no value")
71 1790 ▶ unless defined $fields{$field} && !ref $fields{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields{$field} and not ref $fields{$field} 2 1788 1790
Condition100%3/3
defined $fields{$field} and not ref $fields{$field}
!ll&&!rl&&r
111788
MC/DC100%2/2
defined $fields{$field} and not ref $fields{$field}
defined $fields{$field} not ref $fields{$field}
Truth table100%3/3
A and not(B)
A= defined $fields{$field}B= not ref $fields{$field}
ABresult
0X0
100
111
72 }
73 2097 my %all = (%DEFAULT, %fields);
74  
75 Carp::croak("Game::Brandubh::Variant: escape is 'corner' or 'edge', not '$all{escape}'")
76 2097 ▶ unless $all{escape} eq 'corner' || $all{escape} eq 'edge';
Branch: 1 branch
BranchTrueFalseTotal
unless $all{"escape"} eq "corner" or $all{"escape"} eq "edge" 2 2095 2097
Condition100%3/3
$all{"escape"} eq "corner" or $all{"escape"} eq "edge"
l!l&&r!l&&!r
18712242
MC/DC100%2/2
$all{"escape"} eq "corner" or $all{"escape"} eq "edge"
$all{"escape"} eq "corner" $all{"escape"} eq "edge"
Truth table100%3/3
A or B
A= $all{"escape"} eq "corner"B= $all{"escape"} eq "edge"
ABresult
1X1
011
000
77 2095 for my $flag (qw(king_everywhere_two king_strong throne_reentry throne_pass)) {
78 Carp::croak("Game::Brandubh::Variant: $flag is 0 or 1, not '$all{$flag}'")
79 8378 ▶ unless $all{$flag} =~ /\A[01]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $all{$flag} =~ /\A[01]\z/ 2 8376 8378
80 8376 $all{$flag} += 0;
81 }
82 Carp::croak("Game::Brandubh::Variant: repeat is a whole number from 2 up, not '$all{repeat}'")
83 2093 ▶ unless $all{repeat} =~ /\A[0-9]{1,6}\z/ && $all{repeat} >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $all{"repeat"} =~ /\A[0-9]{1,6}\z/ and $all{"repeat"} >= 2 5 2088 2093
Condition100%3/3
$all{"repeat"} =~ /\A[0-9]{1,6}\z/ and $all{"repeat"} >= 2
!ll&&!rl&&r
232088
MC/DC100%2/2
$all{"repeat"} =~ /\A[0-9]{1,6}\z/ and $all{"repeat"} >= 2
$all{"repeat"} =~ /\A[0-9]{1,6}\z/ $all{"repeat"} >= 2
Truth table100%3/3
A and B
A= $all{"repeat"} =~ /\A[0-9]{1,6}\z/B= $all{"repeat"} >= 2
ABresult
0X0
100
111
84 Carp::croak("Game::Brandubh::Variant: ply_cap is a whole number from 1 to " . PLY_CAP_MAX . ", not '$all{ply_cap}'")
85 2088 ▶ unless $all{ply_cap} =~ /\A[0-9]{1,5}\z/ && $all{ply_cap} >= 1 && $all{ply_cap} <= PLY_CAP_MAX;
Branch: 1 branch
BranchTrueFalseTotal
unless $all{"ply_cap"} =~ /\A[0-9]{1,5}\z/ and $all{"ply_cap"} >= 1 and $all{"ply_cap"} <= 4096 3 2085 2088
Condition100%6/6
$all{"ply_cap"} =~ /\A[0-9]{1,5}\z/ and $all{"ply_cap"} >= 1 and $all{"ply_cap"} <= 4096
!ll&&!rl&&r
212085
$all{"ply_cap"} =~ /\A[0-9]{1,5}\z/ and $all{"ply_cap"} >= 1
!ll&&!rl&&r
112086
MC/DC100%3/3
$all{"ply_cap"} =~ /\A[0-9]{1,5}\z/ and $all{"ply_cap"} >= 1 and $all{"ply_cap"} <= 4096
$all{"ply_cap"} =~ /\A[0-9]{1,5}\z/ $all{"ply_cap"} >= 1 $all{"ply_cap"} <= 4096
Truth table100%4/4
A and B and C
A= $all{"ply_cap"} =~ /\A[0-9]{1,5}\z/B= $all{"ply_cap"} >= 1C= $all{"ply_cap"} <= 4096
ABCresult
0XX0
10X0
1100
1111
86 2085 $all{$_} += 0 for qw(repeat ply_cap);
87  
88 Carp::croak('Game::Brandubh::Variant: king_everywhere_two and king_strong contradict each other')
89 2085 ▶ if $all{king_everywhere_two} && $all{king_strong};
Branch: 1 branch
BranchTrueFalseTotal
if $all{"king_everywhere_two"} and $all{"king_strong"} 2 2083 2085
Condition100%3/3
$all{"king_everywhere_two"} and $all{"king_strong"}
!ll&&!rl&&r
18672162
MC/DC100%2/2
$all{"king_everywhere_two"} and $all{"king_strong"}
$all{"king_everywhere_two"} $all{"king_strong"}
Truth table100%3/3
A and B
A= $all{"king_everywhere_two"}B= $all{"king_strong"}
ABresult
0X0
100
111
90  
91 2083 my $name = 'custom';
92 2083 for my $known (sort keys %NAMED) {
93 2083 my %set = (%DEFAULT, %{ $NAMED{$known} });
94 2083 ▶ next if grep { $set{$_} ne $all{$_} } @FIELDS;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$set{$_} ne $all{$_};} @Game::Brandubh::Variant::FIELDS 1441 642 2083
95 642 $name = $known;
96 642 last;
97 }
98  
99 2083 $MAKING = 1;
100 2083 my $made = eval { $class->new(name => $name, %all) };
101 2083 my $died = $@;
102 2083 $MAKING = 0;
103 2083 ▶ Carp::croak($died) unless $made;
Branch: 1 branch
BranchTrueFalseTotal
unless $made 0 2083 2083
104 2083 return $made;
105 45 };
106  
107 641 ▶sub named {
Subroutine named: called CC=4 SCAR=13.9
Pod documented
108 641 my ($class, $name) = @_;
109 641 ▶ $name = 'brandubh' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 501 140 641
110 Carp::croak("Game::Brandubh::Variant: no rule set is called '$name'")
111 641 ▶ unless !ref $name && exists $NAMED{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $name and exists $Game::Brandubh::Variant::NAMED{$name} 2 639 641
Condition66%2/3
not ref $name and exists $Game::Brandubh::Variant::NAMED{$name}
!ll&&!rl&&r
02639
MC/DC50%1/2
not ref $name and exists $Game::Brandubh::Variant::NAMED{$name}
not ref $name exists $Game::Brandubh::Variant::NAMED{$name}
Truth table66%2/3
not(A) and B
A= not ref $nameB= exists $Game::Brandubh::Variant::NAMED{$name}
ABresult
0X0
100
111
112 639 return $checked->($class, %{ $NAMED{$name} });
113 }
114  
115 1394 ▶sub custom {
Subroutine custom: called CC=2 SCAR=6.9
Pod documented
116 1394 my ($class, @fields) = @_;
117 1394 ▶ Carp::croak('Game::Brandubh::Variant: custom takes field => value pairs') if @fields % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @fields % 2 1 1393 1394
118 1393 return $checked->($class, @fields);
119 }
120  
121 223 ▶sub from_string {
Subroutine from_string: called CC=9 SCAR=22.0
Pod documented
122 223 my ($class, $text) = @_;
123 223 ▶ return undef unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 2 221 223
Condition100%3/3
defined $text and not ref $text
!ll&&!rl&&r
11221
MC/DC100%2/2
defined $text and not ref $text
defined $text not ref $text
Truth table100%3/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
124 221 $text =~ s/\A\s+//;
125 221 $text =~ s/\s+\z//;
126 221 ▶ return eval { $class->named($text) } if exists $NAMED{$text};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Game::Brandubh::Variant::NAMED{$text} 137 84 221
127  
128 84 ▶ my ($list) = $text =~ /\Acustom(?:[ \t]+(\S+))?\z/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my($list) = $text =~ /\Acustom(?:[ \t]+(\S+))?\z/ or return undef 75 9 84
129 75 my %fields;
130 75 ▶ for my $pair (split /,/, defined $list ? $list : '') {
Branch: 1 branch
BranchTrueFalseTotal
defined $list ? : 74 1 75
131 137 ▶ my ($field, $value) = $pair =~ /\A(\w+)=(\w+)\z/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = $pair =~ /\A(\w+)=(\w+)\z/ or return undef 136 1 137
132 136 ▶ return undef if exists $fields{$field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $fields{$field} 1 135 136
133 135 $fields{$field} = $value;
134 }
135 73 return eval { $checked->($class, %fields) };
136 }
137  
138 304 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=6.9
Pod documented
139 304 my ($self) = @_;
140 304 ▶ return $self->name if exists $NAMED{ $self->name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $Game::Brandubh::Variant::NAMED{$self->name} 211 93 304
141 93 my @differ = grep { $self->$_ ne $DEFAULT{$_} } @FIELDS;
142 93 return 'custom ' . join(',', map { "$_=" . $self->$_ } @differ);
143 }
144  
145 2853 ▶sub as_hash {
Subroutine as_hash: called CC=1 SCAR=0.0
Pod documented
146 2853 my ($self) = @_;
147 2853 return { map { $_ => $self->$_ } @FIELDS };
148 }
149  
150 616 ▶sub equals {
Subroutine equals: called CC=4 SCAR=13.9
Pod documented
151 616 my ($self, $other) = @_;
152 616 ▶ return 0 unless ref $other && ref $other eq ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other and ref $other eq ref $self 3 613 616
Condition100%3/3
ref $other and ref $other eq ref $self
!ll&&!rl&&r
21613
MC/DC100%2/2
ref $other and ref $other eq ref $self
ref $other ref $other eq ref $self
Truth table100%3/3
A and B
A= ref $otherB= ref $other eq ref $self
ABresult
0X0
100
111
153 613 ▶ return (grep { $self->$_ ne $other->$_ } @FIELDS) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({$self->$_ ne $other->$_;} @Game::Brandubh::Variant::FIELDS) ? : 2 611 613
154 }
155  
156 2 ▶sub fields { @FIELDS }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
157  
158 1 ▶sub names { sort keys %NAMED }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
159  
160 45 1;
161  
162 __END__
« blib/lib/Game/Brandubh/Terminal.pm ↑ Summary blib/lib/Game/Brandubh/Notation.pm »