Kit/Save.pm

filter: Statementstmt 100% 265 / 265 Branchbran 90.1% 173 / 192 Conditioncond 75.9% 79 / 104 MC/DCmcdc 60.0% 39 / 65 Subroutinesub 100% 32 / 32 Podpod 95.0% 19 / 20 totaltotal 89.5% 607 / 678 CC 139 SCAR 30.8
CC
139
Cov
92%
from_text
50.1
check
23.3
saves_in
14.9
CRAP 21.9
↑ Summary Kit/Form.pm »
1 package Game::Terminal::Kit::Save;
2  
3 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Game::Terminal::Kit::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 our $VERSION = '0.06';
13  
14 6 our $FORMAT = 1;
15 6 my $MAGIC = 'game-terminal-kit save';
16 6 my %BLOCK = map { $_ => 1 } qw/past current future/;
17 6 my $NAME = qr/\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/;
18  
19 has [qw/name version saved_at file/] => (
20 is => 'rw',
21 isa => Str
22 6 );
23  
24 has current => (
25 is => 'rw',
26 isa => Str
27 6 );
28  
29 has [qw/past future/] => (
30 is => 'rw',
31 isa => ArrayRef
32 6 );
33  
34 has settings => (
35 is => 'rw',
36 isa => HashRef
37 6 );
38  
39 has limit => (
40 is => 'rw',
41 isa => Int
42 6 );
43  
44 117 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
45 117 my ($self) = @_;
46 117 ▶ $self->past([]) unless $self->past;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->past 22 95 117
47 117 ▶ $self->future([]) unless $self->future;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->future 24 93 117
48 117 ▶ $self->settings({}) unless $self->settings;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->settings 42 75 117
49 117 $self->check;
50 104 return $self;
51 }
52  
53 62 ▶sub _fail {
Subroutine _fail: called CC=1 SCAR=0.0
54 62 my ($why) = @_;
55 62 Carp::croak("Game::Terminal::Kit::Save: $why");
56 }
57  
58 156 ▶sub check {
Subroutine check: called CC=23 SCAR=31.5
Pod documented
59 156 my ($self) = @_;
60 156 my $name = $self->name;
61 156 ▶ _fail('a save needs the name of the game') unless defined $name && !ref $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name 2 154 156
Condition66%2/3
defined $name and not ref $name
!ll&&!rl&&r
20154
MC/DC50%1/2
defined $name and not ref $name
defined $name not ref $name
Truth table66%2/3
A and not(B)
A= defined $nameB= not ref $name
ABresult
0X0
100
111
62 154 ▶ _fail("'$name' is not a name a save can carry") unless $name =~ $NAME;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$NAME/u 3 151 154
63 151 for my $field (qw/version saved_at/) {
64 301 my $value = $self->$field;
65 301 ▶ _fail("the $field is not one line of text")
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and ref $value || $value =~ /[\r\n]/u || !($value =~ /\S/u) 1 300 301
Condition77%7/9
defined $value and ref $value || $value =~ /[\r\n]/u || !($value =~ /\S/u)
!ll&&!rl&&r
267331
ref $value or $value =~ /[\r\n]/u or not $value =~ /\S/u
l!l&&r!l&&!r
1033
ref $value or $value =~ /[\r\n]/u
l!l&&r!l&&!r
0133
MC/DC50%2/4
defined $value and ref $value || $value =~ /[\r\n]/u || !($value =~ /\S/u)
defined $value ref $value $value =~ /[\r\n]/u not $value =~ /\S/u
Truth table60%3/5
A and B or C or not(D)
A= defined $valueB= ref $valueC= $value =~ /[\r\n]/uD= not $value =~ /\S/u
ABCDresult
0XXX0
10000
11XX1
101X1
10011
66 if defined $value && (ref $value || $value =~ m/[\r\n]/ || $value !~ m/\S/);
67 }
68 150 ▶ _fail('the limit is not a whole number') if defined $self->limit && $self->limit !~ m/\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->limit and not $self->limit =~ /\A\d+\z/u 1 149 150
Condition100%3/3
defined $self->limit and not $self->limit =~ /\A\d+\z/u
!ll&&!rl&&r
118311
MC/DC100%2/2
defined $self->limit and not $self->limit =~ /\A\d+\z/u
defined $self->limit not $self->limit =~ /\A\d+\z/u
Truth table100%3/3
A and not(B)
A= defined $self->limitB= not $self->limit =~ /\A\d+\z/u
ABresult
0X0
100
111
69 149 ▶ _fail('a save needs the game as it is now') unless defined $self->current;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->current 1 148 149
70 148 for my $which (qw/past future/) {
71 295 my $steps = $self->$which;
72 295 ▶ _fail("the $which is not a list of steps") unless ref $steps eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $steps eq "ARRAY" 0 295 295
73 295 ▶ _fail("a step of the $which is not text") if grep { !defined || ref } @{$steps};
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or ref $_;} @{$steps;} 2 293 295
Condition100%3/3
!defined($_) || ref $_
l!l&&r!l&&!r
11533
MC/DC100%2/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table100%3/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
74 }
75 146 ▶ _fail('the game as it is now is not text') if ref $self->current;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->current 2 144 146
76 144 my $settings = $self->settings;
77 144 ▶ _fail('the settings are not a hash') unless ref $settings eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" 0 144 144
78 144 for my $key (sort keys %{$settings}) {
79 41 ▶ _fail("'$key' is not a name a setting can have") unless $key =~ $NAME;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /$NAME/u 1 40 41
80 40 my $value = $settings->{$key};
81 40 ▶ _fail("the setting $key is not one line of text")
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and ref $value || $value =~ /[\r\n]/u 1 39 40
Condition83%5/6
defined $value and ref $value || $value =~ /[\r\n]/u
!ll&&!rl&&r
3361
ref $value or $value =~ /[\r\n]/u
l!l&&r!l&&!r
0136
MC/DC66%2/3
defined $value and ref $value || $value =~ /[\r\n]/u
defined $value ref $value $value =~ /[\r\n]/u
Truth table75%3/4
A and B or C
A= defined $valueB= ref $valueC= $value =~ /[\r\n]/u
ABCresult
0XX0
1000
11X1
1011
82 if defined $value && (ref $value || $value =~ m/[\r\n]/);
83 }
84 142 return $self;
85 }
86  
87 57 ▶sub steps {
Subroutine steps: called CC=2 SCAR=6.9
Pod documented
88 57 my ($self) = @_;
89 57 ▶ return $self->count unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 55 57
90 55 return (@{ $self->past }, $self->current, @{ $self->future });
91 }
92  
93 23 ▶sub at {
Subroutine at: called CC=1 SCAR=0.0
Pod documented
94 23 my ($self) = @_;
95 23 return scalar @{ $self->past };
96 }
97  
98 4 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
99 4 my ($self) = @_;
100 4 return @{ $self->past } + 1 + @{ $self->future };
101 }
102  
103 8 ▶sub from_step {
Subroutine from_step: called CC=5 SCAR=16.1
Pod documented
104 8 my ($self, $index) = @_;
105 8 my @steps = $self->steps;
106 8 ▶ _fail("there is no step $index") unless defined $index && $index =~ m/\A\d+\z/ && $index < @steps;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index and $index =~ /\A\d+\z/u and $index < @steps 2 6 8
Condition83%5/6
defined $index and $index =~ /\A\d+\z/u and $index < @steps
!ll&&!rl&&r
116
defined $index and $index =~ /\A\d+\z/u
!ll&&!rl&&r
017
MC/DC66%2/3
defined $index and $index =~ /\A\d+\z/u and $index < @steps
defined $index $index =~ /\A\d+\z/u $index < @steps
Truth table75%3/4
A and B and C
A= defined $indexB= $index =~ /\A\d+\z/uC= $index < @steps
ABCresult
0XX0
10X0
1100
1111
107 return ref($self)->new(
108 30 ▶ (map { defined $self->$_ ? ($_ => $self->$_) : () } qw/name version saved_at file limit/),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->$_ ? : 10 20 30
109 6 settings => { %{ $self->settings } },
110 past => [ @steps[0 .. $index - 1] ],
111 current => $steps[$index],
112 future => [ @steps[$index + 1 .. $#steps] ],
113 );
114 }
115  
116 11 ▶sub _now {
Subroutine _now: called CC=1 SCAR=0.0
117 11 my @t = gmtime;
118 11 return sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ', $t[5] + 1900, $t[4] + 1, @t[3, 2, 1, 0];
119 }
120  
121 194 ▶sub _block {
Subroutine _block: called CC=2 SCAR=6.9
122 194 my ($keyword, $text) = @_;
123 194 ▶ return ($keyword, map { length $_ ? "| $_" : '|' } split /\n/, $text, -1);
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 189 43 232
124 }
125  
126 39 ▶sub as_text {
Subroutine as_text: called CC=4 SCAR=13.9
Pod documented
127 39 my ($self) = @_;
128 39 $self->check;
129 38 my $settings = $self->settings;
130 my @lines = (
131 "$MAGIC $FORMAT",
132 'game ' . $self->name,
133 (defined $self->version ? 'version ' . $self->version : ()),
134 (defined $self->saved_at ? 'saved ' . $self->saved_at : ()),
135 (defined $self->limit ? 'limit ' . $self->limit : ()),
136 11 (map { "setting $_ $settings->{$_}" } grep { defined $settings->{$_} } sort keys %{$settings}),
137 110 (map { _block(past => $_) } @{ $self->past }),
138 _block(current => $self->current),
139 38 ▶ (map { _block(future => $_) } @{ $self->future }),
Branch: 3 branches
BranchTrueFalseTotal
defined $self->version ? : 1 37 38
defined $self->saved_at ? : 13 25 38
defined $self->limit ? : 4 34 38
140 'end',
141 );
142 38 return join '', map { "$_\n" } @lines;
143 }
144  
145 78 ▶sub from_text {
Subroutine from_text: called CC=50 SCAR=39.1
Pod documented
146 78 my ($class, $text, %expect) = @_;
147 78 ▶ my $source = defined $expect{source} ? $expect{source} : 'the text';
Branch: 1 branch
BranchTrueFalseTotal
defined $expect{'source'} ? : 17 61 78
148 39 ▶ my $refuse = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
149 39 my ($number, $why) = @_;
150 39 ▶ _fail("$source " . (defined $number ? "line $number: " : '') . $why);
Branch: 1 branch
BranchTrueFalseTotal
defined $number ? : 11 28 39
151 78 };
152 78 ▶ $refuse->(undef, 'is not text') unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 1 77 78
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
1077
MC/DC50%1/2
defined $text and not ref $text
defined $text not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
153  
154 77 my @lines = split /\n/, $text, -1;
155 77 ▶ pop @lines if @lines && $lines[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @lines and $lines[-1] eq "" 75 2 77
Condition100%3/3
@lines and $lines[-1] eq ""
!ll&&!rl&&r
1175
MC/DC100%2/2
@lines and $lines[-1] eq ""
@lines $lines[-1] eq ""
Truth table100%3/3
A and B
A= @linesB= $lines[-1] eq ""
ABresult
0X0
100
111
156 77 ▶ my $crlf = @lines && $lines[0] =~ m/\r\z/;
Condition100%3/3
@lines && $lines[0] =~ /\r\z/u
!ll&&!rl&&r
1751
MC/DC100%2/2
@lines && $lines[0] =~ /\r\z/u
@lines $lines[0] =~ /\r\z/u
Truth table100%3/3
A && B
A= @linesB= $lines[0] =~ /\r\z/u
ABresult
0X0
100
111
157 77 ▶ s/\r\z// for $crlf ? @lines : ();
Branch: 1 branch
BranchTrueFalseTotal
$crlf ? : 1 76 77
158  
159 77 ▶ my $first = @lines ? $lines[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
@lines ? : 76 1 77
160 77 ▶ $refuse->(undef, 'is not a saved game') unless $first =~ m/\A\Q$MAGIC\E (\d+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $first =~ /\A\Q$MAGIC\E (\d+)\z/u 4 73 77
161 73 ▶ $refuse->(undef, "was saved in format $1, newer than the format $FORMAT this Game::Terminal::Kit reads")
Branch: 1 branch
BranchTrueFalseTotal
if $1 > $Game::Terminal::Kit::Save::FORMAT 1 72 73
162 if $1 > $FORMAT;
163  
164 72 my (%head, %settings, @past, @future, $current, $in, $ended);
165 72 my $number = 1;
166 72 for my $line (@lines[1 .. $#lines]) {
167 668 $number++;
168 668 ▶ if ($ended) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ended) 3 665 668
169 3 ▶ $refuse->($number, 'there is more after the end') if $line =~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\S/u 1 2 3
170 2 next;
171 }
172 665 ▶ if ($line =~ m/\A\| ?(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\| ?(.*)\z/su) 268 397 665
173 268 ▶ $refuse->($number, 'a line of a step before any step began') unless $in;
Branch: 1 branch
BranchTrueFalseTotal
unless $in 1 267 268
174 267 ▶ push @{ $in->{lines} }, defined $1 ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 267 0 267
175 267 next;
176 }
177 397 ▶ if ($BLOCK{$line}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BLOCK{$line}) 231 166 397
178 231 ▶ $refuse->($number, 'a second current step') if $line eq 'current' && defined $current;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "current" and defined $current 1 230 231
Condition100%3/3
$line eq "current" and defined $current
!ll&&!rl&&r
174561
MC/DC100%2/2
$line eq "current" and defined $current
$line eq "current" defined $current
Truth table100%3/3
A and B
A= $line eq "current"B= defined $current
ABresult
0X0
100
111
179 230 ▶ $refuse->($number, 'a past step after the current one') if $line eq 'past' && defined $current;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "past" and defined $current 1 229 230
Condition100%3/3
$line eq "past" and defined $current
!ll&&!rl&&r
1041251
MC/DC100%2/2
$line eq "past" and defined $current
$line eq "past" defined $current
Truth table100%3/3
A and B
A= $line eq "past"B= defined $current
ABresult
0X0
100
111
180 229 ▶ $refuse->($number, 'a future step before the current one') if $line eq 'future' && !defined $current;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "future" and not defined $current 1 228 229
Condition100%3/3
$line eq "future" and not defined $current
!ll&&!rl&&r
181471
MC/DC100%2/2
$line eq "future" and not defined $current
$line eq "future" not defined $current
Truth table100%3/3
A and not(B)
A= $line eq "future"B= not defined $current
ABresult
0X0
100
111
181 228 my $step = { lines => [] };
182 228 ▶ if ($line eq 'past') { push @past, $step }
Branch: 2 branches
BranchTrueFalseTotal
if ($line eq 'past') { } 125 103 228
elsif ($line eq 'future') { } 47 56 103
183 47 elsif ($line eq 'future') { push @future, $step }
184 56 else { $current = $step }
185 228 $in = $step;
186 228 next;
187 }
188 166 ▶ if ($line eq 'end') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "end") 48 118 166
189 48 $ended = 1;
190 48 next;
191 }
192 118 ▶ next if !$in && $line !~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $in or $line =~ /\S/u 2 116 118
Condition100%3/3
$in or $line =~ /\S/u
l!l&&r!l&&!r
11152
MC/DC100%2/2
$in or $line =~ /\S/u
$in $line =~ /\S/u
Truth table100%3/3
A or B
A= $inB= $line =~ /\S/u
ABresult
1X1
011
000
193 116 ▶ my ($key, $value) = $line =~ m/\A([a-z]+)(?: (.*))?\z/s
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A([a-z]+)(?: (.*))?\z/su or &$refuse($number, "'${line}' is not a line a saved game has") 115 1 116
194 or $refuse->($number, "'$line' is not a line a saved game has");
195 115 ▶ $refuse->($number, "'$key' after the steps began") if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 1 114 115
196 114 ▶ $refuse->($number, "the $key line has no value")
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and $key =~ /\A(?:game|version|saved|limit|setting)\z/u 1 113 114
Condition66%2/3
not defined $value and $key =~ /\A(?:game|version|saved|limit|setting)\z/u
!ll&&!rl&&r
11301
MC/DC50%1/2
not defined $value and $key =~ /\A(?:game|version|saved|limit|setting)\z/u
not defined $value $key =~ /\A(?:game|version|saved|limit|setting)\z/u
Truth table66%2/3
not(A) and B
A= not defined $valueB= $key =~ /\A(?:game|version|saved|limit|setting)\z/u
ABresult
0X0
100
111
197 if !defined $value && $key =~ m/\A(?:game|version|saved|limit|setting)\z/;
198 113 ▶ if ($key eq 'setting') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "setting") 23 90 113
199 23 ▶ my ($name, $setting) = defined $value ? $value =~ m/\A(\S+) (.*)\z/s : ()
Branch: 2 branches
BranchTrueFalseTotal
() = defined $value ? $value =~ /\A(\S+) (.*)\z/su : () or &$refuse($number, "a setting needs a name and a value") 22 1 23
defined $value ? : 23 0 23
200 or $refuse->($number, 'a setting needs a name and a value');
201 22 ▶ $refuse->($number, "the setting $name is there twice") if exists $settings{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $settings{$name} 1 21 22
202 21 $settings{$name} = $setting;
203 21 next;
204 }
205 90 ▶ $refuse->($number, "the $key is there twice") if exists $head{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $head{$key} 1 89 90
206 89 $head{$key} = $value;
207 }
208 61 ▶ $refuse->(undef, 'ends before its last line, and may have been cut short') unless $ended;
Branch: 1 branch
BranchTrueFalseTotal
unless $ended 14 47 61
209 47 ▶ $refuse->(undef, 'has no game line') unless defined $head{game};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $head{"game"} 1 46 47
210 46 ▶ $refuse->(undef, 'has no current step') unless defined $current;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current 1 45 46
211 $refuse->(undef, "is a saved game of $head{game}, not of $expect{game}")
212 45 ▶ if defined $expect{game} && $head{game} ne $expect{game};
Branch: 1 branch
BranchTrueFalseTotal
if defined $expect{"game"} and $head{"game"} ne $expect{"game"} 4 41 45
Condition100%3/3
defined $expect{"game"} and $head{"game"} ne $expect{"game"}
!ll&&!rl&&r
3564
MC/DC100%2/2
defined $expect{"game"} and $head{"game"} ne $expect{"game"}
defined $expect{"game"} $head{"game"} ne $expect{"game"}
Truth table100%3/3
A and B
A= defined $expect{"game"}B= $head{"game"} ne $expect{"game"}
ABresult
0X0
100
111
213 $refuse->(undef, 'has a limit that is not a whole number')
214 41 ▶ if defined $head{limit} && $head{limit} !~ m/\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $head{"limit"} and not $head{"limit"} =~ /\A\d+\z/u 1 40 41
Condition100%3/3
defined $head{"limit"} and not $head{"limit"} =~ /\A\d+\z/u
!ll&&!rl&&r
3731
MC/DC100%2/2
defined $head{"limit"} and not $head{"limit"} =~ /\A\d+\z/u
defined $head{"limit"} not $head{"limit"} =~ /\A\d+\z/u
Truth table100%3/3
A and not(B)
A= defined $head{"limit"}B= not $head{"limit"} =~ /\A\d+\z/u
ABresult
0X0
100
111
215  
216 40 ▶ my $text_of = sub { join "\n", @{ $_[0]{lines} } };
Subroutine __ANON__: called CC=1 SCAR=0.0
217 40 my $save = eval {
218 $class->new(
219 name => $head{game},
220 (defined $head{version} ? (version => $head{version}) : ()),
221 (defined $head{saved} ? (saved_at => $head{saved}) : ()),
222 (defined $head{limit} ? (limit => $head{limit} + 0) : ()),
223 settings => \%settings,
224 88 past => [ map { $text_of->($_) } @past ],
225 current => $text_of->($current),
226 40 ▶ future => [ map { $text_of->($_) } @future ],
Branch: 3 branches
BranchTrueFalseTotal
defined $head{'version'} ? : 0 40 40
defined $head{'saved'} ? : 12 28 40
defined $head{'limit'} ? : 3 37 40
227 );
228 };
229 40 ▶ unless ($save) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($save) 1 39 40
230 1 (my $why = $@) =~ s/\AGame::Terminal::Kit::Save: //;
231 1 $why =~ s/ at \S+ line \d+\.?\n?\z//;
232 1 $refuse->(undef, $why);
233 }
234 39 return $save;
235 }
236  
237 12 ▶sub write_file {
Subroutine write_file: called CC=11 SCAR=24.4
Pod documented
238 12 my ($self, $path) = @_;
239 12 ▶ _fail('write_file needs a file to write') unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 1 11 12
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
0111
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
240 11 my $keep = $self->saved_at;
241 11 $self->saved_at(_now());
242 11 my $text = eval { $self->as_text };
243 11 ▶ unless (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 1 10 11
244 1 my $why = $@;
245 1 $self->saved_at($keep);
246 1 die $why;
247 }
248 10 my $temporary = "$path.$$.tmp";
249 10 my $fh;
250 10 ▶ unless (open $fh, '>:raw:encoding(UTF-8)', $temporary) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">:raw:encoding(UTF-8)", $temporary) 1 9 10
251 1 my $why = $!;
252 1 $self->saved_at($keep);
253 1 _fail("cannot write $path: $why");
254 }
255 9 my $wrote = print {$fh} $text;
256 9 my $why = $!;
257 9 ▶ $wrote = close($fh) && $wrote;
Condition33%1/3
close $fh && $wrote
!ll&&!rl&&r
009
MC/DC0%0/2
close $fh && $wrote
close $fh $wrote
Truth table33%1/3
A && B
A= close $fhB= $wrote
ABresult
0X0
100
111
258 9 ▶ $why = $! unless $wrote || $why;
Branch: 1 branch
BranchTrueFalseTotal
unless $wrote or $why 0 9 9
Condition33%1/3
$wrote or $why
l!l&&r!l&&!r
900
MC/DC0%0/2
$wrote or $why
$wrote $why
Truth table33%1/3
A or B
A= $wroteB= $why
ABresult
1X1
011
000
259 9 ▶ $wrote &&= rename $temporary, $path;
Condition66%2/3
$wrote &&= rename($temporary, $path)
!ll&&!rl&&r
018
MC/DC50%1/2
$wrote &&= rename($temporary, $path)
$wrote rename($temporary, $path)
Truth table66%2/3
A &&= B
A= $wroteB= rename($temporary, $path)
ABresult
0X0
100
111
260 9 ▶ unless ($wrote) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wrote) 1 8 9
261 1 ▶ $why = $! if !$why;
Branch: 1 branch
BranchTrueFalseTotal
unless $why 0 1 1
262 1 unlink $temporary;
263 1 $self->saved_at($keep);
264 1 _fail("cannot write $path: $why");
265 }
266 8 $self->file($path);
267 8 return $path;
268 }
269  
270 21 ▶sub read_file {
Subroutine read_file: called CC=8 SCAR=21.2
Pod documented
271 21 my ($class, $path, %expect) = @_;
272 21 ▶ _fail('read_file needs a file to read') unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 21 21
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
273 21 ▶ _fail("there is no file $path") unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 2 19 21
274 19 ▶ _fail("$path is a directory, not a saved game") if -d $path;
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 1 18 19
275 18 ▶ open my $fh, '<:raw', $path or _fail("cannot read $path: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or _fail("cannot read ${path}: $!") 18 0 18
276 18 my $bytes = do { local $/; readline $fh };
277 18 close $fh;
278 18 ▶ $bytes = '' unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 18 18
279 18 ▶ _fail("$path is not a saved game") unless utf8::decode($bytes);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::decode($bytes) 1 17 18
280 17 my $save = $class->from_text($bytes, %expect, source => $path);
281 12 $save->file($path);
282 12 return $save;
283 }
284  
285 11 ▶sub read_game {
Subroutine read_game: called CC=3 SCAR=11.0
Pod documented
286 11 my ($class, $path, $name) = @_;
287 11 ▶ my $save = eval { $class->read_file($path, (defined $name ? (game => $name) : ())) };
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 7 4 11
288 11 ▶ return $save if $save;
Branch: 1 branch
BranchTrueFalseTotal
if $save 6 5 11
289 5 return (undef, why($@));
290 }
291  
292 3 ▶sub saves_in {
Subroutine saves_in: called CC=13 SCAR=27.0
Pod documented
293 3 my ($dir, %option) = @_;
294 3 ▶ $dir = '.' unless defined $dir && length $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and length $dir 0 3 3
Condition33%1/3
defined $dir and length $dir
!ll&&!rl&&r
003
MC/DC0%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table33%1/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
295 3 ▶ opendir my $listing, $dir or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $listing, $dir or return () 2 1 3
296 2 ▶ my @names = sort grep { m/\.save\z/ && !m/\A\./ } readdir $listing;
Condition66%2/3
/\.save\z/u && !/\A\./u
!ll&&!rl&&r
6010
MC/DC50%1/2
/\.save\z/u && !/\A\./u
/\.save\z/u !/\A\./u
Truth table66%2/3
A && not(B)
A= /\.save\z/uB= !/\A\./u
ABresult
0X0
100
111
297 2 closedir $listing;
298 2 my @found;
299 2 for my $name (@names) {
300 10 ▶ my $path = $dir eq '.' ? $name : File::Spec->catfile($dir, $name);
Branch: 1 branch
BranchTrueFalseTotal
$dir eq '.' ? : 0 10 10
301 10 ▶ next unless -f $path && -s _ <= 16 * 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path and -s _ <= 16777216 2 8 10
Condition66%2/3
-f $path and -s _ <= 16777216
!ll&&!rl&&r
208
MC/DC50%1/2
-f $path and -s _ <= 16777216
-f $path -s _ <= 16777216
Truth table66%2/3
A and B
A= -f $pathB= -s _ <= 16777216
ABresult
0X0
100
111
302 8 my ($save) = __PACKAGE__->read_game($path, $option{game});
303 8 ▶ push @found, $save if $save;
Branch: 1 branch
BranchTrueFalseTotal
if $save 5 3 8
304 }
305 return sort {
306 2 ▶ (defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '')
Branch: 2 branches
BranchTrueFalseTotal
defined $b->saved_at ? : 4 0 4
defined $a->saved_at ? : 4 0 4
Condition66%2/3
(defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '') || $a->file cmp $b->file
l!l&&r!l&&!r
310
MC/DC0%0/2
(defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '') || $a->file cmp $b->file
(defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '') $a->file cmp $b->file
Truth table66%2/3
A || B
A= (defined $b->saved_at ? $b->saved_at : '') cmp (defined $a->saved_at ? $a->saved_at : '')B= $a->file cmp $b->file
ABresult
1X1
011
000
307 || $a->file cmp $b->file
308 } @found;
309 }
310  
311 9 ▶sub why {
Subroutine why: called CC=3 SCAR=11.0
Pod documented
312 9 my ($error) = @_;
313 9 ▶ $error = 'it failed, and said nothing of why' unless defined $error && length $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error and length $error 1 8 9
Condition66%2/3
defined $error and length $error
!ll&&!rl&&r
018
MC/DC50%1/2
defined $error and length $error
defined $error length $error
Truth table66%2/3
A and B
A= defined $errorB= length $error
ABresult
0X0
100
111
314 9 $error =~ s/ at \S+ line \d+\.?\s*\z//;
315 9 $error =~ s/\A(?:[A-Z]\w*(?:::\w+)*): //;
316 9 $error =~ s/\.?\s*\z//;
317 9 return $error;
318 }
319  
320 2 ▶sub default_file {
Subroutine default_file: called CC=3 SCAR=13.1
Pod documented
321 2 my ($name) = @_;
322 2 ▶ return (defined $name && length $name ? $name : 'game') . '.save';
Branch: 1 branch
BranchTrueFalseTotal
defined $name && length $name ? : 2 0 2
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
002
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
323 }
324  
325 1 ▶sub flags {
Subroutine flags: called CC=1 SCAR=0.0
Pod documented
326 1 return ('save=s', 'resume=s', 'replay=s');
327 }
328  
329 7 ▶sub check_flags {
Subroutine check_flags: called CC=7 SCAR=19.5
Pod documented
330 7 my ($given) = @_;
331 7 my @set = grep { defined $given->{$_} } qw/save resume replay/;
332 7 for my $flag (@set) {
333 9 ▶ return "--$flag needs the name of a file" unless length $given->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless length $given->{$flag} 1 8 9
334 }
335 return '--resume and --replay each start from a saved game: give one of them'
336 6 ▶ if defined $given->{resume} && defined $given->{replay};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given->{"resume"} and defined $given->{"replay"} 1 5 6
Condition100%3/3
defined $given->{"resume"} and defined $given->{"replay"}
!ll&&!rl&&r
321
MC/DC100%2/2
defined $given->{"resume"} and defined $given->{"replay"}
defined $given->{"resume"} defined $given->{"replay"}
Truth table100%3/3
A and B
A= defined $given->{"resume"}B= defined $given->{"replay"}
ABresult
0X0
100
111
337 return '--save is where a game is written, and --replay writes nothing'
338 5 ▶ if defined $given->{save} && defined $given->{replay};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given->{"save"} and defined $given->{"replay"} 1 4 5
Condition100%3/3
defined $given->{"save"} and defined $given->{"replay"}
!ll&&!rl&&r
311
MC/DC100%2/2
defined $given->{"save"} and defined $given->{"replay"}
defined $given->{"save"} defined $given->{"replay"}
Truth table100%3/3
A and B
A= defined $given->{"save"}B= defined $given->{"replay"}
ABresult
0X0
100
111
339 4 return undef;
340 }
341  
342 2 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
343 2 return <<'USAGE';
344 --save FILE where S, or "save", writes the game. Default: the file
345 the game was resumed from, or NAME.save.
346 --resume FILE carry on a game saved to FILE, undo and redo and all.
347 --replay FILE watch a game saved to FILE, a step at a time.
348 USAGE
349 }
350  
351 6 ▶sub command {
Subroutine command: called CC=5 SCAR=16.1
Pod documented
352 6 my ($line) = @_;
353 6 ▶ return () unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 5 6
354 5 ▶ return () unless $line =~ m/\A\s*save(?:\s+(.*?))?\s*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A\s*save(?:\s+(.*?))?\s*\z/iu 2 3 5
355 3 ▶ return (1, defined $1 && length $1 ? $1 : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $1 && length $1 ? : 2 1 3
Condition66%2/3
defined $1 and length $1
!ll&&!rl&&r
102
MC/DC50%1/2
defined $1 and length $1
defined $1 length $1
Truth table66%2/3
A and B
A= defined $1B= length $1
ABresult
0X0
100
111
356 }
357  
358 1 ▶sub game_keys {
Subroutine game_keys: called CC=1 SCAR=0.0
Pod documented
359 1 return { S => 'save' };
360 }
361  
362 6 ▶sub ask_file {
Subroutine ask_file: called CC=10 SCAR=23.4
Pod documented
363 6 my ($keys, $screen, %option) = @_;
364 6 ▶ my $default = defined $option{default} ? $option{default} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $option{'default'} ? : 6 0 6
365 6 ▶ my $prompt = defined $option{prompt} ? $option{prompt} : 'save to: ';
Branch: 1 branch
BranchTrueFalseTotal
defined $option{'prompt'} ? : 0 6 6
366 6 ▶ unless ($keys->raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keys->raw) 3 3 6
367 3 my $line = $keys->read_line("$prompt\[$default] ", $screen->out);
368 3 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 2 3
369 2 ▶ return length $line ? $line : (length $default ? $default : undef);
Branch: 2 branches
BranchTrueFalseTotal
length $line ? : 1 1 2
length $default ? : 1 0 1
370 }
371 my $line = Game::Terminal::Kit::Line->new(
372 prompt => $prompt,
373 text => $default,
374 3 ▶ head => [ @{ $option{head} || [] } ],
Condition50%1/2
$option{'head'} || []
l!l
03
MC/DC0%0/1
$option{'head'} || []
$option{'head'}
Truth table50%1/2
A
A= $option{'head'}
Aresult
11
00
375 screen => $screen,
376 );
377 3 my ($event, $value) = $line->run($keys, $screen);
378 3 ▶ return undef unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 1 2 3
379 2 $value =~ s/^\s+|\s+$//g;
380 2 ▶ return length $value ? $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $value ? : 2 0 2
381 }
382  
383 6 1;
384  
385 __END__
↑ Summary Kit/Form.pm »