How to test C++ classes when header files contain explicit paths?

260 Views Asked by At

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:

  1. create another duplicate file structure that matches the include paths, and put my mock DatabaseX there in this duplicate file structure?
  2. Somehow rewite each cpp file before the compiler accesses it by some indirection magic or other?
  3. Add macros to eat up the paths?
  4. Write a python/perl/bash script to temporarily remove the include paths prior to compiling my tests?
  5. 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?
  6. Accept defeat; don't write any tests, and bury my head in the sand.
  7. 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

0

There are 0 best solutions below