Using subroutines could be something as simple as
sub test_encodings {
my ($fh, $fn) = tempfile();
$fh->print($file_string);
$fh->close();
is_deeply(
[ Fancy::Open::fancy_open($fn) ],
[ map encode('utf8', $_), @wanted_array ],
"testing an array opened with no encoding"
);
my @encodings = qw(UTF-8 ascii cp1252 iso-8859-1);
for my $encoding (@encodings) {
is_deeply(
[ Fancy::Open::fancy_open($fn, { 'encoding' => $encoding }) ],
[ map encode($encoding, $_), @wanted_array ],
"testing an array opened with $encoding encoding"
);
}
}
sub test_with_file_that_ends_with_a_newline {
my ($fh, $fn) = tempfile();
$fh->print("$file_string\n");
$fh->close();
is_deeply(
[ Fancy::Open::fancy_open($fn) ],
[ @wanted_array ],
"testing a plain array with file that ends with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($fn, { 'before' => 'solid ' }) ],
[ @solid_array ],
"testing an array with before option with file that ends with a
+newline"
);
is_deeply(
[ Fancy::Open::fancy_open($fn, { 'after' => ' bead' }) ],
[ @bead_array ],
"testing an array with after option with file that ends with a n
+ewline"
);
is_deeply(
[ Fancy::Open::fancy_open($fn, { 'before' => 'solid ', 'after' =
+> ' bead' }) ],
[ @solid_bead_array ],
"testing an array with before and after options with file that e
+nds with a newline"
);
}
As bonuses, the file name/handle variable names are uniform, and the section comments are made redundant by the sub names.
On second thought, "slurp" (in Perl land) means reading a file into a scalar, whereas you're reading into an array, so "fancy_read_lines" is still more accurate than the current name.
I didn't realise GitHub could automatically render .pod files - that is cool.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.