test_suite.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "test_suite.hpp"
  2. #include <iostream>
  3. std::vector<std::pair<std::function<void(void)>, std::string>> &test_suite::tests()
  4. {
  5. static std::vector<std::pair<std::function<void(void)>, std::string>> all_tests;
  6. return all_tests;
  7. }
  8. std::string build_name(const std::string &pretty, const std::string &method)
  9. {
  10. return pretty.substr(0, pretty.find("::") + 2) + method;
  11. }
  12. test_status test_suite::go()
  13. {
  14. test_status status;
  15. for (auto test : tests())
  16. {
  17. try
  18. {
  19. test.first();
  20. std::cout << '.';
  21. status.tests_passed++;
  22. }
  23. catch (std::exception &ex)
  24. {
  25. std::string fail_msg = test.second + " failed with:\n" + std::string(ex.what());
  26. std::cout << "*\n"
  27. << fail_msg << '\n';
  28. status.tests_failed++;
  29. status.failures.push_back(fail_msg);
  30. }
  31. catch (...)
  32. {
  33. std::cout << "*\n"
  34. << test.second << " failed\n";
  35. status.tests_failed++;
  36. status.failures.push_back(test.second);
  37. }
  38. std::cout.flush();
  39. status.tests_run++;
  40. }
  41. return status;
  42. }