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