test.hpp 930 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Copyright (c) 2025 Michal Sledz
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include <functional>
  9. #include <iostream>
  10. using namespace std;
  11. class TestResult {
  12. public:
  13. bool success;
  14. string err_reason;
  15. TestResult(bool success, string err_reason = "") : success(success), err_reason(err_reason) {}
  16. };
  17. class Test {
  18. public:
  19. string name;
  20. function<TestResult(void)> f;
  21. Test(string name, std::function<TestResult(void)> testFunc) : name(name), f(testFunc) {}
  22. TestResult run() {
  23. cout << endl << "*** Running " << name << " test" << endl;
  24. TestResult res = this->f();
  25. if (res.success) {
  26. cout << "*** Finished " << name << " test" << endl;
  27. } else {
  28. cerr << name << " test failed. Reason: " << res.err_reason << endl;
  29. }
  30. return res;
  31. }
  32. };