views:

212

answers:

4

Hi,

I am trying to create a unit test framework using CPPUnit for a large code base. I need to be able to test individual modules, all of which are part of a module tree that begins with a specific root module.

Due to a non-technical reason, I cannot touch the production file (my original approach involved adding an ifdef to the root module). So I thought of another approach, which is to have create copies of the root module headers as well as copies of headers belonging to modules in the intermediate inheritance hierachy. Because of the number of number of modules invovled as well as the size of each module's source. I'm looking for a way to automatically do that merging for me.

So for foo.h, and foo.cpp, I'm looking for a some kind of a tool that'll output fooTest.h, where fooTest.h contains the declaration AND definition of everything that is in foo.cpp/foo.h

Thanks in advance!

EDIT: Thanks for the answers, one thing I forgot to mention is that, the contents of fooTest.h is not supposed to be the merged result of foo.cpp and foo.h . I need to make minor changes to the root fooTest.h in order to make it a suitable mock-module for testing. Thus, simply using includes won't work. I'll look into concatenating the files and see if that solves my problem.

+1  A: 

This isn't a C++ question - you are asking how to manipulate files in some sort of script. The immediate answer that comes to mind is:

cat foo.h foo.cpp > fooTest.h
anon
Same reply to both "cat" answers: What happens when the cpp file tries to #include the original header file, which might not be there anymore or, if it successfully includes it, may have code that can't be used twice in a row? And this doesn't handle the "intermediate headers" as mentioned in the question.
Dennis
@Dennis Well, I don't see why it wouldn't be there any more - the OP's question is unclear, to say the least. And its include guards would still work.
anon
That would definitely work, I didn't exactly do this way though, I just duplicated foo.h and foo.cpp and included the .h before the .cpp in my test runner code.
wk1989
+5  A: 
gcc -E

runs just the preprocessor:

  -E  Stop after the preprocessing stage; do not run the compiler proper.
      The output is in the form of preprocessed source code, which is
      sent to the standard output.

This will have the effect of inlining all #include directives. It will, however, also grab standard libraries - but these shouldn't be harmful to include multiple times.

Steven Schlansker
It would be better to give the complete command line including the file name.
Vulcan Eager
If he can't figure out how to run gcc properly, he has no business trying to write a unit test suite.
Steven Schlansker
A: 

Under *nix you can just use cat.

cat foo.h foo.cpp > fooTest.h
Kyle Lutz
+1  A: 

Write a simple tool that processes only #include "" directives. Note, it should not process "#include <>" directives, and don't touch any other preprocessor directive you encounter.

You'll want to support -I arguments in a gcc like way (or do something equivalent for your compiler if running in a different environment). Should be a afternoon job, and would make a nice open source project (leave a pointer if you do it).

This will result in dragging in the whole tree of "non-standard" headers used by each source file you work on, but that should be harmless.

dmckee