diff options
author | Andrew <forkk@forkk.net> | 2013-12-09 12:04:05 -0600 |
---|---|---|
committer | Andrew <forkk@forkk.net> | 2013-12-09 12:04:05 -0600 |
commit | 7f52bed9e3f559adcbcf3f3c1c7ac2251964db8c (patch) | |
tree | 52097b57dc7d18ec4e35ef3a8e554af2d91545e8 /mmc_updater/src/tests/TestUtils.h | |
parent | 9410dd042ea62224fa3e0eb3b525abbdf0c316ee (diff) | |
parent | 220e07aef4a34dca9f31ae0c5bb994e5c594125d (diff) | |
download | MultiMC-7f52bed9e3f559adcbcf3f3c1c7ac2251964db8c.tar MultiMC-7f52bed9e3f559adcbcf3f3c1c7ac2251964db8c.tar.gz MultiMC-7f52bed9e3f559adcbcf3f3c1c7ac2251964db8c.tar.lz MultiMC-7f52bed9e3f559adcbcf3f3c1c7ac2251964db8c.tar.xz MultiMC-7f52bed9e3f559adcbcf3f3c1c7ac2251964db8c.zip |
Merge branch 'feature_updater' into develop
Diffstat (limited to 'mmc_updater/src/tests/TestUtils.h')
-rw-r--r-- | mmc_updater/src/tests/TestUtils.h | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/mmc_updater/src/tests/TestUtils.h b/mmc_updater/src/tests/TestUtils.h new file mode 100644 index 00000000..68d97da5 --- /dev/null +++ b/mmc_updater/src/tests/TestUtils.h @@ -0,0 +1,108 @@ +#pragma once + +#include <iostream> +#include <functional> +#include <string> +#include <vector> + +template <class T> +class TestList +{ + public: + void addTest(void (T::*test)()) + { + m_tests.push_back(std::mem_fun(test)); + } + + int size() const + { + return m_tests.size(); + } + + void runTest(T* testInstance, int i) + { + m_tests.at(i)(testInstance); + } + + private: + std::vector<std::mem_fun_t<void,T> > m_tests; +}; + +class TestUtils +{ + public: + template <class X, class Y> + static void compare(const X& x, const Y& y, const char* xString, const char* yString) + { + if (x != y) + { + throw "Actual and expected values differ. " + "Actual: " + toString(x,xString) + + " Expected: " + toString(y,yString); + } + } + + template <typename T> + static std::string toString(T value, const char* context) + { + return "Unprintable: " + std::string(context); + } + + template <class T> + static int runTest(class TestList<T>& tests) throw () + { + std::string errorText; + try + { + T testInstance; + for (int i=0; i < tests.size(); i++) + { + tests.runTest(&testInstance,i); + } + } + catch (const std::exception& ex) + { + errorText = ex.what(); + } + catch (const std::string& error) + { + errorText = error; + } + catch (...) + { + errorText = "Unknown exception"; + } + + if (errorText.empty()) + { + std::cout << "Test passed" << std::endl; + return 0; + } + else + { + std::cout << "Test failed: " << errorText << std::endl; + return 1; + } + } +}; + +template <> +inline std::string TestUtils::toString(const std::string& value, const char*) +{ + return value; +} +template <> +inline std::string TestUtils::toString(std::string value, const char*) +{ + return value; +} +template <> +inline std::string TestUtils::toString(const char* value, const char*) +{ + return value; +} + +#define TEST_COMPARE(x,y) \ + TestUtils::compare(x,y,#x,#y); + + |