I am dealing with some old, sloppy C++ code in which there is a structure with a lot of data members and functions. I want to test a class that I have implemented which uses the struct, but this dependency is going to make the unit tests extremely ugly. Inspired by Michael Feather's discussion of preprocessing seams in Working Effectively with Legacy Code, I am getting ready to solve this problem by faking the ugly structure with the preprocessor (not with inheritance) polymorphism as demonstrated in the following notional code.
test.cpp
#include <iostream>
#include "NotAsUgly.hpp"
#include "DependsOnUgly.hpp"
int main() {
std::cout << DependsOnUgly().getDescription() << std::endl;
}
DependsOnUgly.hpp
#ifndef _DEPENDS_ON_UGLY_HPP_
#define _DEPENDS_ON_UGLY_HPP_
#include <string>
#include "Ugly.hpp"
class DependsOnUgly {
public:
std::string getDescription() {
return "Depends on " + Ugly().getName();
}
};
#endif
Ugly.hpp
#ifndef _UGLY_HPP_
#define _UGLY_HPP_
struct Ugly {
double a, b, ..., z;
void extraneousFunction { ... }
std::string getName() { return "Ugly"; }
};
#endif
NotAsUgly.hpp
#ifndef _UGLY_HPP_ // Same name as in Ugly.hpp---deliberately!
#define _UGLY_HPP_
struct Ugly { // Once again, duplicate name is deliberate
std::string getName() { return "not as ugly"; } // All that DependsOnUgly depends on
};
#endif
Is there a special name for the type of fake object I'm using here? I'm inclined to call it a stub, but it is rather different than what I would normally think of as a stub. I would expect something called a stub to implement the interface of the class which is to be faked through inheritance. Here the implementation is only partial, and happens through the preprocessor.
Is there already a known name for this kind of fake/testing object? I want to name the header file that contains it (NotAsUgly.hpp
in my example) to properly signify to other developers what it is for.