Test.cppm 1.8 KB

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