1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-07 00:35:37 +02:00

Add TEST_ERROR_MULTI() macro.

This macro allows the error to be tested against multiple strings and it passes if any of them match.

This is useful for supporting multiple libc versions/implementations that have different error messages and is needed for an upcoming commit to add unit test support for musl libc.
This commit is contained in:
David Steele
2025-06-05 15:35:28 -04:00
parent ad7ba46bc6
commit b5fbb16cce
2 changed files with 71 additions and 32 deletions

View File

@ -136,6 +136,63 @@ Test that an expected error is actually thrown and error when it isn't
FUNCTION_HARNESS_STACK_TRACE_LINE_SET(0); \ FUNCTION_HARNESS_STACK_TRACE_LINE_SET(0); \
} }
/***********************************************************************************************************************************
Test that an expected error is actually thrown and error when it isn't
***********************************************************************************************************************************/
#define TEST_ERROR_MULTI(statement, errorTypeExpected, ...) \
{ \
const char *const errorMessageExpected[] = {__VA_ARGS__}; \
bool TEST_ERROR_catch = false; \
\
/* Set the line number for the current function in the stack trace */ \
FUNCTION_HARNESS_STACK_TRACE_LINE_SET(__LINE__); \
\
hrnTestLogPrefix(__LINE__); \
printf("expect %s: [%s]\n", errorTypeName(&errorTypeExpected), #__VA_ARGS__); \
fflush(stdout); \
\
TEST_ERROR_MEM_CONTEXT_BEGIN() \
{ \
TRY_BEGIN() \
{ \
statement; \
} \
CATCH_FATAL() \
{ \
TEST_ERROR_catch = true; \
bool match = false; \
\
if (errorType() == &errorTypeExpected) \
{ \
for (unsigned int errorIdx = 0; errorIdx < LENGTH_OF(errorMessageExpected); errorIdx++) \
{ \
if (strcmp(errorMessage(), errorMessageExpected[errorIdx]) == 0) \
{ \
match = true; \
break; \
} \
} \
} \
\
if (!match) \
{ \
THROW_FMT( \
TestError, "EXPECTED %s: [%s]\n\n BUT GOT %s: %s\n\nTHROWN AT:\n%s", errorTypeName(&errorTypeExpected), \
#__VA_ARGS__, errorName(), errorMessage(), errorStackTrace()); \
} \
} \
TRY_END(); \
} \
TEST_ERROR_MEM_CONTEXT_END(); \
\
if (!TEST_ERROR_catch) \
THROW_FMT( \
TestError, "statement '%s' returned but error %s, [%s] was expected", #statement, errorTypeName(&errorTypeExpected), \
#__VA_ARGS__); \
\
FUNCTION_HARNESS_STACK_TRACE_LINE_SET(0); \
}
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Test error with a formatted expected message Test error with a formatted expected message
***********************************************************************************************************************************/ ***********************************************************************************************************************************/

View File

@ -13,39 +13,21 @@ testRun(void)
// ***************************************************************************************************************************** // *****************************************************************************************************************************
if (testBegin("regExpNew(), regExpMatch(), and regExpFree()")) if (testBegin("regExpNew(), regExpMatch(), and regExpFree()"))
{ {
// Error message varies based on the libc version/OS TEST_ERROR_MULTI(
TRY_BEGIN() regExpNew(STRDEF("[[[")), FormatError,
{ // Older glibc
// Older libc "Unmatched [ or [^",
TEST_ERROR(regExpNew(STRDEF("[[[")), FormatError, "Unmatched [ or [^"); // Newer glibc
} "Unmatched [, [^, [:, [., or [=",
CATCH(TestError)
{
TRY_BEGIN()
{
// Newer libc
TEST_ERROR(regExpNew(STRDEF("[[[")), FormatError, "Unmatched [, [^, [:, [., or [=");
}
CATCH(TestError)
{
// MacOS
TEST_ERROR(regExpNew(STRDEF("[[[")), FormatError, "brackets ([ ]) not balanced");
}
TRY_END();
}
TRY_END();
TRY_BEGIN()
{
// libc
TEST_ERROR(regExpErrorCheck(REG_BADBR), FormatError, "Invalid content of \\{\\}");
}
CATCH(TestError)
{
// MacOS // MacOS
TEST_ERROR(regExpErrorCheck(REG_BADBR), FormatError, "invalid repetition count(s)"); "brackets ([ ]) not balanced");
}
TRY_END(); TEST_ERROR_MULTI(
regExpErrorCheck(REG_BADBR), FormatError,
// glibc
"Invalid content of \\{\\}",
// MacOS
"invalid repetition count(s)");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("new regexp"); TEST_TITLE("new regexp");