http://qs321.pair.com?node_id=11121962


in reply to Re: Can Test::MockObject mock a file?
in thread Can Test::MockObject mock a file?

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