I have classes that look like this:
Header (.h):
class DatabaseX ; //forward declare
class DeepClass
{
public:
void DeepClass(DatabaseX* db);
void doStuff();
private:
DatabaseX *m_db;
};
Definition (.cpp)
#include "some/path/that/stretches/into/a/rather/deep/structure/DeepClass.h"
#include "another/somewhat/disturbing/long/path/to/somewhere/distant/DatabaseX.h"
void DeepClass::DeepClass(DatabaseX* db):m_db(db){ m_db->open() }
void DeepClass::~DeepClass(){ m_db->close(); delete m_db; }
void DeepClass::doStuff(){ // <complicated stuff here> }
Now I want a test that checks that doStuff()
does the right kind of stuff.
So I write my own mock DatabaseX
.
But I have a problem, my own mock database lives in the test directory, it has no place in production code, and what's worse, DatabaseX
was never written to be inherited and overloaded.
It's a concrete class, and isn't anything like an interface.
So my question is, how do I write a test, with all these hard-coded include paths everywhere? Do I for example:
- create another duplicate file structure that matches the include paths, and put my mock
DatabaseX
there in this duplicate file structure? - Somehow rewite each cpp file before the compiler accesses it by some indirection magic or other?
- Add macros to eat up the paths?
- Write a python/perl/bash script to temporarily remove the include paths prior to compiling my tests?
- Just include everything, accept the dependencies of
DatabaseX
, and just compile the real thing, and all it's dependencies and then replace at link time? - Accept defeat; don't write any tests, and bury my head in the sand.
- OR ... ?
I should say there are well over a million lines of code, so changing the source code isn't an option. Is there a very simple way to overcome this nightmare via a nice simple compiler option or other?
(Perhaps it's not relevant but I'm using Qt's QTest & QtCreator. Maybe there is some magical switch that makes all these gruesome paths go away!).
I am using GCC 4.8.5