Skip to content

Commit f146260

Browse files
committedFeb 14, 2023
Create a TRY CATCH macro for unit tests
1 parent 939a21a commit f146260

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
 

‎test/_resources/Unity/unit_test.c

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ ut_luos_assert_t ut_assert = {.state = 0, .enable = 1};
1919
uint16_t test_case_number;
2020
uint16_t step_number;
2121

22+
// external error context
23+
jmp_buf err_ctx;
24+
bool try_state = false;
25+
2226
/*******************************************************************************
2327
* Function
2428
******************************************************************************/
@@ -192,6 +196,12 @@ void UNIT_TEST_ASSERT(char *file, uint32_t line)
192196
memcpy(msg.data, &line, sizeof(line));
193197
memcpy(&msg.data[sizeof(line)], file, strlen(file));
194198
ut_assert.msg = msg;
199+
// This is the THROW of the TRY CATCH
200+
if (try_state)
201+
{
202+
_LONGJMP(err_ctx, 1);
203+
try_state = false;
204+
}
195205
}
196206

197207
void setUp(void)

‎test/_resources/Unity/unit_test.h

+18
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@
1313
#include "luos_engine.h"
1414
#include "luos_utils.h"
1515
#include "robus_struct.h"
16+
#include <setjmp.h>
1617

1718
/*******************************************************************************
1819
* Definitions
1920
******************************************************************************/
21+
#ifdef _WIN32
22+
#define _SETJMP __builtin_setjmp
23+
#define _LONGJMP __builtin_longjmp
24+
#else
25+
#define _SETJMP setjmp
26+
#define _LONGJMP longjmp
27+
#endif
28+
29+
extern jmp_buf err_ctx;
30+
extern bool try_state;
31+
2032
#ifndef UNIT_TEST_RUN
2133
#define UNIT_TEST_RUN(f) RUN(#f, f)
2234
#endif
@@ -32,6 +44,12 @@ typedef struct
3244
msg_t msg;
3345
} ut_luos_assert_t;
3446

47+
#define TRY \
48+
try_state = true; \
49+
if (!_SETJMP(err_ctx))
50+
51+
#define CATCH else
52+
3553
/*******************************************************************************
3654
* Function
3755
******************************************************************************/

0 commit comments

Comments
 (0)
Please sign in to comment.