Make your C++ singletons testable in 2 lines!
Header-only C++11 library for testable singletons. Add one macro, get instant testability.
#include<PM/SingletonBase.h>classMySingleton : publicPM::SingletonBase<MySingleton> {
PM_SINGLETON(MySingleton) // That's it!public:voidsetValue(int v) { value_ = v; }
intgetValue() const { return value_; }
private:int value_ = 0;
};#include<PM/ScopedSingletonState.h>voidtestMySingleton() {
PM::ScopedSingletonState<MySingleton> scoped; // Fresh instanceMySingleton::instance().setValue(42);
assert(MySingleton::instance().getValue() == 42);
}No mocking. No setup. Just test directly.
| Class | Description | Reference |
|---|---|---|
SingletonBase<T> | Testable singleton base | API |
ScopedSingletonState<T> | Test instance replacement | API |
Advanced topics: Performance, Thread Safety, etc.
- Copy the
include/folder to your project - Add
include/to your compiler's include paths - Include headers:
#include <PM/SingletonBase.h>
Add this to your CMakeLists.txt:
add_subdirectory(path/to/SingletonBase)
target_link_libraries(your_targetPM::SingletonBase)This automatically adds the include directory and excludes tests from your build.
MIT License - Do whatever you want with it! No restrictions, no strings attached.