unity_cxx.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "unity.h"
  8. #define CXX_UNITY_TYPE_TO_STR(x) #x
  9. /**
  10. * Very simple helper macro to catch exceptions.
  11. *
  12. * @note
  13. * * If there is any exception which not a child of std::exception, it will terminate the program!
  14. * * If there is no exception, it will jump from the current frame without de-initializing
  15. * destructors!
  16. */
  17. #define TEST_THROW(expr_, exception_) \
  18. do { \
  19. bool caught = false; \
  20. bool caught_different = false; \
  21. try { \
  22. expr_; \
  23. } catch ( exception_ &e) { \
  24. caught = true; \
  25. } catch ( std::exception &e) { \
  26. caught_different = true; \
  27. } \
  28. TEST_ASSERT_FALSE_MESSAGE(caught_different, "ERROR: Expected " CXX_UNITY_TYPE_TO_STR(exception_) \
  29. ", but caught different exception."); \
  30. TEST_ASSERT_TRUE_MESSAGE(caught, "ERROR: Expected " CXX_UNITY_TYPE_TO_STR(exception_) \
  31. ", but no exception thrown."); \
  32. } \
  33. while (0)