mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-06-27 21:12:55 +08:00
Add MACROs to check if tests are built using the Output Spy
Ignore tests that need the Spy if we are not building with it
This commit is contained in:
@ -308,8 +308,17 @@ TEST_TEAR_DOWN(LeakDetection)
|
||||
memcpy(Unity.AbortFrame, TestAbortFrame, sizeof(jmp_buf)); \
|
||||
}
|
||||
|
||||
// This tricky set of defines lets us see if we are using the Spy, returns 1 if true, else 0
|
||||
#define USING_OUTPUT_SPY(a) EXPAND_AND_USE_2ND(ASSIGN_VALUE(a), 0)
|
||||
#define ASSIGN_VALUE(a) VAL_FUNC_##a
|
||||
#define VAL_FUNC_UnityOutputCharSpy_OutputChar() 0, 1
|
||||
#define EXPAND_AND_USE_2ND(a, b) SECOND_PARAM(a, b)
|
||||
#define SECOND_PARAM(a, b, ...) b
|
||||
TEST(LeakDetection, DetectsLeak)
|
||||
{
|
||||
#if USING_OUTPUT_SPY(UNITY_OUTPUT_CHAR()) == 0
|
||||
TEST_IGNORE_MESSAGE("Build with '-D UNITY_OUTPUT_CHAR=UnityOutputCharSpy_OutputChar' to enable tests");
|
||||
#else
|
||||
void* m = malloc(10);
|
||||
UnityOutputCharSpy_Enable(1);
|
||||
EXPECT_ABORT_BEGIN
|
||||
@ -319,10 +328,15 @@ TEST(LeakDetection, DetectsLeak)
|
||||
Unity.CurrentTestFailed = 0;
|
||||
CHECK(strstr(UnityOutputCharSpy_Get(), "This test leaks!"));
|
||||
free(m);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(LeakDetection, BufferOverrunFoundDuringFree)
|
||||
{
|
||||
#if USING_OUTPUT_SPY(UNITY_OUTPUT_CHAR()) == 0
|
||||
UNITY_PRINT_EOL();
|
||||
TEST_IGNORE();
|
||||
#else
|
||||
void* m = malloc(10);
|
||||
char* s = (char*)m;
|
||||
s[10] = (char)0xFF;
|
||||
@ -333,10 +347,15 @@ TEST(LeakDetection, BufferOverrunFoundDuringFree)
|
||||
UnityOutputCharSpy_Enable(0);
|
||||
Unity.CurrentTestFailed = 0;
|
||||
CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during free()"));
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(LeakDetection, BufferOverrunFoundDuringRealloc)
|
||||
{
|
||||
#if USING_OUTPUT_SPY(UNITY_OUTPUT_CHAR()) == 0
|
||||
UNITY_PRINT_EOL();
|
||||
TEST_IGNORE();
|
||||
#else
|
||||
void* m = malloc(10);
|
||||
char* s = (char*)m;
|
||||
s[10] = (char)0xFF;
|
||||
@ -347,4 +366,5 @@ TEST(LeakDetection, BufferOverrunFoundDuringRealloc)
|
||||
UnityOutputCharSpy_Enable(0);
|
||||
Unity.CurrentTestFailed = 0;
|
||||
CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during realloc()"));
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user