The test I needed help with in Can Test::MockObject mock a file? is finished. I would like to know how I did writing it.
I got some advice on looping I did not quite get a handle on, but this is a short test for a little module.
Here are links to the Fancy::Open module and Fancy::Open pod (unfinished).
#!perl
use strict;
use warnings;
use v5.10.0;
use Test::More tests => "14";
use File::Temp qw(tempfile);
use Encode qw(encode);
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_array = qw(red orange yellow spring green teal cyan azu
+re blue violet magenta pink white black gray);
my @solid_array = map( "solid $_", @wanted_array );
my @bead_array = map( "$_ bead", @wanted_array );
my @solid_bead_array = map( "solid $_ bead", @wanted_array );
my $file_string = join( "\n", @wanted_array );
# Testing with file that ends with a newline
my ($n_fh, $n_file) = tempfile();
$n_fh->print("$file_string\n");
$n_fh->close();
is_deeply(
[ Fancy::Open::fancy_open($n_file) ],
[ @wanted_array ],
"testing a plain array with file that ends with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($n_file, { 'before' => 'solid ' }) ],
[ @solid_array ],
"testing an array with before option with file that ends with a newl
+ine"
);
is_deeply(
[ Fancy::Open::fancy_open($n_file, { 'after' => ' bead' }) ],
[ @bead_array ],
"testing an array with after option with file that ends with a newli
+ne"
);
is_deeply(
[ Fancy::Open::fancy_open($n_file, { 'before' => 'solid ', 'after' =
+> ' bead' }) ],
[ @solid_bead_array ],
"testing an array with before and after options with file that ends
+with a newline"
);
# Testing with file that does not end with a newline
my ($no_n_fh, $no_n_file) = tempfile();
$no_n_fh->print($file_string);
$no_n_fh->close();
is_deeply(
[ Fancy::Open::fancy_open($no_n_file) ],
[ @wanted_array ],
"testing a plain array with file that does not end with a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($no_n_file, { 'before' => 'solid ' }) ],
[ @solid_array ],
"testing an array with before option with file that does not end wit
+h a newline"
);
is_deeply(
[ Fancy::Open::fancy_open($no_n_file, { 'after' => ' bead' }) ],
[ @bead_array ],
"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' }) ],
[ @solid_bead_array ],
"testing an array with before and after options with file that does
+not end with a newline"
);
# Testing encoding
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"
);
}
done_testing();
# Written with the help of tobyink, davido, chromatic, and perlfan on
+PerlMonks.
# https://www.perlmonks.org/?node_id=11121949
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.
No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|