blib/lib/Test/Story/TestCase.pm

filter: Statementstmt 95.6% 111 / 116 Branchbran 86.3% 19 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.0% 19 / 20 Podpod 33.3% 1 / 3 totaltotal 93.1% 150 / 161 CC 16 SCAR 8.4
CC
16
Cov
94%
BUILD
4.0
parse_data
4.0
hasTags
3.0
CRAP 2.3
« blib/lib/Test/Story/File.pm ↑ Summary blib/lib/Test/Story.pm »
1 package Test::Story::TestCase;
2 21 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use YAML qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 126 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
8 126 my $self = shift;
9 126 my @configs = ();
10 126 foreach my $file (@{ $self->configuration }) {
11 2 $file = catfile($self->config->{file_root}, $file);
12 2 ▶ if (!-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 1 1 2
13 1 die sprintf(
14 q{Can't load configuration file "%s" for testcase "%s" in file "%s"; no such file.},
15 $file, $self->name, $self->filename
16 );
17 }
18 1 push @configs, %{ LoadFile($file) };
19 }
20 125 push @configs, %{ dclone($self->config) };
21 125 my %config = @configs;
22 125 foreach my $key (keys %config) {
23 768 $self->config->{$key} = $config{$key};
24 }
25 }
26  
27 21 has config => (
28 is => q{ro},
29 required => 1,
30 isa => q{HashRef}
31 );
32  
33 my %default_lazy = (
34 required => 1,
35 lazy => 1,
36 is => q{ro},
37 0 ▶ default => sub { die "need to override" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 21 );
39  
40 21 has data => (
41 is => q{ro},
42 required => 1,
43 isa => q{HashRef}
44 );
45  
46 21 has filename => (
47 is => q{ro},
48 required => 1,
49 isa => q{Str}
50 );
51  
52 21 has index => (
53 is => q{ro},
54 required => 1,
55 isa => q{Int}
56 );
57  
58 has id => (
59 %default_lazy,
60 isa => q{Str},
61 22 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
62 22 my $self = shift;
63 22 ▶ if (exists $self->data->{ID}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->data->{'ID'}) { } 15 7 22
64 15 return $self->data->{ID};
65 } else {
66 7 my $id = $self->name;
67 7 $id =~ s/ /_/g;
68 7 return lc($id);
69 }
70 }
71 21 );
72  
73 has name => (
74 %default_lazy,
75 isa => q{Str},
76 9 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
77 9 my $self = shift;
78 9 return $self->data->{NAME};
79 }
80 21 );
81  
82 has summary => (
83 %default_lazy,
84 isa => q{Str},
85 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 1 my $self = shift;
87 1 return $self->data->{SUMMARY};
88 }
89 21 );
90  
91 has tags => (
92 %default_lazy,
93 isa => q{ArrayRef},
94 70 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
95 70 my $self = shift;
96 70 ▶ if (ref $self->data->{TAGS} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->data->{"TAGS"} eq "ARRAY") 70 0 70
97 70 return [map { lc($_) } @{ $self->data->{TAGS} }];
98 }
99 0 return [];
100 }
101 21 );
102  
103 90 ▶sub hasTags {
Subroutine hasTags: called CC=3 SCAR=11.0
Pod undocumented
104 90 my $self = shift;
105 90 my $truth = 1;
106 90 foreach my $tag (@_) {
107 #warn "Inspecting tag $tag against " . join(", ", @{ $self->tags }) . "\n";
108 124 ▶ $truth = 0 unless (grep {$_ eq $tag} @{ $self->tags });
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $tag;} @{$self->tags;} 73 51 124
109 }
110 #warn "Returning truth $truth\n";
111 90 return $truth;
112 }
113  
114 has configuration => (
115 %default_lazy,
116 isa => q{ArrayRef},
117 126 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
118 126 my $self = shift;
119 126 ▶ if (ref $self->data->{CONFIGURATION} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->data->{"CONFIGURATION"} eq "ARRAY") 2 124 126
120 2 return [@{ $self->data->{CONFIGURATION} }];
121 }
122 124 return [];
123 }
124 21 );
125  
126 has instructions => (
127 %default_lazy,
128 isa => q{ArrayRef},
129 121 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
130 121 my $self = shift;
131 121 ▶ if (ref $self->data->{INSTRUCTIONS} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->data->{"INSTRUCTIONS"} eq "ARRAY") 118 3 121
132 118 return [@{ $self->data->{INSTRUCTIONS} }];
133 }
134 3 return [];
135 }
136 21 );
137  
138 has expected => (
139 %default_lazy,
140 isa => q{ArrayRef},
141 1 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
142 1 my $self = shift;
143 1 ▶ if (ref $self->data->{EXPECTED} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->data->{"EXPECTED"} eq "ARRAY") 1 0 1
144 1 return [@{ $self->data->{EXPECTED} }];
145 }
146 0 return [];
147 }
148 21 );
149  
150 has preconditions => (
151 %default_lazy,
152 isa => q{ArrayRef},
153 121 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
154 121 my $self = shift;
155 121 ▶ if (ref $self->data->{PRECONDITIONS} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->data->{"PRECONDITIONS"} eq "ARRAY") 1 120 121
156 1 return [@{ $self->data->{PRECONDITIONS} }];
157 }
158 120 return [];
159 }
160 21 );
161  
162 has postconditions => (
163 %default_lazy,
164 isa => q{ArrayRef},
165 121 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
166 121 my $self = shift;
167 121 ▶ if (ref $self->data->{POSTCONDITIONS} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->data->{"POSTCONDITIONS"} eq "ARRAY") 0 121 121
168 0 return [@{ $self->data->{POSTCONDITIONS} }];
169 }
170 121 return [];
171 }
172 21 );
173  
174 has is_valid => (
175 %default_lazy,
176 isa => q{Bool},
177 120 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
178 120 my $self = shift;
179 120 my $length = @{ $self->test_data };
180 120 return $length > 0;
181 }
182 21 );
183  
184 has test_data => (
185 %default_lazy,
186 isa => q{ArrayRef},
187 121 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
188 121 my $self = shift;
189 return $self->parse_data([
190 121 @{ $self->preconditions },
191 121 @{ $self->instructions },
192 121 @{ $self->postconditions },
193 ]);
194 }
195 21 );
196  
197 127 ▶sub parse_data {
Subroutine parse_data: called CC=4 SCAR=13.9
Pod documented
198 127 my $self = shift;
199 127 my ($data) = @_;
200 127 my @tests = ();
201 127 foreach my $test (@$data) {
202 # Handle single-string tests
203 461 ▶ if (!ref($test)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $test) { } 128 333 461
elsif (ref $test eq 'HASH') { } 332 1 333
204 128 push @tests, [$test];
205 }
206  
207 # Handle hash tests
208 elsif (ref($test) eq 'HASH') {
209 332 my ($name) = keys %$test;
210 332 my ($value) = $test->{$name};
211 332 push @tests, [$name, $value];
212 }
213  
214 else {
215 1 die "Unable to parse structure of type '".ref($test)."'";
216 }
217 }
218 126 return \@tests;
219 }
220  
221 # unimport moose functions and make immutable
222 21 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 21 __PACKAGE__->meta->make_immutable();
224 21 1;
225  
226 =pod
227  
228 =head1 NAME
229  
230 Test::Story::TestCase - Storytest testcase object
231  
232 =head1 SYNOPSIS
233  
234 my $tc = Test::Story::TestCase->new({
235 data => [ ... ],
236 index => ++$idx,
237 filename => "cases/test1.tc",
238 });
239  
240 =head1 DESCRIPTION
241  
242 This represents an individual testcase within a test file. It encapsulates
243 the logic around parsing test instructions, sorting them and processing
244 their arguments in such a way that they are readable by
245 L<Test::FITesque::Test>.
246  
247 =head1 METHODS
248  
249 =head2 Data Accessors
250  
251 =over 4
252  
253 =item id
254  
255 Returns the C<ID> property from the testcase data. If none is supplied, it
256 generates an ID from the testcase L</name> property.
257  
258 =item name
259  
260 Returns the C<NAME> property from the testcase data.
261  
262 =item summary
263  
264 Returns the C<SUMMARY> property from the testcase data.
265  
266 =item tags
267  
268 Returns an array of the C<TAGS> list from the testcase data. An example of
269 the expected syntax is:
270  
271 TAGS:
272 - clustering
273 - smoke
274  
275 =item expected
276  
277 Returns an array of the C<EXPECTED> list from the testcase data.
278  
279 =item configuration
280  
281 Returns an array of the C<CONFIGURATION> list from the testcase data. This
282 can be used by fixtures to load additional configuration that may be needed
283 to run your test.
284  
285 =item preconditions
286  
287 Returns an array of the C<PRECONDITIONS> list, used by L</test_data> to
288 compose a list of fixture calls.
289  
290 =item postconditions
291  
292 Returns an array of the C<POSTCONDITIONS> list, used by L</test_data> to
293 compose a list of fixture calls.
294  
295 =item instructions
296  
297 Returns an array of the C<INSTRUCTIONS> list, used by L</test_data> to
298 compose a list of fixture calls.
299  
300 =back
301  
302 =head2 Object Methods
303  
304 =over 4
305  
306 =item data
307  
308 Returns the raw datastructure of the YAML file.
309  
310 =item test_data
311  
312 Assembles the results from L</preconditions>, L</instructions>, and
313 L</postconditions> and, using L</parse_data>, returns a data structure that
314 L<Test::FITesque::Test> can process.
315  
316 =item parse_data
317  
318 Scrubs the arguments to test statements into a format that
319 L<Test::FITesque::Test> can process.
320  
321 =back
322  
323 =head1 SEE ALSO
324  
325 L<Test::Story::File>
326  
327 =head1 AUTHORS
328  
329 Michael Nachbaur E<lt>mike@nachbaur.comE<gt>,
330 Scott McWhirter E<lt>konobi@cpan.orgE<gt>
331  
332 =head1 LICENSE
333  
334 This library is free software; you can redistribute it and/or modify
335 it under the same terms as Perl itself, either Perl version 5.8.7 or,
336 at your option, any later version of Perl 5 you may have available.
337  
338 =head1 COPYRIGHT
339  
340 Copyright (C) 2008 Sophos, Plc.
341  
342 =cut
« blib/lib/Test/Story/File.pm ↑ Summary blib/lib/Test/Story.pm »