Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Testing Input and Output

by jaldhar (Vicar)
on Sep 03, 2002 at 19:11 UTC ( [id://194865]=note: print w/replies, xml ) Need Help??


in reply to Testing Input and Output

Thanks to all who replied. jcleland (with a slight refinement from grep) suggested the typical way for redirecting a stream. However that won't really work in this case as my brainfck instructions are transformed into hardcoded perl statements. greenFox's suggestion has the same problem.

Joost and dog andPony suggested two other modules. Of those IO::Scalar does the reverse of what I want i.e. it treats a scalar as a filehandle. Tie::Handle::Scalar on the other hand was perfect. The only stumbling block was that it is not part of the Perl core modules and asking people to install it just to run tests seems a bit extreme. Luckily the code is short enough that I could just include it verbatim in my test script. Normally this would be a maintainence problem but i'm not even using all the features of the current module so it won't be a problem if my local copy goes out of sync with the upstream version.

Here is the actual code from my test script. See the Inline::brainfck thread to understand the interpolated Brainfck code.

$a = "\t"; tie *STDIN, 'Tie::Handle::Scalar', \$a; my $b = , ; ok ( $b == 9, ' Does , work?'); untie *STDIN; $a = ''; tie *STDOUT, 'Tie::Handle::Scalar', \$a; .. ok ( $a eq "\t\t", ' Does . work?'); untie *STDOUT; $a = ''; tie *STDERR, 'Tie::Handle::Scalar', \$a; # ok ( $a eq "\$p = 1 \$m[\$p]= 9\n", ' Does # work?'); untie *STDERR;

--
જલધર

Replies are listed 'Best First'.
Re^2: Testing Input and Output
by adrianh (Chancellor) on Sep 04, 2002 at 13:32 UTC

    In the spirit of TMTOWTDI you could also use IO::File to create a temporary file, redirect STDIN to the temporary file, do your stuff, seek back to the start and slurp up the output into a scalar for testing as normal.

    The skip2.t test in Test::Class uses this method.

    One of the advantages is that IO::File is core so you don't have to muck around with other modules.

    I did write a Test::Output module (download Test-Output-0.01.tar.gz) that allowed you to do things like:

    output_is { hello() } "hello world\n", STDOUT, "hello world"; output_isnt { hello() } "goodbye", STDOUT, "not goodbye"; output_unlike { hello() } qr/bye/, STDOUT, "didn't print bye +"; output_like { hello() } qr/hello/, STDOUT, "printed hello"; like(Test::Output->last, qr/world/, "... and world");

    ... but I'm not entirely happy with the API so have not distributed it yet... feedback welcome :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://194865]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found