Added the hook for checking programmer error responses, and made sure it works. Now to simplify it.

This commit is contained in:
Pietro Gagliardi 2019-05-26 15:41:22 -04:00
parent 9060ef3852
commit 7808b3ee94
4 changed files with 55 additions and 2 deletions

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#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);
}

13
common/testhooks.h Normal file
View File

@ -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

View File

@ -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);
}

View File

@ -1,5 +1,6 @@
// 28 april 2019
#include "../ui.h"
#include "../common/testhooks.h"
#include "lib/testing.h"
#include "lib/timer.h"