Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Testing an input reader?

Is there a way to automatically test using the standard Test etc. modules whether a Perl program is reading input from e.g. STDIN properly? E.g. testing a program that reads two integers from STDIN and prints their sum.

like image 764
draeklae Avatar asked Feb 13 '26 09:02

draeklae


1 Answers

It's not 100% clear what you mean, I'll asnswer assuming you want to write a test script that tests your main program, which as part of the test needs to have test input data passed via STDIN.

You can easily do that if your program outputs what it reads. You don't need a special test module - simply:

  1. Call your program your're testing via a system call

    • redirect both STDIN and STDOUT of tested program to your test script, using

      • IPC::Open2 module to open both sides via pipes to filehandles,

      • ... OR, build your command to redirect to/from files and read/write the files in the test script

  2. Check STDOUT from tested program that you collected in the last step to make sure correct values are printed.

like image 196
DVK Avatar answered Feb 15 '26 11:02

DVK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!