Isolating legacy code from external dependencies can be awkward. Code naturally resists being isolated if it isn't designed to be isolatable. In C and C++ the transitive nature of #includes is the most obvious and direct reflection of the high-coupling such code exhibits. There is a technique that you can use to isolate a source file by cutting all it's #includes.
It relies on a little known third way of writing a #include.
From the C standard:
6.10.2 Source file inclusion
...
A preprocessing directive of the form:
#include pp-tokens
(that does not match one of the two previous forms) is permitted.
The preprocessing tokens after include
in the directive are processed just as in normal text. ... The directive resulting after all replacements shall match one of the two previous forms.
An example. Suppose you have a legacy source file that you want to write some unit tests for. For example:
/* legacy.c */ #include "wibble.h" #include <stdio.h> int legacy(void) { ... info = external_dependency(stdout); ... }
First refactor into this (nothing.h
is a file containing nothing):
/* legacy.c */ #if defined(UNIT_TEST) # define LOCAL(header) "nothing.h" # define SYSTEM(header) "nothing.h" #else # define LOCAL(header) #header # define SYSTEM(header) <header>
Makasih