File Coverage

blib/lib/Test/Data/Split.pm
Criterion Covered Total %
statement 27 28 96.4
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 36 38 94.7


line stmt bran cond sub pod time code
1             package Test::Data::Split;
2             $Test::Data::Split::VERSION = '0.2.2';
3 3     3   172274 use strict;
  3         27  
  3         76  
4 3     3   13 use warnings;
  3         5  
  3         100  
5 3     3   1178 use autodie;
  3         38945  
  3         11  
6              
7 3     3   16994 use 5.008;
  3         19  
8              
9 3     3   1396 use IO::All qw/ io /;
  3         35490  
  3         23  
10              
11              
12 3     3   1362 use MooX qw/ late /;
  3         25355  
  3         17  
13              
14             has '_target_dir' =>
15             ( is => 'ro', isa => 'Str', required => 1, init_arg => 'target_dir', );
16             has ['_filename_cb'] =>
17             ( is => 'ro', isa => 'CodeRef', required => 1, init_arg => 'filename_cb', );
18             has ['_contents_cb'] =>
19             ( is => 'ro', isa => 'CodeRef', required => 1, init_arg => 'contents_cb', );
20             has '_data_obj' => ( is => 'ro', required => 1, init_arg => 'data_obj' );
21              
22              
23             sub run
24             {
25 4     4 1 11825 my $self = shift;
26              
27 4         18 my $target_dir = $self->_target_dir;
28 4         12 my $filename_cb = $self->_filename_cb;
29 4         9 my $contents_cb = $self->_contents_cb;
30              
31 4         12 my $data_obj = $self->_data_obj;
32              
33 4         10 foreach my $id ( @{ $data_obj->list_ids() } )
  4         12  
34             {
35             # Croak on bad IDs.
36 17 50       15224 if ( $id !~ /\A[A-Za-z_\-0-9]{1,80}\z/ )
37             {
38 0         0 die "Invalid id '$id'.";
39             }
40              
41 17         50 io->catfile( $target_dir, $filename_cb->( $self, { id => $id, }, ) )
42             ->assert->print(
43             $contents_cb->(
44             $self, { id => $id, data => $data_obj->lookup_data($id) },
45             )
46             );
47             }
48              
49 4         1946 return;
50             }
51              
52             1;
53              
54             __END__
55              
56             =pod
57              
58             =encoding UTF-8
59              
60             =head1 NAME
61              
62             Test::Data::Split - split data-driven tests into several test scripts.
63              
64             =head1 VERSION
65              
66             version 0.2.2
67              
68             =head1 SYNOPSIS
69              
70             use Test::Data::Split;
71              
72             # Implements Test::Data::Split::Backend::Hash
73             use MyTest;
74              
75             my $tests_dir = "./t";
76              
77             my $obj = Test::Data::Split->new(
78             {
79             target_dir => $tests_dir,
80             filename_cb => sub {
81             my ($self, $args) = @_;
82              
83             my $id = $args->{id};
84              
85             return "valgrind-$id.t";
86             },
87             contents_cb => sub {
88             my ($self, $args) = @_;
89              
90             my $id = $args->{id};
91              
92             return <<"EOF";
93             #!/usr/bin/perl
94              
95             use strict;
96             use warnings;
97              
98             use Test::More tests => 1;
99             use MyTest;
100              
101             @{['# TEST']}
102             MyTest->run_id(qq#$id#);
103              
104             EOF
105             },
106             data_obj => MyTest->new,
107             }
108             );
109              
110             $obj->run;
111              
112             # And later in the shell:
113             prove t/*.t
114              
115             =head1 DESCRIPTION
116              
117             This module splits a set of data with IDs and arbitrary values into one
118             test file per (key+value) for easy parallelisation.
119              
120             =head1 METHODS
121              
122             =head2 my $obj = Test::Data::Split->new({ %PARAMS })
123              
124             Accepts the following parameters:
125              
126             =over 4
127              
128             =item * target_dir
129              
130             The path to the target directory - a string.
131              
132             =item * filename_cb
133              
134             A subroutine references that accepts C<< ($self, {id => $id }) >>
135             and returns the filename.
136              
137             =item * contents_cb
138              
139             A subroutine references that accepts
140             C<< ($self, {id => $id, data => $data }) >> and returns the contents inside
141             the file.
142              
143             =item * data_obj
144              
145             An object reference that implements the C<< ->list_ids() >> methods
146             that returns an array reference of IDs to generate as files.
147              
148             =back
149              
150             An example for using it can be found in the synopsis.
151              
152             =head2 $self->run()
153              
154             Generate the files.
155              
156             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
157              
158             =head1 SUPPORT
159              
160             =head2 Websites
161              
162             The following websites have more information about this module, and may be of help to you. As always,
163             in addition to those websites please use your favorite search engine to discover more resources.
164              
165             =over 4
166              
167             =item *
168              
169             MetaCPAN
170              
171             A modern, open-source CPAN search engine, useful to view POD in HTML format.
172              
173             L<https://metacpan.org/release/Test-Data-Split>
174              
175             =item *
176              
177             RT: CPAN's Bug Tracker
178              
179             The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
180              
181             L<https://rt.cpan.org/Public/Dist/Display.html?Name=Test-Data-Split>
182              
183             =item *
184              
185             CPANTS
186              
187             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
188              
189             L<http://cpants.cpanauthors.org/dist/Test-Data-Split>
190              
191             =item *
192              
193             CPAN Testers
194              
195             The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
196              
197             L<http://www.cpantesters.org/distro/T/Test-Data-Split>
198              
199             =item *
200              
201             CPAN Testers Matrix
202              
203             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
204              
205             L<http://matrix.cpantesters.org/?dist=Test-Data-Split>
206              
207             =item *
208              
209             CPAN Testers Dependencies
210              
211             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
212              
213             L<http://deps.cpantesters.org/?module=Test::Data::Split>
214              
215             =back
216              
217             =head2 Bugs / Feature Requests
218              
219             Please report any bugs or feature requests by email to C<bug-test-data-split at rt.cpan.org>, or through
220             the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Test-Data-Split>. You will be automatically notified of any
221             progress on the request by the system.
222              
223             =head2 Source Code
224              
225             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
226             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
227             from your repository :)
228              
229             L<https://github.com/shlomif/perl-Test-Data-Split>
230              
231             git clone git://github.com/shlomif/perl-Test-Data-Split.git
232              
233             =head1 AUTHOR
234              
235             Shlomi Fish <shlomif@cpan.org>
236              
237             =head1 BUGS
238              
239             Please report any bugs or feature requests on the bugtracker website
240             L<https://github.com/shlomif/perl-Test-Data-Split/issues>
241              
242             When submitting a bug or request, please include a test-file or a
243             patch to an existing test-file that illustrates the bug or desired
244             feature.
245              
246             =head1 COPYRIGHT AND LICENSE
247              
248             This software is Copyright (c) 2014 by Shlomi Fish.
249              
250             This is free software, licensed under:
251              
252             The MIT (X11) License
253              
254             =cut