File Coverage

blib/lib/Games/Solitaire/Verify/Solution.pm
Criterion Covered Total %
statement 71 79 89.8
branch 14 24 58.3
condition 4 6 66.6
subroutine 14 14 100.0
pod 1 1 100.0
total 104 124 83.8


line stmt bran cond sub pod time code
1             package Games::Solitaire::Verify::Solution;
2             $Games::Solitaire::Verify::Solution::VERSION = '0.2601';
3 8     8   497512 use warnings;
  8         15  
  8         586  
4 8     8   44 use strict;
  8         15  
  8         249  
5              
6 8     8   128 use 5.008;
  8         51  
7              
8              
9 8     8   559 use parent 'Games::Solitaire::Verify::Solution::Base';
  8         399  
  8         44  
10              
11 8     8   1664 use Games::Solitaire::Verify::Exception ();
  8         18  
  8         150  
12 8     8   4372 use Games::Solitaire::Verify::Card ();
  8         23  
  8         270  
13 8     8   4133 use Games::Solitaire::Verify::Column ();
  8         29  
  8         219  
14 8     8   4105 use Games::Solitaire::Verify::Move ();
  8         20  
  8         225  
15 8     8   4433 use Games::Solitaire::Verify::State ();
  8         29  
  8         6020  
16              
17              
18             sub _init
19             {
20 16     16   48 my ( $self, $args ) = @_;
21              
22 16         86 $self->SUPER::_init($args);
23              
24 16         50 $self->_st(undef);
25 16         44 $self->_reached_end(0);
26              
27 16         36 return 0;
28             }
29              
30             sub _read_state
31             {
32 1343     1343   2005 my $self = shift;
33              
34 1343         3306 my $line = $self->_l();
35              
36 1343 50       3211 if ( $line ne "\n" )
37             {
38 0         0 die "Non empty line before state";
39             }
40              
41 1343         2043 my $str = "";
42              
43 1343   66     2697 while ( ( $line = $self->_l() ) && ( $line ne "\n" ) )
44             {
45 13774         27427 $str .= $line;
46             }
47              
48 1343 100       3609 if ( !defined( $self->_st() ) )
49             {
50             $self->_st(
51             Games::Solitaire::Verify::State->new(
52             {
53             string => $str,
54 16         35 @{ $self->_V },
  16         198  
55             }
56             )
57             );
58             }
59             else
60             {
61 1327 50       3529 if ( $self->_st()->to_string() ne $str )
62             {
63 0         0 die "States don't match";
64             }
65             }
66              
67 1343   66     3762 while ( defined( $line = $self->_l() ) && ( $line eq "\n" ) )
68             {
69             }
70              
71 1343 50       5580 if ( $line !~ m{\A={3,}\n\z} )
72             {
73 0         0 die "No ======== separator";
74             }
75              
76 1343         3495 return ();
77             }
78              
79             sub _read_move
80             {
81 1343     1343   2053 my $self = shift;
82              
83 1343         2758 my $line = $self->_l();
84              
85 1343 50       2969 if ( $line ne "\n" )
86             {
87 0         0 die "No empty line before move";
88             }
89              
90 1343         2484 $line = $self->_l();
91              
92 1343 100       2648 if ( $line eq "This game is solveable.\n" )
93             {
94 10         33 $self->_reached_end(1);
95              
96 10         43 return "END";
97             }
98              
99 1333         2355 chomp($line);
100              
101 1333         7913 $self->_move(
102             Games::Solitaire::Verify::Move->new(
103             {
104             fcs_string => $line,
105             game => $self->_variant(),
106             }
107             )
108             );
109              
110 1333         3861 return ();
111             }
112              
113             sub _apply_move
114             {
115 1333     1333   1890 my $self = shift;
116              
117 1333 100       4121 if ( my $verdict = $self->_st()->verify_and_perform_move( $self->_move() ) )
118             {
119 6         76 Games::Solitaire::Verify::Exception::VerifyMove->throw(
120             error => "Wrong Move",
121             problem => $verdict,
122             );
123             }
124              
125 1327         2299 return ();
126             }
127              
128              
129             sub verify
130             {
131 16     16 1 544 my $self = shift;
132              
133 16         37 eval {
134              
135 16         384 my $line = $self->_l();
136              
137 16 50       186 if ( $line !~ m{\A(-=)+-\n\z} )
138             {
139 0         0 die "Incorrect start";
140             }
141              
142 16         55 $self->_read_state();
143 16         127 $self->_st->verify_contents( { max_rank => $self->_max_rank } );
144              
145 16         67 while ( !defined( scalar( $self->_read_move() ) ) )
146             {
147 1333         3295 $self->_apply_move();
148 1327         3379 $self->_read_state();
149             }
150             };
151              
152 16         4430 my $err;
153 16 100       81 if ( !$@ )
    50          
    0          
154             {
155             # Do nothing - no exception was thrown.
156             }
157             elsif (
158             $err = Exception::Class->caught(
159             'Games::Solitaire::Verify::Exception::VerifyMove')
160             )
161             {
162 6         50 return { error => $err, line_num => $self->_ln(), };
163             }
164             elsif (
165             $err = Exception::Class->caught(
166             'Games::Solitaire::Verify::Exception::State::MissingCards')
167             )
168             {
169 0         0 return { error => $err, line_num => $self->_ln(), };
170             }
171             elsif (1)
172             {
173 0         0 $err = Exception::Class->caught();
174 0 0       0 ref $err ? $err->rethrow : die $err;
175             }
176              
177 10         81 return ();
178             }
179              
180             1; # End of Games::Solitaire::Verify::Solution
181              
182             __END__