Guest User

Untitled

a guest
May 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1.  
  2.  
  3. #ifndef CUULIGHT
  4. #define CUULIGHT
  5.  
  6. #include <exception>
  7. #include <list>
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <sstream>
  11.  
  12. class cuu_exception : public std::exception
  13. {
  14. public:
  15. cuu_exception(const std::string& message)
  16. : message(message) {}
  17.  
  18. virtual ~cuu_exception() throw () {}
  19.  
  20. virtual const char *what() throw () {
  21. return message.c_str();
  22. }
  23. private:
  24. std::string message;
  25. };
  26.  
  27. inline std::string
  28. cuu_condition_text(bool value)
  29. {
  30. return value ? "true" : "false";
  31. }
  32.  
  33. inline std::string
  34. cuu_error_location(const char *fileName,
  35. unsigned long lineNumber,
  36. const char *testName)
  37. {
  38. std::stringstream buffer;
  39. buffer << fileName << ":" << lineNumber << ": " << testName << " ";
  40. return buffer.str();
  41. }
  42.  
  43. inline std::string
  44. cuu_fail_error_message(const char *fileName,
  45. unsigned long lineNumber,
  46. const char *testName,
  47. const char *message)
  48. {
  49. std::stringstream buffer;
  50. buffer << cuu_error_location(fileName, lineNumber, testName)
  51. << "error: " << message;
  52. return buffer.str();
  53. }
  54.  
  55. template<typename T> std::string
  56. cuu_equals_error_message(const char *fileName,
  57. unsigned long lineNumber,
  58. const char *testName,
  59. T expected,
  60. T actual)
  61. {
  62. std::stringstream buffer;
  63. buffer << cuu_error_location(fileName, lineNumber, testName)
  64. << "expected: <" << expected << "> but was: <" << actual << ">";
  65. return buffer.str();
  66. }
  67.  
  68. inline std::string
  69. cuu_bool_error_message(const char *fileName,
  70. unsigned long lineNumber,
  71. const char *testName,
  72. const char *boolToCheckText,
  73. bool boolToCheck)
  74. {
  75. std::stringstream buffer;
  76. buffer << cuu_error_location(fileName, lineNumber, testName)
  77. << "expected: " << boolToCheckText << " to be "
  78. << cuu_condition_text(!boolToCheck);
  79. return buffer.str();
  80. }
  81.  
  82.  
  83. inline void
  84. cuu_assert_equal(const char *expected,
  85. const char *actual,
  86. const char *fileName,
  87. unsigned long lineNumber,
  88. const char *testName)
  89. {
  90. if (!strcmp(expected, actual))
  91. return;
  92. throw cuu_exception(cuu_equals_error_message(fileName, lineNumber,
  93. testName, expected, actual).c_str());
  94. }
  95.  
  96. template<typename T> void
  97. cuu_assert_equal(T expected,
  98. T actual,
  99. const char *fileName,
  100. unsigned long lineNumber,
  101. const char *testName)
  102. {
  103. if (expected == actual)
  104. return;
  105. throw cuu_exception(cuu_equals_error_message(fileName, lineNumber,
  106. testName, expected, actual).c_str());
  107. }
  108.  
  109. inline void
  110. cuu_assert_bool(bool boolToCheck,
  111. bool sense,
  112. const char *boolToCheckText,
  113. const char *fileName,
  114. unsigned long lineNumber,
  115. const char *testName)
  116. {
  117. if(boolToCheck == sense)
  118. return;
  119. throw cuu_exception(cuu_bool_error_message(fileName, lineNumber,
  120. testName, boolToCheckText, boolToCheck).c_str());
  121. }
  122.  
  123. inline void
  124. cuu_fail(const char *message, const char *fileName, unsigned long lineNumber, const char *testName)
  125. {
  126. throw cuu_exception(cuu_fail_error_message(fileName, lineNumber, testName, message).c_str());
  127. }
  128.  
  129. #define ASSERT_TRUE(x)\
  130. cuu_assert_bool((x),(true), #x, __FILE__, __LINE__, testName)
  131.  
  132. #define ASSERT_FALSE(x)\
  133. cuu_assert_bool((x),(false), #x, __FILE__, __LINE__, testName)
  134.  
  135. #define ASSERT_NULL(x)\
  136. ASSERT_TRUE((x) == 0)
  137.  
  138. #define ASSERT_NOT_NULL(x)\
  139. ASSERT_TRUE((x) != 0)
  140.  
  141. #define ASSERT_EQUAL(x,y)\
  142. cuu_assert_equal((x), (y), __FILE__, __LINE__, testName)
  143.  
  144. #define FAIL(message)\
  145. cuu_fail((message), __FILE__, __LINE__, testName)
  146.  
  147.  
  148. class TestResultCollector
  149. {
  150. public:
  151. static void addException(cuu_exception& e) { std::cout << e.what() << std::endl; }
  152. };
  153.  
  154. class Runner
  155. {
  156. public:
  157. virtual void run() = 0;
  158. virtual ~Runner() {}
  159. };
  160.  
  161. inline void run_runner(Runner * runner) { runner->run(); }
  162.  
  163. class TestRegistry
  164. {
  165. std::list<Runner *> runners;
  166.  
  167. static TestRegistry& instance() {
  168. static TestRegistry it;
  169. return it;
  170. }
  171.  
  172. public:
  173. static void addRunner(Runner *runner) {
  174. instance().runners.push_back(runner);
  175. }
  176.  
  177. static void runAll() {
  178. std::for_each(
  179. instance().runners.begin(),
  180. instance().runners.end(),
  181. run_runner);
  182. }
  183. };
  184.  
  185. template<typename TESTCASECLASS> class TestRunner : public Runner
  186. {
  187. public:
  188. void run() {
  189. try {
  190. TESTCASECLASS instance;
  191. }
  192. catch (cuu_exception& e) {
  193. reportError(e);
  194. }
  195. catch (std::exception& e) {
  196. reportError(std::string("Caught exception: ") + e.what());
  197. }
  198. catch(...) {
  199. reportError("Caught unknown exception");
  200. }
  201. }
  202. private:
  203. void reportError(const std::string& description) {
  204. std::stringstream message;
  205. message << description << " in " << TESTCASECLASS::testName;
  206. cuu_exception cuu_e(message.str());
  207. reportError(cuu_e);
  208. }
  209.  
  210. void reportError(cuu_exception& e) {
  211. TestResultCollector::addException(e);
  212. }
  213. };
  214.  
  215. template<typename TESTRUNNER> class TestRegistrar
  216. {
  217. public:
  218. TestRegistrar() { TestRegistry::addRunner(new TESTRUNNER);}
  219. };
  220.  
  221. class test {};
  222.  
  223.  
  224. #define TEST(suite, test)\
  225. class test##suite : public suite {\
  226. public:\
  227. test##suite();\
  228. static const char *testName;\
  229. };\
  230. const char *test##suite::testName = "[test <" #test "> in suite <" #suite ">]";\
  231. TestRegistrar<TestRunner<test##suite> > test##suite##registration;\
  232. test##suite::test##suite()
  233.  
  234. #ifdef MAIN
  235. int main() { TestRegistry::runAll(); return 0; }
  236. #endif
  237.  
  238. #endif
Add Comment
Please, Sign In to add comment