Verify/Freecells.pm

filter: Statementstmt 95.4% 63 / 66 Branchbran 83.3% 15 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 94.4% 102 / 108 CC 13 SCAR 9.2
CC
13
Cov
93%
_input_from_string
5.4
_init
3.0
num_empty
3.0
CRAP 2.5
« Verify/State.pm ↑ Summary Verify/Card.pm »
1 package Games::Solitaire::Verify::Freecells;
2 22 $Games::Solitaire::Verify::Freecells::VERSION = '0.2601';
3 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 22 ▶use parent 'Games::Solitaire::Verify::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 ▶use Games::Solitaire::Verify::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use Games::Solitaire::Verify::Card ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # _s is the string.
13 22 __PACKAGE__->mk_acc_ref(
14 [
15 qw(
16 _count
17 _cells
18 _s
19 )
20 ]
21 );
22  
23  
24 sub _input_from_string
25 272 ▶{
Subroutine _input_from_string: called CC=5 SCAR=16.8
26 272 my $self = shift;
27 272 my $str = shift;
28  
29 272 ▶ if ( $str !~ m{\AFreecells:}gms )
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /\AFreecells:/gms) 0 272 272
30 {
31 0 Games::Solitaire::Verify::Exception::Parse::State::Freecells->throw(
32 error => "Wrong Freecell String", );
33 }
34  
35 POS:
36 272 for my $pos ( 0 .. ( $self->count() - 1 ) )
37 {
38 932 ▶ if ( $str =~ m{\G\z}cg )
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /\G\z/cg) { } 131 801 932
elsif ($str =~ /\G (..)/gms) { } 801 0 801
39 {
40 131 last POS;
41 }
42 elsif ( $str =~ m{\G (..)}gms )
43 {
44 801 my $card_s = $1;
45 801 $self->assign( $pos, $self->_parse_freecell_card($card_s) );
46 }
47 else
48 {
49 0 Games::Solitaire::Verify::Exception::Parse::State::Freecells
50 ->throw( error => "Wrong Freecell String", );
51 }
52 }
53 }
54  
55 sub _init
56 287 ▶{
Subroutine _init: called CC=3 SCAR=11.1
57 287 my ( $self, $args ) = @_;
58  
59 287 ▶ if ( !exists( $args->{count} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"count"}) 0 287 287
60 {
61 0 die "The count was not specified for the freecells";
62 }
63  
64 287 $self->_count( $args->{count} );
65  
66 287 $self->_s( 'Freecells:' . ( ' ' x $self->_count ) );
67  
68 287 $self->_cells( [ (undef) x $self->_count() ] );
69  
70 287 ▶ if ( exists( $args->{string} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"string"}) 272 15 287
71 {
72 272 return $self->_input_from_string( $args->{string} );
73 }
74  
75 15 return ();
76 }
77  
78 sub _parse_freecell_card
79 801 ▶{
Subroutine _parse_freecell_card: called CC=2 SCAR=6.9
80 801 my ( $self, $s ) = @_;
81  
82 return (
83 801 ▶ ( $s eq q{ } )
Branch: 1 branch
BranchTrueFalseTotal
$s eq ' ' ? : 174 627 801
84 ? undef()
85 : Games::Solitaire::Verify::Card->new(
86 {
87 string => $s,
88 }
89 )
90 );
91 }
92  
93  
94 sub count
95 775 ▶{
Subroutine count: called CC=1 SCAR=0.0
Pod documented
96 775 my $self = shift;
97  
98 775 return $self->_count();
99 }
100  
101  
102 sub cell
103 3958 ▶{
Subroutine cell: called CC=1 SCAR=0.0
Pod documented
104 3958 my ( $self, $idx ) = @_;
105  
106 3958 return $self->_cells()->[$idx];
107 }
108  
109  
110 sub assign
111 2340 ▶{
Subroutine assign: called CC=2 SCAR=6.9
Pod documented
112 2340 my ( $self, $idx, $card ) = @_;
113  
114 2340 $self->_cells()->[$idx] = $card;
115 substr(
116 2340 ▶ $self->{_s}, length('Freecells:') + ( $idx << 2 ) + 2,
Branch: 1 branch
BranchTrueFalseTotal
defined $card ? : 1568 772 2340
117 2, ( defined($card) ? $card->fast_s : ' ' )
118 );
119  
120 2340 return ();
121 }
122  
123  
124 sub to_string
125 2328 ▶{
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
126 2328 ( my $r = $_[0]->{_s} ) =~ s# +\z##;
127 2328 return $r;
128 }
129  
130  
131 sub cell_clone
132 26 ▶{
Subroutine cell_clone: called CC=2 SCAR=6.9
Pod documented
133 26 my ( $self, $pos ) = @_;
134  
135 26 my $card = $self->cell($pos);
136  
137 26 ▶ return defined($card) ? $card->clone() : undef();
Branch: 1 branch
BranchTrueFalseTotal
defined $card ? : 6 20 26
138 }
139  
140  
141 sub clear
142 578 ▶{
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
143 578 my ( $self, $pos ) = @_;
144  
145 578 $self->assign( $pos, undef() );
146  
147 578 return ();
148 }
149  
150  
151 sub clone
152 7 ▶{
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
153 7 my $self = shift;
154  
155 7 my $copy = __PACKAGE__->new(
156 {
157 count => $self->count(),
158 }
159 );
160  
161 7 foreach my $pos ( 0 .. ( $self->count() - 1 ) )
162 {
163 26 $copy->assign( $pos, $self->cell_clone($pos) );
164 }
165  
166 7 return $copy;
167 }
168  
169  
170 sub num_empty
171 489 ▶{
Subroutine num_empty: called CC=3 SCAR=11.0
Pod documented
172 489 my $self = shift;
173  
174 489 my $count = 0;
175  
176 489 foreach my $fc_idx ( 0 .. ( $self->count() - 1 ) )
177 {
178 1692 ▶ if ( !defined( $self->cell($fc_idx) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->cell($fc_idx)) 251 1441 1692
179 {
180 251 ++$count;
181 }
182 }
183 489 return $count;
184 }
185  
186 22 1; # End of Games::Solitaire::Verify::Freecells
187  
188 __END__
« Verify/State.pm ↑ Summary Verify/Card.pm »