blib/lib/Test/Story/File.pm

filter: Statementstmt 95.9% 95 / 99 Branchbran 85.7% 24 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 2 totaltotal 92.3% 133 / 144 CC 18 SCAR 13.7
CC
18
Cov
94%
filtered_cases
7.0
__ANON__
6.2
__ANON__
4.0
CRAP 4.0
« blib/lib/Test/Story/Fixture.pm ↑ Summary blib/lib/Test/Story/TestCase.pm »
1 package Test::Story::File;
2 20 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use YAML qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use Test::Story::TestCase;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 has config => (
9 is => q{ro},
10 required => 1,
11 isa => q{HashRef}
12 );
13  
14 my %default_lazy = (
15 required => 1,
16 lazy => 1,
17 is => q{ro},
18 0 ▶ default => sub { die "need to override" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 20 );
20  
21 20 has filename => (
22 is => q{ro},
23 required => 1,
24 isa => q{Str}
25 );
26  
27 has file_root => (
28 %default_lazy,
29 isa => q{Str},
30 42 ▶ default => sub { return shift->config->{file_root} },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 20 );
32  
33 has fixture_base => (
34 %default_lazy,
35 isa => q{Str},
36 42 ▶ default => sub { return shift->config->{fixture_base} },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 20 );
38  
39 has verbose => (
40 %default_lazy,
41 isa => q{Int},
42 46 ▶ default => sub { return shift->config->{verbose} },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 20 );
44  
45 has data => (
46 %default_lazy,
47 isa => q{ArrayRef},
48 58 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
49 58 my $self = shift;
50 58 my $content = [];
51 58 eval {
52 58 $content = [ LoadFile($self->filename) ];
53 };
54 58 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 54 58
55 4 ▶ printf("# YAML syntax error while loading %s: %s\n", $self->filename, $@) if ($self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 3 4
56 }
57 58 return $content;
58 }
59 20 );
60  
61 20 has testcase_class => (
62 %default_lazy,
63 isa => q{Str},
64 default => "Test::Story::TestCase"
65 );
66  
67 has cases => (
68 %default_lazy,
69 isa => q{ArrayRef},
70 57 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
71 57 my $self = shift;
72 57 my @cases;
73 57 my $idx = 0;
74 57 my $filename = $self->filename;
75 57 foreach my $case (@{ $self->data }) {
76 118 ▶ next unless $case;
Branch: 1 branch
BranchTrueFalseTotal
unless $case 0 118 118
77 118 my $case = $self->testcase_class->new({
78 'data' => $case,
79 'index' => ++$idx,
80 'filename' => $filename,
81 'config' => dclone( $self->config ),
82 });
83 118 ▶ push @cases, $case if ($case->is_valid);
Branch: 1 branch
BranchTrueFalseTotal
if $case->is_valid 116 2 118
84 }
85 57 return \@cases;
86 }
87 20 );
88  
89 56 ▶sub filtered_cases {
Subroutine filtered_cases: called CC=7 SCAR=19.5
Pod undocumented
90 56 my $self = shift;
91 56 my ($id) = @_;
92  
93 56 my @cases = @{ $self->cases };
94 56 ▶ if (@{ $self->config->{'tags'}->{'include'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->config->{"tags"}{"include"};}) 19 37 56
95 19 @cases = grep { $_->hasTags(@{ $self->config->{'tags'}->{'include'} }) } @cases;
96 }
97 56 ▶ if (@{ $self->config->{'tags'}->{'exclude'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->config->{"tags"}{"exclude"};}) 10 46 56
98 10 my @filtered_cases = ();
99 10 CASE: foreach my $case (@cases) {
100 20 foreach my $tag (@{ $self->config->{'tags'}->{'exclude'} }) {
101 25 ▶ next CASE if ($case->hasTags($tag));
Branch: 1 branch
BranchTrueFalseTotal
if $case->hasTags($tag) 10 15 25
102 }
103 10 push @filtered_cases, $case;
104 }
105 10 @cases = @filtered_cases;
106 }
107 56 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 2 54 56
108 2 @cases = grep { $_->id eq $id } @cases;
109 }
110 56 return \@cases;
111 }
112  
113 has fixture_class => (
114 %default_lazy,
115 isa => q{Str},
116 42 ▶ default => sub {
Subroutine __ANON__: called CC=6 SCAR=18.3
117 42 my $self = shift;
118 42 local @INC = @INC;
119 42 ▶ unless (grep $self->fixture_base, @INC) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep $self->fixture_base, @INC) 0 42 42
120 0 unshift @INC, $self->fixture_base;
121 }
122 42 my $filename = $self->filename;
123 42 my $root = $self->file_root;
124 42 $filename =~ s#^$root/?##;
125 42 $filename =~ s/\s+//g;
126 42 my @path = split('/', $filename);
127 42 pop @path; # take off the filename
128 42 unshift @path, split(/::/, $self->fixture_base);
129  
130 42 ▶ while ($#path > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#path > -1) 49 0 49
131 49 my $class = join('::', @path);
132 49 ▶ print "# Attempting to load fixture class $class\n" if ($self->verbose > 2);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 2 1 48 49
133 49 eval {
134 49 load($class);
135 };
136 49 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 42 7 49
137 42 return $class;
138 }
139 7 ▶ if ($@ !~ /^Can't locate /) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@ =~ /^Can't locate /) 0 7 7
140 0 warn "Error while loading fixture $class: $@\n";
141 }
142 7 pop @path;
143 }
144 0 die 'Cannot find a fixture class for "' . $self->filename . '"';
145 }
146 20 );
147  
148 63 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
149 63 my $self = shift;
150 63 my ($params) = @_;
151  
152 63 ▶ if (!-f $self->filename){
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $self->filename) 1 62 63
153 1 die 'Could not find a8n file "' . $self->filename . '"';
154 }
155 }
156  
157 # unimport moose functions and make immutable
158 20 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 20 __PACKAGE__->meta->make_immutable();
160 20 1;
161  
162 =pod
163  
164 =head1 NAME
165  
166 Test::Story::File - Storytest file object
167  
168 =head1 SYNOPSIS
169  
170 my $file = Test::Story::File->new({
171 filename => "cases/test1.tc",
172 file_root => $a8n->file_root,
173 fixture_base => $a8n->fixture_base,
174 });
175 $file->run_tests();
176  
177 =head1 DESCRIPTION
178  
179 This class is used to represent and run tests within individual storytest
180 files. For more information, please see L<Test::Story>.
181  
182 =head1 METHODS
183  
184 =head2 Accessors
185  
186 =over 4
187  
188 =item filename
189  
190 The filename that this object is supposed to represent.
191  
192 =item fixture_base
193  
194 See L<Test::Story/fixture_base>.
195  
196 =item file_root
197  
198 See L<Test::Story/file_root>.
199  
200 =item verbose
201  
202 See L<Test::Story/verbose>.
203  
204 =item testcase_class
205  
206 Used to set the class used to store testcases.
207  
208 Default: L<Test::Story::TestCase>
209  
210 =back
211  
212 =head2 Object Methods
213  
214 =over 4
215  
216 =item run_tests
217  
218 This iterates over each of the L</cases> and will create a
219 L<Test::FITesque::Test> test runner for test case. If you call this method
220 with a testcase ID, it will only run that one testcase.
221  
222 =item data
223  
224 Returns the raw datastructure of the YAML file.
225  
226 =item cases
227  
228 Returns objects representing each testcase within this test file. Unless
229 L</testcase_class> is overridden, this property returns instances of the
230 L<Test::Story::TestCase> class.
231  
232 =item fixture_class
233  
234 This locates the fixture class used to run testcases. The resulting class
235 is called whenever a test action needs to be run.
236  
237 If the L</filename> of this test is one or more sub-directories below the
238 L</file_root>, then it will append the directory name to the
239 L</fixture_base>, and will use it as part of the class name. It works its
240 way up the directory hierarchy until it finds a valid Perl class. If no
241 such classes can be found, it will use the value of L</fixture_base>.
242  
243 =back
244  
245 =head1 SEE ALSO
246  
247 L<Test::Story::TestCase>, L<Test::FITesque::Test>
248  
249 =head1 AUTHORS
250  
251 Michael Nachbaur E<lt>mike@nachbaur.comE<gt>,
252 Scott McWhirter E<lt>konobi@cpan.orgE<gt>
253  
254 =head1 LICENSE
255  
256 This library is free software; you can redistribute it and/or modify
257 it under the same terms as Perl itself, either Perl version 5.8.7 or,
258 at your option, any later version of Perl 5 you may have available.
259  
260 =head1 COPYRIGHT
261  
262 Copyright (C) 2008 Sophos, Plc.
263  
264 =cut
« blib/lib/Test/Story/Fixture.pm ↑ Summary blib/lib/Test/Story/TestCase.pm »