From 7808b3ee943157d6de983ece148c3400a3fbbb6e Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 26 May 2019 15:41:22 -0400 Subject: [PATCH] Added the hook for checking programmer error responses, and made sure it works. Now to simplify it. --- common/errors.c | 12 +++++++++++- common/testhooks.h | 13 +++++++++++++ test/events.c | 31 ++++++++++++++++++++++++++++++- test/test.h | 1 + 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 common/testhooks.h diff --git a/common/errors.c b/common/errors.c index 8f111841..a4d398d8 100644 --- a/common/errors.c +++ b/common/errors.c @@ -3,6 +3,7 @@ #include #include "ui.h" #include "uipriv.h" +#include "testhooks.h" #define internalErrorPrefix "libui internal error" // TODO add debugging advice? @@ -53,6 +54,15 @@ static void prepareProgrammerError(char *buf, int size, unsigned int which, va_l // TODO add debugging advice? #define programmerErrorSuffix "This likely means you are using libui incorrectly. Check your source code and try again. If you have received this warning in error, contact the libui authors." +static uiprivTestHookReportProgrammerErrorFunc reportProgrammerErrorTestHook = uiprivReportError; + +void uiprivTestHookReportProgrammerError(uiprivTestHookReportProgrammerErrorFunc f) +{ + if (f == NULL) + f = uiprivReportError; + reportProgrammerErrorTestHook = f; +} + void uiprivProgrammerError(unsigned int which, ...) { va_list ap; @@ -61,5 +71,5 @@ void uiprivProgrammerError(unsigned int which, ...) va_start(ap, which); prepareProgrammerError(buf, 256, which, ap); va_end(ap); - uiprivReportError(programmerErrorPrefix, buf, programmerErrorSuffix, false); + (*reportProgrammerErrorTestHook)(programmerErrorPrefix, buf, programmerErrorSuffix, false); } diff --git a/common/testhooks.h b/common/testhooks.h new file mode 100644 index 00000000..b1d9ce3d --- /dev/null +++ b/common/testhooks.h @@ -0,0 +1,13 @@ +// 26 may 2019 + +#ifdef __cplusplus +extern "C" { +#endif + +// errors.c +typedef void (*uiprivTestHookReportProgrammerErrorFunc)(const char *prefix, const char *msg, const char *suffix, bool internal); +uiprivExtern void uiprivTestHookReportProgrammerError(uiprivTestHookReportProgrammerErrorFunc f); + +#ifdef __cplusplus +} +#endif diff --git a/test/events.c b/test/events.c index 27f74732..97dbdcfe 100644 --- a/test/events.c +++ b/test/events.c @@ -684,7 +684,36 @@ testingTest(EventBlocksHonoredWithDifferentSenders) runGlobalSubtests(t, &p); } +static struct { + testingT *t; + const char *msgWant; + bool caught; +} errorParams; + +static void catchProgrammerError(const char *prefix, const char *msg, const char *suffix, bool internal) +{ + errorParams.caught = true; + if (strstr(prefix, "programmer error") == NULL) + testingTErrorf(errorParams.t, "prefix string doesn't contain \"programmer error\": %s", prefix); + if (internal) + testingTErrorf(errorParams.t, "error is marked internal; should not have been"); + if (strstr(msg, errorParams.msgWant) == NULL) + diff(errorParams.t, "message doesn't contain expected substring", + "%s", msg, errorParams.msgWant); +} + testingTest(EventErrors) { - // TODO + uiprivTestHookReportProgrammerError(catchProgrammerError); + + testingTLogf(t, "*** uiNewEvent(NULL)"); + errorParams.t = t; + errorParams.msgWant = "invalid null pointer for uiEventOptions passed into uiNewEvent()"; + errorParams.caught = false; + if (uiNewEvent(NULL) != NULL) + testingTErrorf(t, "uiNewEvent(NULL) returned non-NULL; wanted NULL"); + if (!errorParams.caught) + testingTErrorf(t, "uiNewEvent(NULL) did not throw a programmer error; should have"); + + uiprivTestHookReportProgrammerError(NULL); } diff --git a/test/test.h b/test/test.h index 796c347a..98d21979 100644 --- a/test/test.h +++ b/test/test.h @@ -1,5 +1,6 @@ // 28 april 2019 #include "../ui.h" +#include "../common/testhooks.h" #include "lib/testing.h" #include "lib/timer.h"