Cleaned up programmer error testing code.

This commit is contained in:
Pietro Gagliardi 2019-05-26 20:19:02 -04:00
parent 7808b3ee94
commit 7cb4f010d4
3 changed files with 38 additions and 31 deletions

View File

@ -70,7 +70,7 @@ static void wantBlocked(struct handler *h)
h->wantBlocked = true; h->wantBlocked = true;
} }
// TODO carry over the file nad line numbers somehow // TODO carry over the file and line numbers somehow
static void run(testingT *t, uiEvent *e, void *sender, void *args, struct handler *handlers, int n, int wantRunCount) static void run(testingT *t, uiEvent *e, void *sender, void *args, struct handler *handlers, int n, int wantRunCount)
{ {
int i; int i;
@ -684,36 +684,8 @@ testingTest(EventBlocksHonoredWithDifferentSenders)
runGlobalSubtests(t, &p); 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) testingTest(EventErrors)
{ {
uiprivTestHookReportProgrammerError(catchProgrammerError); testProgrammerError(t, uiNewEvent(NULL),
"invalid null pointer for uiEventOptions passed into uiNewEvent()");
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

@ -8,6 +8,20 @@ void timeoutMain(void *data)
uiMain(); uiMain();
} }
struct errorParams errorParams;
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, "%s prefix string doesn't contain \"programmer error\": %s", errorParams.exprstr, prefix);
if (internal)
testingTErrorf(errorParams.t, "%s error is marked internal; should not have been", errorParams.exprstr);
if (strstr(msg, errorParams.msgWant) == NULL)
diff_2str(errorParams.t, errorParams.exprstr, "message doesn't contain expected substring",
"%s", msg, errorParams.msgWant);
}
static void runSetORingResults(testingSet *set, const struct testingOptions *options, int *anyRun, int *anyFailed) static void runSetORingResults(testingSet *set, const struct testingOptions *options, int *anyRun, int *anyFailed)
{ {
int ar, af; int ar, af;

View File

@ -25,6 +25,27 @@ extern void timeoutMain(void *data);
testingTErrorf(t, "uiMain() timed out (%s)", timeoutstr); \ testingTErrorf(t, "uiMain() timed out (%s)", timeoutstr); \
} \ } \
} }
struct errorParams {
testingT *t;
// TODO this shouldn't have a colon in it but the diff() macros above necessitate it
const char *exprstr;
const char *msgWant;
bool caught;
};
extern struct errorParams errorParams;
extern void catchProgrammerError(const char *prefix, const char *msg, const char *suffix, bool internal);
#define testProgrammerError(tt, expr, mw) { \
testingTLogf(t, "*** %s", #expr); \
uiprivTestHookReportProgrammerError(catchProgrammerError); \
errorParams.t = tt; \
errorParams.exprstr = #expr ":"; \
errorParams.msgWant = mw; \
errorParams.caught = false; \
expr; \
if (!errorParams.caught) \
testingTErrorf(t, "%s did not throw a programmer error; should have", #expr); \
uiprivTestHookReportProgrammerError(NULL); \
}
// init.c // init.c
extern testingSet *beforeTests; extern testingSet *beforeTests;