File Coverage

blib/lib/Dist/Zilla/Plugin/Stenciller/MojoliciousTests.pm
Criterion Covered Total %
statement 40 40 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod 0 1 0.0
total 51 52 98.0


line stmt bran cond sub pod time code
1 1     1   1714748 use 5.10.0;
  1         3  
2 1     1   3 use warnings;
  1         1  
  1         49  
3              
4             package Dist::Zilla::Plugin::Stenciller::MojoliciousTests;
5              
6             # ABSTRACT: Create Mojolicious tests from text files parsed with Stenciller
7             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
8             our $VERSION = '0.0200';
9              
10 1     1   4 use Moose;
  1         1  
  1         11  
11             with 'Dist::Zilla::Role::FileGatherer';
12 1     1   4693 use Stenciller;
  1         366122  
  1         38  
13 1     1   8 use Types::Stenciller -types;
  1         1  
  1         9  
14 1     1   439 use Types::Standard qw/Str/;
  1         2  
  1         4  
15 1     1   455 use Types::Path::Tiny qw/AbsFile Dir Path/;
  1         1  
  1         6  
16 1     1   400 use Path::Tiny;
  1         1  
  1         52  
17 1     1   462 use Dist::Zilla::File::InMemory;
  1         52631  
  1         37  
18 1     1   15 use namespace::autoclean;
  1         1  
  1         6  
19              
20             has source_directory => (
21             is => 'ro',
22             isa => Dir,
23             coerce => 1,
24             default => 'examples/source',
25             );
26             has file_pattern => (
27             is => 'ro',
28             isa => Str,
29             default => '.+\.stencil',
30             );
31             has output_directory => (
32             is => 'ro',
33             isa => Path,
34             coerce => 1,
35             default => 't',
36             );
37             has template_file => (
38             is => 'ro',
39             isa => AbsFile,
40             lazy => 1,
41             coerce => 1,
42             default => sub { shift->source_directory->child('template.test')->absolute },
43             );
44              
45             sub gather_files {
46 1     1 0 37573 my $self = shift;
47              
48 1         40 my $template = $self->template_file->slurp_utf8;
49 1         189 my @source_files = $self->source_directory->children(qr{^@{[ $self->file_pattern ]}$});
  1         36  
50              
51 1         121 $self->log('Generating tests from stencils');
52              
53 1         219 foreach my $file (@source_files) {
54 1         6 my $contents = Stenciller->new(filepath => $file->stringify)->transform(
55             plugin_name => 'ToMojoliciousTest',
56             transform_args => {
57             require_in_extra => {
58             key => 'is_test',
59             value => 1,
60             default => 1
61             },
62             },
63             constructor_args => {
64             template => $self->template_file,
65             },
66             );
67              
68 1         18249 my $new_filename = $file->basename(qr/\.[^.]+$/) . '.t';
69 1         212 $self->log("Generated $new_filename");
70              
71 1         353 my $generated_file = Dist::Zilla::File::InMemory->new(
72             name => path($self->output_directory, $new_filename)->stringify,
73             content => $contents,
74             );
75 1         306 $self->add_file($generated_file);
76              
77             }
78             }
79              
80             __PACKAGE__->meta->make_immutable;
81              
82             1;
83              
84             __END__
85              
86             =pod
87              
88             =encoding UTF-8
89              
90             =head1 NAME
91              
92             Dist::Zilla::Plugin::Stenciller::MojoliciousTests - Create Mojolicious tests from text files parsed with Stenciller
93              
94              
95              
96             =begin html
97              
98             <p>
99             <img src="https://img.shields.io/badge/perl-5.10+-blue.svg" alt="Requires Perl 5.10+" />
100             <a href="https://travis-ci.org/Csson/p5-Dist-Zilla-Plugin-Stenciller-MojoliciousTests"><img src="https://api.travis-ci.org/Csson/p5-Dist-Zilla-Plugin-Stenciller-MojoliciousTests.svg?branch=master" alt="Travis status" /></a>
101             <a href="http://cpants.cpanauthors.org/dist/Dist-Zilla-Plugin-Stenciller-MojoliciousTests-0.0200"><img src="https://badgedepot.code301.com/badge/kwalitee/Dist-Zilla-Plugin-Stenciller-MojoliciousTests/0.0200" alt="Distribution kwalitee" /></a>
102             <a href="http://matrix.cpantesters.org/?dist=Dist-Zilla-Plugin-Stenciller-MojoliciousTests%200.0200"><img src="https://badgedepot.code301.com/badge/cpantesters/Dist-Zilla-Plugin-Stenciller-MojoliciousTests/0.0200" alt="CPAN Testers result" /></a>
103             <img src="https://img.shields.io/badge/coverage-98.1%-yellow.svg" alt="coverage 98.1%" />
104             </p>
105              
106             =end html
107              
108             =head1 VERSION
109              
110             Version 0.0200, released 2016-03-23.
111              
112             =head1 SYNOPSIS
113              
114             ; in dist.ini
115             ; these are the defaults:
116             [Stenciller::MojoliciousTests]
117             source_directory = examples/source
118             file_pattern = .+\.stencil
119             template_file = examples/source/template.test
120             output_directory = t
121              
122             =head1 DESCRIPTION
123              
124             Dist::Zilla::Plugin::Stenciller::MojoliciousTests uses L<Stenciller> and L<Stenciller::Plugin::ToMojoliciousTest> to turn
125             stencil files in C<source_directory> (that matches the C<file_pattern>) into
126             test files in C<output_directory> by applying the C<template_file>.
127              
128             This L<Dist::Zilla> plugin does the C<FileGatherer> role.
129              
130             =head1 ATTRIBUTES
131              
132             =head2 source_directory
133              
134             Path to where the stencil files are.
135              
136             =head2 output_directory
137              
138             Path to where the generated files are saved.
139              
140             =head2 file_pattern
141              
142             This is put inside a regular expression (with start and end anchors) to find stencil files in the C<source_directory>. The output files
143             will have the same basename, but the suffix is replaced by C<t>.
144              
145             =head2 template_file
146              
147             The template file should contain use statements and such. The transformed contents returned from L<Stenciller::Plugin::ToMojoliciousTest> will be placed after
148             the contents of C<template_file>. The template file is applied to each stencil file, so the number of generated test files is equal
149             to the number of stencil files.
150              
151             =head1 SEE ALSO
152              
153             =over 4
154              
155             =item *
156              
157             L<Stenciller>
158              
159             =item *
160              
161             L<Stenciller::Plugin::ToMojoliciousTest>
162              
163             =back
164              
165             =head1 SOURCE
166              
167             L<https://github.com/Csson/p5-Dist-Zilla-Plugin-Stenciller-MojoliciousTests>
168              
169             =head1 HOMEPAGE
170              
171             L<https://metacpan.org/release/Dist-Zilla-Plugin-Stenciller-MojoliciousTests>
172              
173             =head1 AUTHOR
174              
175             Erik Carlsson <info@code301.com>
176              
177             =head1 COPYRIGHT AND LICENSE
178              
179             This software is copyright (c) 2016 by Erik Carlsson.
180              
181             This is free software; you can redistribute it and/or modify it under
182             the same terms as the Perl 5 programming language system itself.
183              
184             =cut