| 1 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | package Dist::Zilla::Plugin::Test::Inline; | ||||||||||
| 3 | # ABSTRACT: Create test files from inline tests in POD sections | ||||||||||
| 4 | # VERSION | ||||||||||
| 5 | 1 | $Dist::Zilla::Plugin::Test::Inline::VERSION = '0.011005'; | |||||||||
| 6 | #pod =head1 SYNOPSIS | ||||||||||
| 7 | #pod | ||||||||||
| 8 | #pod In your C<dist.ini>: | ||||||||||
| 9 | #pod | ||||||||||
| 10 | #pod [Test::Inline] | ||||||||||
| 11 | #pod | ||||||||||
| 12 | #pod In your module: | ||||||||||
| 13 | #pod | ||||||||||
| 14 | #pod # My/AddressRange.pm | ||||||||||
| 15 | #pod | ||||||||||
| 16 | #pod =begin testing | ||||||||||
| 17 | #pod | ||||||||||
| 18 | #pod use Test::Exception; | ||||||||||
| 19 | #pod dies_ok { | ||||||||||
| 20 | #pod My::AddressRange->list_from_range('10.2.3.A', '10.2.3.5') | ||||||||||
| 21 | #pod } "list_from_range() complains about invalid address"; | ||||||||||
| 22 | #pod | ||||||||||
| 23 | #pod =end testing | ||||||||||
| 24 | #pod | ||||||||||
| 25 | #pod =cut | ||||||||||
| 26 | #pod | ||||||||||
| 27 | #pod sub list_from_range { | ||||||||||
| 28 | #pod # ... | ||||||||||
| 29 | #pod } | ||||||||||
| 30 | #pod | ||||||||||
| 31 | #pod This will result in a file C<t/inline-tests/my_addressrange.t> in your distribution. | ||||||||||
| 32 | #pod | ||||||||||
| 33 | #pod =head1 DESCRIPTION | ||||||||||
| 34 | #pod | ||||||||||
| 35 | #pod This plugin integrates L<Test::Inline> into C<Dist::Zilla>. | ||||||||||
| 36 | #pod | ||||||||||
| 37 | #pod It scans all non-binary files in the lib path of your distribution for inline | ||||||||||
| 38 | #pod tests in POD sections that are embedded between the keywords | ||||||||||
| 39 | #pod | ||||||||||
| 40 | #pod =begin testing | ||||||||||
| 41 | #pod ... | ||||||||||
| 42 | #pod =end testing | ||||||||||
| 43 | #pod | ||||||||||
| 44 | #pod These tests are then exported into C<t/inline-tests/*.t> files when | ||||||||||
| 45 | #pod C<Dist::Zilla> builds your module. Multiple of test sections may be specified | ||||||||||
| 46 | #pod within one file. | ||||||||||
| 47 | #pod | ||||||||||
| 48 | #pod Please note that this plugin (in contrast to pure L<Test::Inline>) can also | ||||||||||
| 49 | #pod handle L<Moops>-like class and role definitions. | ||||||||||
| 50 | #pod | ||||||||||
| 51 | #pod =head2 Files to be scanned for inline tests | ||||||||||
| 52 | #pod | ||||||||||
| 53 | #pod Only files already gathered by previous file gatherer plugins are scanned. In | ||||||||||
| 54 | #pod other words: tests will not be extracted for files which have been excluded. | ||||||||||
| 55 | #pod | ||||||||||
| 56 | #pod Example: | ||||||||||
| 57 | #pod | ||||||||||
| 58 | #pod [GatherDir] | ||||||||||
| 59 | #pod exclude_match = Hidden\.pm | ||||||||||
| 60 | #pod [Test::Inline] | ||||||||||
| 61 | #pod | ||||||||||
| 62 | #pod This will lead to C<Dist::Zilla::Plugin::Test::Inline> ignoring C<Hidden.pm>. | ||||||||||
| 63 | #pod | ||||||||||
| 64 | #pod =head1 ACKNOWLEDGEMENTS | ||||||||||
| 65 | #pod | ||||||||||
| 66 | #pod The code of this Dist::Zilla file gatherer plugin is based on | ||||||||||
| 67 | #pod L<https://github.com/moose/moose/blob/master/inc/ExtractInlineTests.pm>. | ||||||||||
| 68 | #pod | ||||||||||
| 69 | #pod =for :list | ||||||||||
| 70 | #pod * Dave Rolsky <autarch@urth.org>, who basically wrote most of this but left the | ||||||||||
| 71 | #pod honor of making a plugin out of it to me ;-) | ||||||||||
| 72 | #pod | ||||||||||
| 73 | #pod =cut | ||||||||||
| 74 | |||||||||||
| 75 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 1 | with | |||||||||
| 77 | # FileGatherer: turn this module as a Dist::Zilla plugin | ||||||||||
| 78 | 'Dist::Zilla::Role::FileGatherer', | ||||||||||
| 79 | # FileFinderUser: add $self->found_files function to this module | ||||||||||
| 80 | 'Dist::Zilla::Role::FileFinderUser' => { # where to take input files from | ||||||||||
| 81 | default_finders => [ ':InstallModules', ':ExecFiles' ], | ||||||||||
| 82 | }, | ||||||||||
| 83 | # PrereqSource: allows to register prerequisites | ||||||||||
| 84 | 'Dist::Zilla::Role::PrereqSource', | ||||||||||
| 85 | ; | ||||||||||
| 86 | |||||||||||
| 87 | #pod =method gather_files | ||||||||||
| 88 | #pod | ||||||||||
| 89 | #pod Required by role L<Dist::Zilla::Role::FileGatherer>. | ||||||||||
| 90 | #pod | ||||||||||
| 91 | #pod Searches for inline test code in POD sections using L<Test::Inline>, creates | ||||||||||
| 92 | #pod in-memory test files and passes them to L<Dist::Zilla>. | ||||||||||
| 93 | #pod | ||||||||||
| 94 | #pod =cut | ||||||||||
| 95 | 1 | ▶ | sub gather_files { | ||||||||
|
Subroutine
gather_files: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 96 | 1 | my $self = shift; | |||||||||
| 97 | 1 | my $arg = shift; | |||||||||
| 98 | |||||||||||
| 99 | 1 | ▶ | use Test::Inline; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 100 | |||||||||||
| 101 | # give Test::Inline our own extract and output handlers | ||||||||||
| 102 | 1 | my $inline = Test::Inline->new( | |||||||||
| 103 | verbose => 0, | ||||||||||
| 104 | ExtractHandler => 'Dist::Zilla::Plugin::Test::Inline::Extract', | ||||||||||
| 105 | OutputHandler => Dist::Zilla::Plugin::Test::Inline::Output->new($self), | ||||||||||
| 106 | ); | ||||||||||
| 107 | |||||||||||
| 108 | # all files in the dist that match above filters (':InstallModules', ':ExecFiles') | ||||||||||
| 109 | 1 | for my $file (@{$self->found_files}) { | |||||||||
| 110 | 1 | ▶ | next if $file->is_bytes; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1 | ▶ | $inline->add(\$file->content) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | and $self->log("extracted inline tests from ".$file->name); | ||||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | # add test files to Dist::Zilla distribution | ||||||||||
| 116 | 1 | $inline->save; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | #pod =method register_prereqs | ||||||||||
| 120 | #pod | ||||||||||
| 121 | #pod Required by role L<Dist::Zilla::Role::PrereqSource>. | ||||||||||
| 122 | #pod | ||||||||||
| 123 | #pod Adds L<Test::More> to the list of prerequisites (as L<Test::Inline> inserts | ||||||||||
| 124 | #pod C<use Test::More;>) for the distribution that uses this plugin. | ||||||||||
| 125 | #pod | ||||||||||
| 126 | #pod =cut | ||||||||||
| 127 | 1 | ▶ | sub register_prereqs { | ||||||||
|
Subroutine
register_prereqs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 128 | 1 | my $self = shift; | |||||||||
| 129 | 1 | $self->zilla->register_prereqs( | |||||||||
| 130 | { | ||||||||||
| 131 | type => 'requires', | ||||||||||
| 132 | phase => 'test', | ||||||||||
| 133 | }, | ||||||||||
| 134 | 'Test::More' => 0, | ||||||||||
| 135 | ); | ||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | # | ||||||||||
| 139 | # Used to connect Test::Inline to Dist::Zilla | ||||||||||
| 140 | # (write generated test code into in-memory files) | ||||||||||
| 141 | # | ||||||||||
| 142 | { | ||||||||||
| 143 | package Dist::Zilla::Plugin::Test::Inline::Output; | ||||||||||
| 144 | 1 | $Dist::Zilla::Plugin::Test::Inline::Output::VERSION = '0.011005'; | |||||||||
| 145 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||
| 146 | 1 | my $class = shift; | |||||||||
| 147 | 1 | my $dzil = shift; | |||||||||
| 148 | |||||||||||
| 149 | 1 | return bless { dzil => $dzil }, $class; | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 1 | ▶ | sub write { | ||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
| |||||||||||
| 153 | 1 | my $self = shift; | |||||||||
| 154 | 1 | my $name = shift; | |||||||||
| 155 | 1 | my $content = shift; | |||||||||
| 156 | |||||||||||
| 157 | 1 | ▶ | use Dist::Zilla::File::InMemory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 158 | |||||||||||
| 159 | $self->{dzil}->add_file( | ||||||||||
| 160 | 1 | Dist::Zilla::File::InMemory->new( | |||||||||
| 161 | name => "t/inline-tests/$name", | ||||||||||
| 162 | content => $content, | ||||||||||
| 163 | ) | ||||||||||
| 164 | ); | ||||||||||
| 165 | |||||||||||
| 166 | 1 | return 1; | |||||||||
| 167 | } | ||||||||||
| 168 | } | ||||||||||
| 169 | # | ||||||||||
| 170 | # Taken from https://github.com/moose/Moose/blob/master/inc/MyInline.pm | ||||||||||
| 171 | # | ||||||||||
| 172 | { | ||||||||||
| 173 | 1 | package Dist::Zilla::Plugin::Test::Inline::Extract; | |||||||||
| 174 | 1 | $Dist::Zilla::Plugin::Test::Inline::Extract::VERSION = '0.011005'; | |||||||||
| 175 | 1 | ▶ | use parent 'Test::Inline::Extract'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 176 | |||||||||||
| 177 | # Extract code specifically marked for testing | ||||||||||
| 178 | 1 | our $search = qr/ | |||||||||
| 179 | (?:^|\n) # After the beginning of the string, or a newline | ||||||||||
| 180 | ( # ... start capturing | ||||||||||
| 181 | # EITHER | ||||||||||
| 182 | package\s+ # A package | ||||||||||
| 183 | [^\W\d]\w*(?:(?:\'|::)[^\W\d]\w*)* # ... with a name | ||||||||||
| 184 | \s*; # And a statement terminator | ||||||||||
| 185 | | # OR | ||||||||||
| 186 | class\s+ # A class | ||||||||||
| 187 | [^\W\d]\w*(?:(?:\'|::)[^\W\d]\w*)* # ... with a name | ||||||||||
| 188 | ($|\s+|\s*{) # And some spaces or an opening bracket | ||||||||||
| 189 | | # OR | ||||||||||
| 190 | role\s+ # A role | ||||||||||
| 191 | [^\W\d]\w*(?:(?:\'|::)[^\W\d]\w*)* # ... with a name | ||||||||||
| 192 | ($|\s+|\s*{) # And some spaces or an opening bracket | ||||||||||
| 193 | | # OR | ||||||||||
| 194 | =for[ \t]+example[ \t]+begin\n # ... when we find a =for example begin | ||||||||||
| 195 | .*? # ... and keep capturing | ||||||||||
| 196 | \n=for[ \t]+example[ \t]+end\s*? # ... until the =for example end | ||||||||||
| 197 | (?:\n|$) # ... at the end of file or a newline | ||||||||||
| 198 | | # OR | ||||||||||
| 199 | =begin[ \t]+(?:test|testing)\b # ... when we find a =begin test or testing | ||||||||||
| 200 | .*? # ... and keep capturing | ||||||||||
| 201 | \n=end[ \t]+(?:test|testing)\s*? # ... until an =end tag | ||||||||||
| 202 | (?:\n|$) # ... at the end of file or a newline | ||||||||||
| 203 | ) # ... and stop capturing | ||||||||||
| 204 | /isx; | ||||||||||
| 205 | |||||||||||
| 206 | 1 | ▶ | sub _elements { | ||||||||
|
Subroutine
_elements: called CC=3 SCAR=11.0
| |||||||||||
| 207 | 1 | my $self = shift; | |||||||||
| 208 | 1 | my @elements = (); | |||||||||
| 209 | 1 | ▶ | while ( $self->{source} =~ m/$search/go ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 210 | 2 | my $element = $1; | |||||||||
| 211 | # rename "role" or "class" to "package" so Test::Inline understands | ||||||||||
| 212 | 2 | $element =~ s/^(role|class)(\s+)/package$2/; | |||||||||
| 213 | 2 | $element =~ s/\n\s*$//; | |||||||||
| 214 | 2 | push @elements, $element; | |||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | 1 | ▶ | (List::Util::first { /^=/ } @elements) ? \@elements : ''; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||
| 218 | } | ||||||||||
| 219 | |||||||||||
| 220 | } | ||||||||||
| 221 | |||||||||||
| 222 | |||||||||||
| 223 | 1 | 1; | |||||||||
| 224 | |||||||||||
| 225 | __END__ | ||||||||||