You've already forked pgbackrest
							
							
				mirror of
				https://github.com/pgbackrest/pgbackrest.git
				synced 2025-10-30 23:37:45 +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:
		| @@ -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);                                                                                      \ | ||||
| } | ||||
|  | ||||
| /*********************************************************************************************************************************** | ||||
| 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 | ||||
| ***********************************************************************************************************************************/ | ||||
|   | ||||
| @@ -13,39 +13,21 @@ testRun(void) | ||||
|     // ***************************************************************************************************************************** | ||||
|     if (testBegin("regExpNew(), regExpMatch(), and regExpFree()")) | ||||
|     { | ||||
|         // Error message varies based on the libc version/OS | ||||
|         TRY_BEGIN() | ||||
|         { | ||||
|             // Older libc | ||||
|             TEST_ERROR(regExpNew(STRDEF("[[[")), FormatError, "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) | ||||
|         { | ||||
|         TEST_ERROR_MULTI( | ||||
|             regExpNew(STRDEF("[[[")), FormatError, | ||||
|             // Older glibc | ||||
|             "Unmatched [ or [^", | ||||
|             // Newer glibc | ||||
|             "Unmatched [, [^, [:, [., or [=", | ||||
|             // MacOS | ||||
|             TEST_ERROR(regExpErrorCheck(REG_BADBR), FormatError, "invalid repetition count(s)"); | ||||
|         } | ||||
|         TRY_END(); | ||||
|             "brackets ([ ]) not balanced"); | ||||
|  | ||||
|         TEST_ERROR_MULTI( | ||||
|             regExpErrorCheck(REG_BADBR), FormatError, | ||||
|             // glibc | ||||
|             "Invalid content of \\{\\}", | ||||
|             // MacOS | ||||
|             "invalid repetition count(s)"); | ||||
|  | ||||
|         // ------------------------------------------------------------------------------------------------------------------------- | ||||
|         TEST_TITLE("new regexp"); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user