note
Lady_Aleena
<p>I went back and gave [doc://File::Temp] a try. I did not like the idea of putting files for these tests on a user's filesystem. Here is what I wrote, with the suggestions to make my test code less repetitive and to test whether a newline or lack thereof at the end of the file would matter. I hope cheating and using the <c>@wanted</c> array to make the temp test files is not a bad thing.</p>
<p><b>Note:</b> I cargo culted the use of File::Temp from the one place in my code where I used it before. I am still a little fuzzy on how it works, but it does appear to work and the tests still passed.</p>
<code>
#!perl
use strict;
use warnings;
use v5.10.0;
use Test::More tests => "9";
use File::Temp qw(tempfile);
BEGIN {
use_ok( 'Fancy::Open', qw(fancy_open) )
or die "Fancy::Open is not available\n";
}
diag( "Testing Fancy::Open $Fancy::Open::VERSION, Perl $], $^X" );
my @wanted = qw(red orange yellow spring green teal cyan azure blue violet magenta pink white black gray);
# Creating file that ends with a newline
my ($n_fh, $n_file) = tempfile();
$n_fh->print(join("\n", @wanted)."\n");
$n_fh->close();
# Testing with file that ends with a newline
is_deeply(
[ Fancy::Open::fancy_open($n_file) ],
[ @wanted ],
"testing a plain array with file that ends with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($n_file, { 'before' => 'solid ' }) ],
[ map "solid $_", @wanted ],
"testing an array with before option with file that ends with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($n_file, { 'after' => ' bead' }) ],
[ map "$_ bead", @wanted ],
"testing an array with after option with file that ends with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($n_file, { 'before' => 'solid ', 'after' => ' bead' }) ],
[ map "solid $_ bead", @wanted ],
"testing an array with before and after options with file that ends with a newline"
);
# Creating file that does not end with a newline
my ($no_n_fh, $no_n_file) = tempfile();
$no_n_fh->print(join("\n", @wanted));
$no_n_fh->close();
# Testing with file that does not end with a newline
is_deeply(
[ Fancy::Open::fancy_open($no_n_file) ],
[ @wanted ],
"testing a plain array with file that does not end with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($no_n_file, { 'before' => 'solid ' }) ],
[ map "solid $_", @wanted ],
"testing an array with before option with file that does not end with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($no_n_file, { 'after' => ' bead' }) ],
[ map "$_ bead", @wanted ],
"testing an array with after option with file that does not end with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($no_n_file, { 'before' => 'solid ', 'after' => ' bead' }) ],
[ map "solid $_ bead", @wanted ],
"testing an array with before and after options with file that does not end with a newline"
);
done_testing();
</code>
<p>I have not figured out how to test the <c>encoding</c> option for this module.</p>
<div class="pmsig"><div class="pmsig-161890">
<p><small>My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.</small></p>
<div><strong>No matter how hysterical I get, my problems are <em>not</em> time sensitive. So, relax, have a [id://1026423|cookie], and a very nice day!</strong></div>
<div><em>Lady Aleena</em></div>
</div></div>
11121949
11121950