Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect data to stdin within a single executable?

Tags:

c++

I am using cxxtest as the test framework for my C++ classes, and would like to figure out a way to simulate sending data to classes which would normally expect to receive it from standard input. I have several different files which I would like to send to the classes during different tests, so redirection from the command line to the test suite executable is not an option.

Basically, what I would really like to do is find a way to redefine or redirect the 'stdin' handle to some other value that I create inside of my program, and then use fwrite() from these tests so that the corresponding fread() inside of the class pulls the data from within the program, not from the actual standard I/O handles associated with the executable.

Is this even possible? Bonus points for a platform-independent solution, but at a very minimum, I need this to work with Visual Studio 9 under Windows.

like image 857
Nik Reiman Avatar asked Dec 01 '22 13:12

Nik Reiman


1 Answers

The appropriate method is to rewrite your classes so that they are testable. They should accept as a parameter the handle, stream or file from which they are supposed to read data - in your test framework, you can then mock in the stream or supply the path to the file containing the test data.

like image 126
1800 INFORMATION Avatar answered Dec 05 '22 05:12

1800 INFORMATION