test_cxx_exceptions.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <stdio.h>
  2. #include <cstring>
  3. #include "unity.h"
  4. #include "unity_cxx.hpp"
  5. #include "esp_exception.hpp"
  6. #ifdef __cpp_exceptions
  7. using namespace std;
  8. using namespace idf;
  9. #define TAG "CXX Exception Test"
  10. #if CONFIG_IDF_TARGET_ESP32
  11. #define LEAKS "300"
  12. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  13. #define LEAKS "800"
  14. #else
  15. #error "unknown target in CXX tests, can't set leaks threshold"
  16. #endif
  17. TEST_CASE("TEST_THROW catches exception", "[cxx exception][leaks=" LEAKS "]")
  18. {
  19. TEST_THROW(throw ESPException(ESP_FAIL);, ESPException);
  20. }
  21. /* The following two test cases are expected to fail */
  22. TEST_CASE("TEST_THROW asserts catching different exception", "[cxx exception][ignore]")
  23. {
  24. TEST_THROW(throw std::exception();, ESPException);
  25. }
  26. TEST_CASE("TEST_THROW asserts not catching any exception", "[cxx exception][ignore]")
  27. {
  28. TEST_THROW(printf(" ");, ESPException); // need statement with effect
  29. }
  30. TEST_CASE("CHECK_THROW continues on ESP_OK", "[cxx exception][leaks=" LEAKS "]")
  31. {
  32. esp_err_t error = ESP_OK;
  33. CHECK_THROW(error);
  34. }
  35. TEST_CASE("CHECK_THROW throws", "[cxx exception][leaks=" LEAKS "]")
  36. {
  37. esp_err_t error = ESP_FAIL;
  38. TEST_THROW(CHECK_THROW(error), ESPException);
  39. }
  40. TEST_CASE("ESPException has working what() method", "[cxx exception][leaks=" LEAKS "]")
  41. {
  42. try {
  43. throw ESPException(ESP_FAIL);
  44. } catch (ESPException &e) {
  45. TEST_ASSERT(strcmp(esp_err_to_name(ESP_FAIL), e.what()) == 0);
  46. }
  47. }
  48. #endif // __cpp_exceptions