blib/lib/Statocles/Test.pm

filter: Statementstmt 59.4% 69 / 116 Branchbran 21.7% 10 / 46 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 76.9% 10 / 13 Podpod 60.0% 3 / 5 totaltotal 46.0% 98 / 213 CC 34 SCAR 38.5
CC
34
Cov
47%
test_pages
122.5
build_test_site_apps
12.0
__ANON__
12.0
CRAP 47.2
« blib/lib/Statocles/Command/daemon.pm ↑ Summary blib/lib/Statocles/App/Blog.pm »
1 package Statocles::Test;
2 58 our $VERSION = '0.099';
3 # ABSTRACT: Common test routines for Statocles
4  
5 58 ▶use Statocles::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶use Statocles::Util qw( dircopy derp );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 58 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 our @EXPORT_OK = qw(
10 test_constructor test_pages build_test_site build_test_site_apps
11 build_temp_site
12 );
13  
14 #pod =sub build_test_site
15 #pod
16 #pod my $site = build_test_site( %site_args )
17 #pod
18 #pod Build a site for testing. The build and deploy will be set correctly to temporary
19 #pod directories. C<%site_args> will be given to the L<Statocles::Site|Statocles::Site>
20 #pod constructor.
21 #pod
22 #pod You must provide a C<theme> (probably using the one in C<t/share/theme>).
23 #pod
24 #pod =cut
25  
26 26 ▶sub build_test_site {
Subroutine build_test_site: called CC=5 SCAR=18.3
Pod documented
27 26 my ( %site_args ) = @_;
28 26 require Statocles::Site;
29  
30 26 ▶ $site_args{store} ||= Path::Tiny->tempdir;
Condition66%2/3
$site_args{"store"} ||= "Path::Tiny"->tempdir
l!l&&r!l&&!r
5210
MC/DC0%0/2
$site_args{"store"} ||= "Path::Tiny"->tempdir
$site_args{"store"} "Path::Tiny"->tempdir
Truth table66%2/3
A ||= B
A= $site_args{"store"}B= "Path::Tiny"->tempdir
ABresult
1X1
011
000
31 26 ▶ $site_args{build_store} ||= Path::Tiny->tempdir;
Condition33%1/3
$site_args{"build_store"} ||= "Path::Tiny"->tempdir
l!l&&r!l&&!r
0260
MC/DC0%0/2
$site_args{"build_store"} ||= "Path::Tiny"->tempdir
$site_args{"build_store"} "Path::Tiny"->tempdir
Truth table33%1/3
A ||= B
A= $site_args{"build_store"}B= "Path::Tiny"->tempdir
ABresult
1X1
011
000
32 26 ▶ $site_args{deploy} ||= Path::Tiny->tempdir;
Condition33%1/3
$site_args{"deploy"} ||= "Path::Tiny"->tempdir
l!l&&r!l&&!r
0260
MC/DC0%0/2
$site_args{"deploy"} ||= "Path::Tiny"->tempdir
$site_args{"deploy"} "Path::Tiny"->tempdir
Truth table33%1/3
A ||= B
A= $site_args{"deploy"}B= "Path::Tiny"->tempdir
ABresult
1X1
011
000
33  
34 # Give a testable logger by default, but only if we haven't asked
35 # for some verbose logging from the environment
36 my $log = $site_args{log}
37 26 ▶ || Mojo::Log->new(
Condition33%1/3
$site_args{'log'} || 'Mojo::Log'->new('level', 'warn', 'max_history_size', 500)
l!l&&r!l&&!r
0260
MC/DC0%0/2
$site_args{'log'} || 'Mojo::Log'->new('level', 'warn', 'max_history_size', 500)
$site_args{'log'} 'Mojo::Log'->new('level', 'warn', 'max_history_size', 500)
Truth table33%1/3
A || B
A= $site_args{'log'}B= 'Mojo::Log'->new('level', 'warn', 'max_history_size', 500)
ABresult
1X1
011
000
38 level => 'warn',
39 max_history_size => 500,
40 );
41  
42 return Statocles::Site->new(
43 title => 'Example Site',
44 store => $site_args{store},
45 build_store => $site_args{build_store},
46 deploy => $site_args{deploy},
47 26 log => $log,
48 %site_args,
49 );
50 }
51  
52 #pod =sub build_test_site_apps
53 #pod
54 #pod my ( $site, $build_dir, $deploy_dir ) = build_test_site_apps( $share_dir, %site_args );
55 #pod
56 #pod Build a site for testing, with some apps. Returns the site, the build dir, and the
57 #pod deploy dir.
58 #pod
59 #pod =cut
60  
61 0 ▶sub build_test_site_apps {
Subroutine build_test_site_apps: not called CC=3 SCAR=24.8
Pod documented
62 0 my ( $share_dir, %site_args ) = @_;
63  
64 0 my $build_dir = Path::Tiny->tempdir;
65 0 my $deploy_dir = Path::Tiny->tempdir;
66  
67 0 ▶ $site_args{store} //= Path::Tiny->tempdir;
Condition0%0/3
$site_args{"store"} //= "Path::Tiny"->tempdir
l!l&&r!l&&!r
000
MC/DC0%0/2
$site_args{"store"} //= "Path::Tiny"->tempdir
$site_args{"store"} "Path::Tiny"->tempdir
Truth table0%0/3
A //= B
A= $site_args{"store"}B= "Path::Tiny"->tempdir
ABresult
1X1
011
000
68 0 $site_args{build_store} = $build_dir;
69 0 $site_args{deploy} = $deploy_dir;
70  
71 0 ▶ if ( !$site_args{apps} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($site_args{"apps"}) 0 0 0
72 0 require Statocles::App::Blog;
73 0 my $blog = Statocles::App::Blog->new(
74 url_root => '/blog',
75 page_size => 2,
76 );
77  
78 $site_args{apps} = {
79 0 blog => $blog,
80 };
81 }
82  
83 return (
84 build_test_site(
85 theme => $share_dir->child( 'theme' ),
86 build_store => delete $site_args{build_store},
87 deploy => delete $site_args{deploy},
88 0 %site_args,
89 ),
90 $build_dir,
91 $deploy_dir,
92 );
93 }
94  
95  
96 1 ▶sub test_constructor {
Subroutine test_constructor: called CC=3 SCAR=12.6
Pod undocumented
97 1 my ( $class, %args ) = @_;
98 1 derp 'Statocles::Test::test_constructor is deprecated and will be removed in v1.000';
99 1 ▶ my %required = $args{required} ? ( %{ $args{required} } ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$args{'required'} ? : 1 0 1
100 0 ▶ my %defaults = $args{default} ? ( %{ $args{default} } ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$args{'default'} ? : 0 1 1
101 1 require Test::Builder;
102 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
103  
104 1 my $tb = Test::Builder->new();
105  
106 1 ▶ $tb->subtest( $class . ' constructor' => sub {
Subroutine __ANON__: called CC=7 SCAR=24.6
107 1 my $got = $class->new( %required );
108 1 my $want = $class;
109 1 my $typeof = do {
110 !defined $got ? 'undefined'
111 : !ref $got ? 'scalar'
112 : !Scalar::Util::blessed($got) ? ref $got
113 1 ▶ : eval { $got->isa($want) } ? $want
Branch: 4 branches
BranchTrueFalseTotal
!defined($got) ? : 0 1 1
!ref($got) ? : 0 1 1
!builtin::blessed($got) ? : 0 1 1
eval { do { $got->isa($want) } } ? : 1 0 1
114 : Scalar::Util::blessed($got);
115 };
116 1 $tb->is_eq($typeof, $class, 'constructor works with all required args');
117  
118 1 ▶ if ( $args{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"required"}) 1 0 1
119 1 ▶ $tb->subtest( 'required attributes' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
120 1 for my $key ( keys %required ) {
121 1 require Test::Exception;
122 1 ▶ &Test::Exception::dies_ok(sub {
Subroutine __ANON__: called CC=1 SCAR=0.4
123 $class->new(
124 0 map {; $_ => $required{ $_ } } grep { $_ ne $key } keys %required,
125 );
126 1 }, $key . ' is required');
127 }
128 1 });
129 }
130  
131 1 ▶ if ( $args{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"default"}) 0 1 1
132 0 ▶ $tb->subtest( 'attribute defaults' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
133 0 my $obj = $class->new( %required );
134 0 for my $key ( keys %defaults ) {
135 0 ▶ if ( ref $defaults{ $key } eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defaults{$key} eq 'CODE') { } 0 0 0
136 0 local $_ = $obj->$key;
137 0 $tb->subtest( "$key default value" => $defaults{ $key } );
138 }
139 else {
140 0 require Test::Deep;
141 0 Test::Deep::cmp_deeply( $obj->$key, $defaults{ $key }, "$key default value" );
142 }
143 }
144 0 });
145 }
146  
147 1 });
148 }
149  
150 1 ▶sub test_pages {
Subroutine test_pages: called CC=16 SCAR=48.1
Pod undocumented
151 1 my ( $site, $app ) = ( shift, shift );
152 1 derp 'Statocles::Test::test_pages is deprecated and will be removed in v1.000';
153 1 require Test::Builder;
154  
155 1 my %opt;
156 1 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 1 0 1
157 1 %opt = %{ +shift };
158 }
159  
160 1 my %page_tests = @_;
161  
162 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
163  
164 1 my $tb = Test::Builder->new();
165  
166 1 my @warnings;
167 0 ▶ local $SIG{__WARN__} = sub { push @warnings, $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
168  
169 1 my @pages = $app->pages;
170  
171 1 $tb->is_eq( scalar @pages, scalar keys %page_tests, 'correct number of pages' );
172  
173 1 for my $page ( @pages ) {
174 0 $tb->ok( $page->DOES( 'Statocles::Page' ), 'must be a Statocles::Page' );
175  
176 0 my $date = $page->date;
177 0 my $want = 'DateTime::Moonpig';
178 0 my $typeof = do {
179 !defined $date ? 'undefined'
180 : !ref $date ? 'scalar'
181 : !Scalar::Util::blessed($date) ? ref $date
182 0 ▶ : eval { $date->isa($want) } ? $want
Branch: 4 branches
BranchTrueFalseTotal
!defined($date) ? : 0 0 0
!ref($date) ? : 0 0 0
!builtin::blessed($date) ? : 0 0 0
eval { do { $date->isa($want) } } ? : 0 0 0
183 : Scalar::Util::blessed($date);
184 };
185 0 $tb->is_eq( $typeof, $want, 'must set a date' );
186  
187 0 ▶ if ( !$page_tests{ $page->path } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page_tests{$page->path}) 0 0 0
188 0 $tb->ok( 0, "No tests found for page: " . $page->path );
189 0 next;
190 }
191  
192 0 my $output;
193  
194 0 ▶ if ( $page->has_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->has_dom) { } 0 0 0
195 0 $output = "".$page->dom;
196 }
197 else {
198 0 $output = $page->render;
199 # Handle filehandles from render
200 0 ▶ if ( ref $output eq 'GLOB' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $output eq 'GLOB') { } 0 0 0
elsif (builtin::blessed($output) and $output->isa('Path::Tiny')) { } 0 0 0
201 0 $output = do { local $/; <$output> };
202 }
203 # Handle Path::Tiny from render
204 ▶ elsif ( Scalar::Util::blessed( $output ) && $output->isa( 'Path::Tiny' ) ) {
Condition0%0/3
builtin::blessed($output) and $output->isa("Path::Tiny")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($output) and $output->isa("Path::Tiny")
builtin::blessed($output) $output->isa("Path::Tiny")
Truth table0%0/3
A and B
A= builtin::blessed($output)B= $output->isa("Path::Tiny")
ABresult
0X0
100
111
205 0 $output = $output->slurp_raw;
206 }
207 }
208  
209 0 ▶ if ( $page->path =~ /[.](?:html|rss|atom)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($page->path =~ /[.](?:html|rss|atom)$/) { } 0 0 0
elsif ($page_tests{$page->path}) { } 0 0 0
210 0 my $dom = Mojo::DOM->new( $output );
211 0 ▶ $tb->ok( 0, "Could not parse dom" ) unless $dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $dom 0 0 0
212 0 $tb->subtest( 'html content: ' . $page->path, $page_tests{ $page->path }, $output, $dom );
213 }
214 elsif ( $page_tests{ $page->path } ) {
215 0 $tb->subtest( 'text content: ' . $page->path, $page_tests{ $page->path }, $output );
216 }
217 else {
218 0 $tb->ok( 0, "Unknown page: " . $page->path );
219 }
220  
221 }
222  
223 1 ▶ $tb->ok( !@warnings, "no warnings!" ) or $tb->diag( join "\n", @warnings );
Branch: 1 branch
BranchTrueFalseTotal
$tb->ok(!@warnings, "no warnings!") or $tb->diag(join("\n", @warnings)) 1 0 1
224 }
225  
226  
227 #pod =sub build_temp_site
228 #pod
229 #pod my ( $tmpdir, $config_fn, $config ) = build_temp_site( $share_dir );
230 #pod
231 #pod Build a config file so we can test config loading and still use
232 #pod temporary directories
233 #pod
234 #pod =cut
235  
236 6 ▶sub build_temp_site {
Subroutine build_temp_site: called CC=2 SCAR=6.9
Pod documented
237 6 my ( $share_dir ) = @_;
238  
239 6 ▶ my $tmp = Path::Tiny->tempdir( CLEANUP => $ENV{STATOCLES_TEST_CLEANUP} // 1 );
Condition50%1/2
$ENV{"STATOCLES_TEST_CLEANUP"} // 1
l!l
06
MC/DC0%0/1
$ENV{"STATOCLES_TEST_CLEANUP"} // 1
$ENV{"STATOCLES_TEST_CLEANUP"}
Truth table50%1/2
A
A= $ENV{"STATOCLES_TEST_CLEANUP"}
Aresult
11
00
240 6 dircopy $share_dir->child( 'theme' ), $tmp->child( 'theme' );
241 6 $tmp->child( 'build_site' )->mkpath;
242 6 $tmp->child( 'deploy_site' )->mkpath;
243 6 $tmp->child( 'build_foo' )->mkpath;
244 6 $tmp->child( 'deploy_foo' )->mkpath;
245  
246 6 my $config = {
247 theme => {
248 class => 'Statocles::Theme',
249 args => {
250 store => "".$tmp->child( 'theme' ),
251 },
252 },
253  
254 build => {
255 class => 'Statocles::Store',
256 args => {
257 path => "".$tmp->child( 'build_site' ),
258 },
259 },
260  
261 deploy => {
262 class => 'Statocles::Deploy::File',
263 args => {
264 path => "".$tmp->child( 'deploy_site' ),
265 },
266 },
267  
268 blog => {
269 'class' => 'Statocles::App::Blog',
270 'args' => {
271 url_root => '/blog',
272 },
273 },
274  
275 site => {
276 class => 'Statocles::Site',
277 args => {
278 title => 'Site Title',
279 index => '/blog',
280 build_store => { '$ref' => 'build' },
281 deploy => { '$ref' => 'deploy' },
282 theme => { '$ref' => 'theme' },
283 apps => {
284 blog => { '$ref' => 'blog' },
285 },
286 store => {
287 '$class' => 'Statocles::Store',
288 '$args' => {
289 path => $tmp,
290 },
291 },
292 },
293 },
294  
295 build_foo => {
296 class => 'Statocles::Store',
297 args => {
298 path => "".$tmp->child( 'build_foo' ),
299 },
300 },
301  
302 deploy_foo => {
303 class => 'Statocles::Deploy::File',
304 args => {
305 path => "".$tmp->child( 'deploy_foo' ),
306 },
307 },
308  
309 site_foo => {
310 class => 'Statocles::Site',
311 args => {
312 title => 'Site Foo',
313 index => '/blog',
314 build_store => { '$ref' => 'build_foo' },
315 deploy => { '$ref' => 'deploy_foo' },
316 theme => '::default',
317 apps => {
318 blog => { '$ref' => 'blog' },
319 },
320 store => {
321 '$class' => 'Statocles::Store',
322 '$args' => {
323 path => $tmp,
324 },
325 },
326 },
327 },
328 };
329  
330 6 my $config_fn = $tmp->child( 'site.yml' );
331 6 YAML::DumpFile( $config_fn, $config );
332 6 return ( $tmp, $config_fn, $config );
333 }
334  
335 58 1;
336  
337 __END__
« blib/lib/Statocles/Command/daemon.pm ↑ Summary blib/lib/Statocles/App/Blog.pm »