Test.cppm 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. export module Core.Test;
  2. import Core.Logger;
  3. import Core.TerminalConstants;
  4. import Core.ToString;
  5. import Core.Types;
  6. import Core.Std;
  7. #define SOURCE const std::source_location& l = std::source_location::current()
  8. export namespace Core {
  9. void finalizeTests(void) noexcept;
  10. bool addTestResult(const char* file, bool comparison) noexcept;
  11. template<typename A, typename B>
  12. bool test(const A& wanted, const B& actual, SOURCE) noexcept {
  13. const char* file = getShortFileName(l.file_name());
  14. if(addTestResult(file, static_cast<const B&>(wanted) == actual)) {
  15. return true;
  16. }
  17. print(
  18. "{}{}:{} - expected '{}' got '{}'{}\n", Terminal::FG_RED, file,
  19. l.line(), wanted, actual, Terminal::RESET);
  20. return false;
  21. }
  22. template<typename T>
  23. bool testTrue(const T& actual, SOURCE) noexcept {
  24. return test<T>(true, actual, l);
  25. }
  26. template<typename T>
  27. bool testFalse(const T& actual, SOURCE) noexcept {
  28. return test<T>(false, actual, l);
  29. }
  30. bool testString(const char* wanted, const char* actual, SOURCE) noexcept;
  31. template<typename A, typename B>
  32. bool testString(const A& wanted, const B& actual, SOURCE) noexcept {
  33. String<512> wantedString;
  34. size_t lw = wantedString.add(wanted);
  35. String<512> actualString;
  36. size_t la = actualString.add(actual);
  37. test(lw, la, l);
  38. return testString(
  39. static_cast<const char*>(wantedString),
  40. static_cast<const char*>(actualString), l);
  41. }
  42. bool testFloat(float wanted, float actual, float error, SOURCE) noexcept;
  43. bool testNull(const void* p, SOURCE) noexcept;
  44. bool testNotNull(const void* p, SOURCE) noexcept;
  45. }