| 30 | // Uses CRTP |
| 31 | template<typename TSubclass> |
| 32 | class TestClass |
| 33 | { |
| 34 | public: |
| 35 | static void notifyTestFailed(int line, const char* expr) |
| 36 | { |
| 37 | std::printf(" FAILED!\n ******* Assertion failed (line %d): %s\n\n", line, expr); |
| 38 | } |
| 39 | |
| 40 | bool validateTestName(std::string const& which) const |
| 41 | { |
| 42 | return testMap.find(which) != testMap.end(); |
| 43 | } |
| 44 | |
| 45 | void getAllTestNames(std::vector<std::string>& names) const |
| 46 | { |
| 47 | for (auto it = testMap.cbegin(); it != testMap.cend(); ++it) { |
| 48 | names.push_back(it->first); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | bool run(unsigned int iterations = 1) |
| 53 | { |
| 54 | bool success = true; |
| 55 | for (auto it = testVec.cbegin(); it != testVec.cend(); ++it) { |
| 56 | if (!execTest(*it, iterations)) { |
| 57 | success = false; |
| 58 | } |
| 59 | } |
| 60 | return success; |
| 61 | } |
| 62 | |
| 63 | bool run(std::vector<std::string> const& which, unsigned int iterations = 1) |
| 64 | { |
| 65 | bool success = true; |
| 66 | for (auto it = which.begin(); it != which.end(); ++it) { |
| 67 | if (!execTest(*testMap.find(*it), iterations)) { |
| 68 | success = false; |
| 69 | } |
| 70 | } |
| 71 | return success; |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | typedef TSubclass subclass_t; |
| 76 | |
| 77 | void registerTest(const char* name, bool (subclass_t::* method)()) |
| 78 | { |
| 79 | testVec.push_back(std::make_pair(std::string(name), method)); |
| 80 | testMap[std::string(name)] = method; |
| 81 | } |
| 82 | |
| 83 | bool execTest(std::pair<std::string, bool (subclass_t::*)()> const& testRef, unsigned int iterations) |
| 84 | { |
| 85 | std::printf("%s::%s... \n", demangle_type_name(typeid(subclass_t).name()).c_str(), testRef.first.c_str()); |
| 86 | |
| 87 | bool result = true; |
| 88 | for (unsigned int i = 0; i != iterations; ++i) { |
| 89 | if (!(static_cast<subclass_t*>(this)->*testRef.second)()) { |
nothing calls this directly
no outgoing calls
no test coverage detected