Change bx_fprintf to a function pointer to enable unit testing on MacOS

This commit is contained in:
David Sargeant 2023-11-25 11:34:07 -07:00 committed by Thomas Jensen
parent f369cb80ba
commit 54957a144b
2 changed files with 33 additions and 3 deletions

View File

@ -42,6 +42,15 @@
static pcre2_code *pattern_ascii_id = NULL;
static pcre2_code *pattern_ascii_id_strict = NULL;
/**
* Initialize the `bx_fprintf` function pointer to point to the original
* `bx_fprintf` function, now renamed `bx_fprintf_original`. During unit
* tests, this will be replaced with `__wrap_bx_fprintf`, which stores
* the result that would have been printed so the output can be validated.
* This is necessary for unit testing and CI to work with MacOS.
*/
bx_fprintf_t bx_fprintf = bx_fprintf_original;
static pcre2_code *get_pattern_ascii_id(int strict)
{
@ -849,7 +858,7 @@ char *bx_strndup(const char *s, size_t n)
void bx_fprintf(FILE *stream, const char *format, ...)
void bx_fprintf_original(FILE *stream, const char *format, ...)
{
va_list va;
va_start(va, format);
@ -858,6 +867,10 @@ void bx_fprintf(FILE *stream, const char *format, ...)
}
void set_bx_fprintf(bx_fprintf_t bx_fprintf_function) {
bx_fprintf = bx_fprintf_function;
}
FILE *bx_fopens(bxstr_t *pathname, char *mode)
{

View File

@ -40,6 +40,18 @@
}
/**
* Define type for a function pointer to specify which `bx_fprintf` function
* will be called. This enables unit testing on MacOS, since Apple's `ld`
* linker does not support the `--wrap` flag that GNU `ld` does.
*/
typedef void (*bx_fprintf_t)(FILE *stream, const char *format, ...);
/**
* Declare function pointer to be changed when running unit tests
*/
extern bx_fprintf_t bx_fprintf;
int empty_line(const line_t *line);
@ -173,7 +185,7 @@ int tag_is_valid(char *tag);
/**
* Duplicate at most `n` bytes from the given string `s`. Memory for the new string is obtained with `malloc()`, and
* can be freed with `free()`. A terminating null byte is added. We include this implementation because the libc's
* can be freed with `free()`. A terminating null byte is added. We include this implementation because the libc's
* `strndup()` is not consistently available across all platforms.
* @param s a string
* @param n maximum number of characters to copy (excluding the null byte)
@ -187,9 +199,14 @@ char *bx_strndup(const char *s, size_t n);
* @param stream Where to print, for example `stderr`
* @param format the format string, followed by the arguments of the format string
*/
void bx_fprintf(FILE *stream, const char *format, ...);
void bx_fprintf_original(FILE *stream, const char *format, ...);
/**
* Set the bx_fprintf_ptr function pointer to point to a specific function
*/
void set_bx_fprintf(bx_fprintf_t func_to_use);
/**
* Determine if the given string is an "ASCII ID", which means:
* - It consists only of the letters `abcdefghijklmnopqrstuvwxyz-0123456789`. If not in strict mode, upper case A-Z