unit_tests.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include <list>
  24. #include <string>
  25. #include <unity.h>
  26. // Include MarlinConfig so configurations are available to all tests
  27. #include "src/inc/MarlinConfig.h"
  28. /**
  29. * Class that allows us to dynamically collect tests
  30. */
  31. class MarlinTest {
  32. public:
  33. MarlinTest(const std::string name, const void(*test)(), const char *_file, const int line);
  34. /**
  35. * Run the test via Unity
  36. */
  37. void run();
  38. /**
  39. * The name, a pointer to the function, and the line number. These are
  40. * passed to the Unity test framework.
  41. */
  42. const std::string name;
  43. const void(*test)();
  44. const std::string file;
  45. const int line;
  46. };
  47. /**
  48. * Internal macros used by MARLIN_TEST
  49. */
  50. #define _MARLIN_TEST_CLASS_NAME(SUITE, NAME) MarlinTestClass_##SUITE##_##NAME
  51. #define _MARLIN_TEST_INSTANCE_NAME(SUITE, NAME) MarlinTestClass_##SUITE##_##NAME##_instance
  52. /**
  53. * Macro to define a test. This will create a class with the test body and
  54. * register it with the global list of tests.
  55. *
  56. * Usage:
  57. * MARLIN_TEST(test_suite_name, test_name) {
  58. * // Test body
  59. * }
  60. */
  61. #define MARLIN_TEST(SUITE, NAME) \
  62. class _MARLIN_TEST_CLASS_NAME(SUITE, NAME) : public MarlinTest { \
  63. public: \
  64. _MARLIN_TEST_CLASS_NAME(SUITE, NAME)() : MarlinTest(#SUITE "___" #NAME, (const void(*)())&TestBody, __FILE__, __LINE__) {} \
  65. static void TestBody(); \
  66. }; \
  67. const _MARLIN_TEST_CLASS_NAME(SUITE, NAME) _MARLIN_TEST_INSTANCE_NAME(SUITE, NAME); \
  68. void _MARLIN_TEST_CLASS_NAME(SUITE, NAME)::TestBody()