Extended/Overview.pm untested

filter: Statementstmt 0.0% 0 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 1 Podpod n/a totaltotal 0.0% 0 / 4 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
CRAP 2.0
1 package IO::Capture::Extended::Overview;
2 use strict;
3  
4 ########## DOCUMENTATION ##########
5  
6 =head1 NAME
7  
8 IO::Capture::Extended - Extend functionality of IO::Capture
9  
10 =head1 SYNOPSIS
11  
12 The programmer interface consists of two classes:
13  
14 =head2 IO::Capture::Stdout::Extended
15  
16 use IO::Capture::Stdout::Extended;
17  
18 $capture = IO::Capture::Stdout::Extended->new();
19 $capture->start();
20 # some code that prints to STDOUT
21 $capture->stop();
22  
23 # scalar context: return number of print statements with 'fox'
24 $matches = $capture->grep_print_statements('fox');
25  
26 # list context: return list of print statements with 'fox'
27 @matches = $capture->grep_print_statements('fox');
28  
29 # return number of print statements
30 $matches = $capture->statements;
31  
32 # scalar context: return number of pattern matches
33 $regex = qr/some regular expression/;
34 $matches = $capture->matches($regex);
35  
36 # list context: return list of pattern matches
37 @matches = $capture->matches($regex);
38  
39 # return reference to array holding list of pattern matches
40 $matchesref = $capture->matches_ref($regex);
41  
42 # scalar context: return number of 'screen' lines printed
43 $screen_lines = $capture->all_screen_lines();
44  
45 # list context: return list of 'screen' lines printed
46 @all_screen_lines = $capture->all_screen_lines();
47  
48 =head2 IO::Capture::Stderr::Extended
49  
50 $capture = IO::Capture::Stderr::Extended->new();
51 $capture->start();
52 # some code that prints to STDERR
53 $capture->stop();
54  
55 ... and then use all the methods defined above for
56 F<IO::Capture::Stdout::Extended>.
57  
58 =head1 DESCRIPTION
59  
60 IO::Capture::Extended is a distribution consisting of two classes, each
61 of which is a collection of subroutines which are useful in extending
62 the functionality of CPAN modules IO::Capture::Stdout and
63 IO::Capture::Stderr, particularly when used in a testing context
64 such as that provided by Test::Simple, Test::More or other modules
65 built on Test::Builder.
66  
67 =head1 USAGE
68  
69 =head2 Requirements
70  
71 IO::Capture distribution, available from CPAN
72 L<http://search.cpan.org/~reynolds/IO-Capture-0.05/>. Use version 0.05
73 or later to take advantage of important bug fixes. The IO::Capture
74 distribution includes base class IO::Capture, IO::Capture::Stdout,
75 IO::Capture::Stderr and other packages. It also includes useful
76 documentation in IO::Capture::Overview.
77  
78 =head2 General Comments
79  
80 The IO::Capture::Stdout::Extended and IO::Capture::Stdout::Extended
81 methods are designed to provide
82 return values which work nicely as arguments to Test::More functions
83 such as C<ok()>, C<is()> and C<like()>. The examples below illustrate
84 that objective. Suggestions are welcome for additional methods
85 which would fulfill that objective.
86  
87 =head2 Individual Methods
88  
89 B<Note:> Since IO::Capture::Extended is structured so as to make
90 exactly the same methods available for IO::Capture::Stdout::Extended
91 I<and> IO::Capture::Stderr::Extended, whenver there appears below a
92 reference to, I<e.g.,>
93 C<IO::Capture::Stdout::Extended::grep_print_statements()>, you should
94 assume that the remarks apply equally to
95 C<IO::Capture::Stderr::Extended::grep_print_statements()>. Wherever
96 reference is made to STDOUT, the remarks apply to STDERR as well.
97  
98 =head3 C<grep_print_statements()>
99  
100 =over 4
101  
102 =item * Scalar Context
103  
104 I<Problem:> You wish to test a function that prints to STDOUT.
105 You can
106 predict the I<number> of C<print> statements that match a pattern and wish
107 to test that prediction. (The example below is adapted from
108 IO::Capture::Overview.)
109  
110 sub print_fox {
111 print "The quick brown fox jumped over ...";
112 print "garden wall";
113 print "The quick red fox jumped over ...";
114 print "garden wall";
115 }
116  
117 $capture->start;
118 print_fox();
119 $capture->stop;
120 $matches = $capture->grep_print_statements('fox');
121 is($capture->grep_print_statements('fox'), 2,
122 "correct no. of print statements grepped");
123  
124 I<Solution:> Precede the function call with a call to the
125 IO::Capture::Stdout::Extended
126 C<start()> method and follow it with a call to the C<stop()> method.
127 Call C<grep_print_statements>. Use its
128 return value as one of two arguments to C<Test::More::is()>. Use your
129 prediction as the other argument. Add a useful comment to C<is()>.
130  
131 I<Potential Pitfall:> The number of print statements captured between
132 C<IO::Capture::Stdout::Extended::start()> and C<stop()> is I<not>
133 necessarily the number of lines that would
134 appear to be printed to standard output by a given block of code. If
135 your subroutine or other code block prints partial lines -- I<i.e.,>
136 lines lacking C<\n> newline characters -- the number of print statements
137 will be greater than the number of ''screen lines.'' This is
138 illustrated by the following:
139  
140 sub print_fox_long {
141 print "The quick brown fox jumped over ...";
142 print "a less adept fox\n";
143 print "The quick red fox jumped over ...";
144 print "the garden wall\n";
145 }
146  
147 $capture->start;
148 print_fox_long();
149 $capture->stop;
150 $matches = $capture->grep_print_statements("fox");
151 is($capture->grep_print_statements("fox"), 3,
152 "correct no. of print statements grepped");
153  
154 The number of C<print> statements matching C<fox> is three -- even though
155 the number of lines on the screen which appear on STDOUT containing
156 C<fox> is only two.
157  
158 =item * List Context
159  
160 I<Problem:> As above, you wish to test a function that prints to STDOUT.
161 This time, you can predict the I<content> of C<print> statements that
162 match a pattern and wish to test that prediction.
163  
164 %matches = map { $_, 1 } $capture->grep_print_statements('fox');
165 is(keys %matches, 2, "correct no. of print statements grepped");
166 ok($matches{'The quick brown fox jumped over ...'},
167 'print statement correctly grepped');
168 ok($matches{'The quick red fox jumped over ...'},
169 'print statement correctly grepped');
170  
171 I<Solution:> As above, call C<grep_print_statements>, but map its
172 output to a 'seen-hash'. You can then use the number of keys in that
173 seen-hash as an argument to C<Test::More::is()>. You can use C<ok()> to
174 test whether the keys of that hash are as predicted.
175  
176 =back
177  
178 =head3 C<statements()>
179  
180 I<Problem:> You've written a function which prints to STDOUT. You can make a
181 prediction as to the number of screen lines which should be printed.
182 You want to test that prediction with C<Test::More::is()>.
183  
184 sub print_greek {
185 local $_;
186 print "$_\n" for (qw| alpha beta gamma delta |);
187 }
188  
189 $capture->start();
190 print_greek();
191 $capture->stop();
192 is($capture->statements, 4,
193 "number of print statements is correct");
194  
195 I<Solution:> Precede the function call with a call to the IO::Capture::Stdout
196 C<start()> method and follow it with a call to the C<stop()> method.
197 Call C<IO::Capture::Stdout::Extended::statements()> and use its return
198 value as the first argument to C<is()>. Use your prediction as
199 the second argument to C<is()>. Be sure to write a useful comment for your
200 test.
201  
202 I<Potential Pitfall:> The number of print statements returned by
203 C<statements> is I<not> necessarily the number of lines that would
204 appear to be printed to standard output by a given block of code. If
205 your subroutine or other code block prints partial lines -- I<i.e.,>
206 lines lacking C<\n> newline characters -- the number of print statements
207 will be greater than the number of ''screen lines.'' This is
208 illustrated by the following:
209  
210 sub print_greek_long {
211 local $_;
212 for (qw| alpha beta gamma delta |) {
213 print $_;
214 print "\n";
215 }
216 }
217  
218 $capture->start();
219 print_greek_long();
220 $capture->stop();
221 is($capture->statements, 8,
222 "number of print statements is correct");
223  
224 This pitfall can be avoided by using C<all_screen_lines()> below.
225  
226 =head3 C<all_screen_lines()>
227  
228 =over 4
229  
230 =item * Scalar Context
231  
232 Returns the number of lines which would normally be counted by eye on
233 STDOUT. This number is not necessarily equal to the number of C<print()>
234 statements found in the captured output. This method avoids the 'pitfall'
235 found when using C<statements()> above.
236  
237 $capture->start();
238 print_greek_long();
239 $capture->stop();
240 $screen_lines = $capture->all_screen_lines;
241 is($screen_lines, 4, "correct no. of lines printed to screen");
242  
243 =item * List Context
244  
245 Returns an array holding lines as normally viewed on STDOUT. The size
246 of this array is not necessarily equal to the number of C<print()>
247 statements found in the captured output. This method avoids the
248 'pitfall' found when using C<statements()> above.
249  
250 $capture->start();
251 print_greek_long();
252 $capture->stop();
253 @all_screen_lines = $capture->all_screen_lines;
254 is($all_screen_lines[0],
255 "alpha",
256 "line correctly printed to screen");
257 is($all_screen_lines[1],
258 "beta",
259 "line correctly printed to screen");
260  
261 Any newline (C<\n>) appearing at the end of a screen line is I<not> included
262 in the list of lines returned by this method, I<i.e.,> the lines are
263 chomped.
264  
265 =back
266  
267 =head3 C<matches()>
268  
269 =over 4
270  
271 =item * Scalar Context
272  
273 I<Problem:> You've written a function which, much like the ''mail merge''
274 function in word processing programs, extracts data from some data source,
275 merges the data with text in a standard form, and prints the result to
276 STDOUT. You make a prediction as to the number of forms which are
277 printed to STDOUT and wish to confirm that prediction.
278  
279 my @week = (
280 [ qw| Monday Lundi Lunes | ],
281 [ qw| Tuesday Mardi Martes | ],
282 [ qw| Wednesday Mercredi Miercoles | ],
283 [ qw| Thursday Jeudi Jueves | ],
284 [ qw| Friday Vendredi Viernes | ],
285 [ qw| Saturday Samedi Sabado | ],
286 [ qw| Sunday Dimanche Domingo | ],
287 );
288  
289 sub print_week {
290 my $weekref = shift;
291 my @week = @{$weekref};
292 for (my $day=0; $day<=$#week; $day++) {
293 print "English: $week[$day][0]\n";
294 print "French: $week[$day][1]\n";
295 print "Spanish: $week[$day][2]\n";
296 print "\n";
297 }
298 }
299  
300 $capture->start();
301 print_week(\@week);
302 $capture->stop();
303 $regex = qr/English:.*?French:.*?Spanish:/s;
304  
305 is($capture->matches($regex), 7,
306 "correct number of forms printed to screen");
307  
308 I<Solution:> Precede the function call with a call to the IO::Capture::Stdout
309 C<start()> method and follow it with a call to the C<stop()> method.
310 Write a Perl regular expression and assign it to a variable using
311 the C<qr//> notation. (Remember to use the C</s> modifier
312 if the text you are testing crosses screen lines.) Pass the
313 regex variable to C<IO::Capture::Stdout::Extended::matches()>
314 and use the return value of that method call as one argument to
315 C<Test::More::is()>. Use your prediction as the second argument to C<is()>.
316 Be sure to write a useful comment for your test.
317  
318 =item * List Context
319  
320 I<Problem:> As above, you've written a function which, much like the
321 ''mail merge'' function in word processing programs, extracts data
322 from some data source, merges the data with text in a standard form,
323 and prints the result to STDOUT. This time, however, you wish to do
324 a quick test on the results by examining a sample form.
325  
326 $capture->start();
327 print_week(\@week); # as defined above
328 $capture->stop();
329  
330 @matches = $capture->matches($regex);
331 $predicted = "English: Monday\nFrench: Lundi\nSpanish:";
332 is($matches[0], $predicted, "first form matches test portion");
333  
334 I<Solution:> Same as above, but capture the output of C<matches()> in
335 a list or array. Write a string which predicts typical contents of one
336 instance of your merged form. Use the contents of one form (one element
337 in the list output) and the prediction string as arguments to C<is()>.
338 Be sure to write a useful comment for your test.
339  
340 I<Problem:> As above, but now you wish to make sure that a form was
341 generated for each required field in the data source.
342  
343 $regex = qr/French:\s+(.*?)\n/s;
344 @predicted = qw| Lundi Mardi Mercredi Jeudi
345 Vendredi Samedi Dimanche |;
346 ok(eq_array( [ $capture->matches($regex) ], \@predicted ),
347 "all predicted matches found");
348  
349 I<Solution:> Similar to above, but this time you predict which data
350 points are going to be present in the output. Store that prediction in
351 a list or array. Take references to (a) the array holding that
352 prediction; and (b) the result of C<$capture->matches($regex)>. Pass
353 those two references to Test::More's utility function C<eq_array>, which
354 will return a true value if the underlying arrays are identical
355 element-by-element. Pass that value in turn to <ok()>. As always,
356 be sure to write a useful comment for your test.
357  
358 =back
359  
360 =head3 C<matches_ref()>
361  
362 I<Problem:> Same as the first ''List Context'' example above, but now
363 you would prefer to work with a method that returns an array reference
364 rather than all the elements in a list.
365  
366 $matchesref = $capture->matches_ref($regex);
367 is(${$matchesref}[0], $predicted, "first form matches test portion");
368  
369 I<Solution:> Call C<IO::Capture::Stdout::Extended::matches_ref()>
370 instead of C<matches>. You will have to rephrase your test in terms of
371 an element of a dereferenced array.
372  
373 =head1 BUGS
374  
375 As Paul Johnson says, ''Did I mention that this is alpha code?''.
376  
377 =head1 SUPPORT
378  
379 Contact the author or post to the perl-qa mailing list.
380  
381 =head1 ACKNOWLEDGEMENTS
382  
383 Thanks go first and foremost to the two authors of the IO::Capture
384 distribution, Mark Reynolds and Jon Morgan. Mark Reynolds was
385 responsive to this module's author's suggestions for bug fixes and
386 additional tests. The documentation found in
387 IO::Capture::Overview was particularly helpful in showing me how to
388 extend IO::Capture's functionality. This distribution is maintained
389 independently but will be updated as needed if and when IO::Capture
390 is revised.
391  
392 The methods in IO::Capture::Extended are
393 offered to the Perl community in gratitude for being turned on to
394 IO::Capture by David Cantrell on the perl.qa mailing list in
395 February 2005 (L<http://www.nntp.perl.org/group/perl.qa/3567>).
396  
397 Other contributors to that discussion thread whose suggestions are
398 reflected in this module were David H. Adler, David Golden, Michael G.
399 Schwern and Tels. Fergal Daly also made suggestions in separate
400 communications.
401  
402 The structure for this module was created with my own hacked-up version
403 of R. Geoffrey Avery's F<modulemaker> utility, based on his CPAN module
404 ExtUtils::ModuleMaker.
405  
406 =head1 AUTHOR
407  
408 James E Keenan. CPAN ID: JKEENAN. jkeenan [at] cpan [dot] org.
409  
410 =head1 COPYRIGHT
411  
412 Copyright 2005-15 James E Keenan.
413  
414 This program is free software; you can redistribute
415 it and/or modify it under the same terms as Perl itself.
416  
417 The full text of the license can be found in the
418 LICENSE file included with this module.
419  
420 =head1 SEE ALSO
421  
422 perl(1). IO::Capture; IO::Capture::Stdout; IO::Capture::Overview.
423 Test::Simple; Test::More.
424  
425 =cut
426  
427 1;
428