Skip to content

Commit 6b180d6

Browse files
committed
Added macros to support noreturn functions
Added TEST_PROTECT_NORETURN(), TEST_DO_NOT_RETURN(), and TEST_NOT_RETURNING(call) to test for non-returning functions. Mock functions that are declared noreturn shall use TEST_DO_NOT_RETURN() instead of return.
1 parent b4ac04a commit 6b180d6

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

docs/UnityAssertionsReference.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,50 @@ This can be useful for outputting `INFO` messages into the Unity output stream
222222
without actually ending the test. Like pass and fail messages, it will be output
223223
with the filename and line number.
224224
225+
#### `TEST_PROTECT_NORETURN()`
226+
227+
This macro prepares an environment to test a function that is declared `noreturn`.
228+
229+
```
230+
if(TEST_PROTECT_NORETURN()){
231+
}
232+
```
233+
234+
The function `mock_go_elsewhere()` would use `TEST_DO_NOT_RETURN()`
235+
instead of returning.
236+
237+
#### `TEST_NOT_RETURNING(call)`
238+
239+
Calls a function, and the test fails if the function returns.
240+
Instead, the called function should use `TEST_DO_NOT_RETURN()`.
241+
242+
```
243+
/* prepare the test here */
244+
TEST_NOT_RETURNING(go_elsewhere(42));
245+
```
246+
247+
Note: this can be used to call a mock function (that uses
248+
`TEST_NOT_RETURNING()` instead of returning), or an actual function
249+
that effectively does not return. In the later case, the control
250+
won't resume to the caller test (unless there's a failure), so the
251+
test should expect that and have set up things accordingly.
252+
253+
This cannot be used if `UNITY_EXCLUDE_SETJMP_H` is defined.
254+
255+
#### `TEST_DO_NOT_RETURN()`
256+
257+
Aborts the execution, and resume after the current
258+
`TEST_NOT_RETURNING()`, successfully.
259+
260+
This should be used in mock functions declared `noreturn` instead of
261+
returning (explicitely or implicitely).
262+
263+
Note: actual functions that don't return will go wherever they shoud
264+
go and it's assumed this is handled in the test. These macros are
265+
intended to be used by mock functions.
266+
267+
This cannot be used if `UNITY_EXCLUDE_SETJMP_H` is defined.
268+
225269
### Boolean
226270
227271
#### `TEST_ASSERT (condition)`

src/unity_internals.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ struct UNITY_STORAGE_T
509509
#endif
510510
#ifndef UNITY_EXCLUDE_SETJMP_H
511511
jmp_buf AbortFrame;
512+
jmp_buf NoReturnFrame;
512513
#endif
513514
};
514515

@@ -762,9 +763,28 @@ extern const char UnityStrErrShorthand[];
762763
#ifndef UNITY_EXCLUDE_SETJMP_H
763764
#define TEST_PROTECT() (setjmp(Unity.AbortFrame) == 0)
764765
#define TEST_ABORT() longjmp(Unity.AbortFrame, 1)
766+
767+
#define TEST_PROTECT_NORETURN() (setjmp(Unity.NoReturnFrame) == 0)
768+
#define TEST_DO_NOT_RETURN() longjmp(Unity.NoReturnFrame, 1)
769+
770+
/*
771+
TEST_NOT_RETURNING(call)
772+
This macro uses TEST_PROTECT_NORETURN to protect the call.
773+
If the call returns, then the test fails.
774+
*/
775+
#define TEST_NOT_RETURNING(call) \
776+
if (TEST_PROTECT_NORETURN()) \
777+
{ \
778+
call; \
779+
UnityFail("Call Was Expected Not To Return: " #call, __LINE__); \
780+
}
781+
765782
#else
766783
#define TEST_PROTECT() 1
767784
#define TEST_ABORT() return
785+
#define TEST_PROTECT_NORETURN() _Static_assert(false, "TEST_PROTECT_NORETURN() cannot be used when UNITY_EXCLUDE_SETJMP_H is defined"
786+
#define TEST_DO_NOT_RETURN() _Static_assert(false, "TEST_DO_NOT_RETURN() cannot be used when UNITY_EXCLUDE_SETJMP_H is defined"
787+
#define TEST_NOT_RETURNING(call) _Static_assert(false, "TEST_NOT_RETURNING(call) cannot be used when UNITY_EXCLUDE_SETJMP_H is defined"
768788
#endif
769789

770790
/* Automatically enable variadic macros support, if it not enabled before */

0 commit comments

Comments
 (0)