#exclude - a testing idea for C/C++



Suppose you're working on a legacy C/C++ codebase and you want to introduce some characterization tests. One seam you might consider exploiting is #includes. It's quite easy to exclude includes. For example:
# excluder.rb
include = Regexp.new('(\s*)#(\s*)include(.*)')
STDIN.readlines.each do |line|
 if include.match(line)
   line = "#if 0\n" + line + "#endif\n"
 end
 STDOUT.write line
end
This small Ruby program reads in a source file and writes out the same source file, except the #includes are commented out. Each line like this:
#include <stdio.h>
is replaced like this:
#if 0
#include <stdio.h>
#endif

This enables you to create an "isolated" version of a source file. One where all the dependencies arising from the #includes, to any depth, are slashed in one swift cut. One where your tests clearly and visibly have to recreate all those dependencies in a custom mock environment.

This is just an idea. I might not be a good idea. I only thought of it last week. Caveat emptor.