Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to RFC: How did I do writing my first test? by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found