Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I went back and gave 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 @wanted array to make the temp test files is not a bad thing.

Note: 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.

#!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 vi +olet 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 newl +ine" ); 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 newli +ne" ); 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 wit +h 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();

I have not figured out how to test the encoding option for this module.

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 Re^2: Can Test::MockObject mock a file? by Lady_Aleena
in thread Can Test::MockObject mock a file? 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 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? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2021-10-21 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (84 votes). Check out past polls.

    Notices?