And rewrote TestFunctionsFailBeforeInit. This should work nicely.

This commit is contained in:
Pietro Gagliardi 2019-06-09 15:29:03 -04:00
parent db3de44432
commit 91c1e8e517
1 changed files with 40 additions and 92 deletions

View File

@ -2,18 +2,14 @@
#include "test.h" #include "test.h"
struct errorCase { struct errorCase {
const char *name;
const char *file;
long line;
bool caught; bool caught;
char *msgGot; char *msgGot;
const char *msgWant; const char *msgWant;
struct errorCase *next;
}; };
static void catalogProgrammerError(const char *msg, void *data) static void handleProgrammerError(const char *msg, void *data)
{ {
static struct errorCase *c; struct errorCase *c = (struct errorCase *) data;
size_t n; size_t n;
c->caught = true; c->caught = true;
@ -23,100 +19,50 @@ static void catalogProgrammerError(const char *msg, void *data)
} }
} }
static struct errorCase *newCase(void) #define allcallsCase(f, ...) \
{ static void beforeInitCase ## f ## Impl(testingT *t, void *data) \
struct errorCase *p; { \
struct errorCase *c = (struct errorCase *) data; \
p = (struct errorCase *) malloc(sizeof (struct errorCase)); memset(c, 0, sizeof (struct errorCase)); \
if (p == NULL) { c->msgWant = "attempt to call " #f "() before uiInit()"; \
caseError = caseErrorMemoryExhausted; uiprivTestHookReportProgrammerError(handleProgrammerError, c); \
return NULL; f(__VA_ARGS__); \
if (!c->caught) \
testingTErrorf(t, "did not throw a programmer error; should have"); \
if (c->msgGot != NULL) { \
testingTErrorf(t, "message doesn't contain expected string:" diff("%s"), \
c->msgGot, c->msgWant); \
testingUtilFreeStrdup(c->msgGot); \
} \
uiprivTestHookReportProgrammerError(NULL, NULL); \
} }
memset(p, 0, sizeof (struct errorCase)); allcallsCase(uiQueueMain, NULL, NULL)
return p;
}
static void freeCases(struct errorCase *first)
{
struct errorCase *p, *next;
p = first;
while (p != NULL) {
if (p->msgGot != NULL)
testingUtilFreeStrdup(p->msgGot);
next = p->next;
free(p);
p = next;
}
}
static void reportCases(testingT *t, struct errorCase *p)
{
while (p != NULL) {
testingTLogfFull(t, p->file, p->line, "*** %s", p->name);
if (!p->caught) {
testingTErrorfFull(t, p->file, p->line, "%s did not throw a programmer error; should have", p->name);
p = p->next;
continue;
}
if (p->msgGot != NULL)
// TODO use diff
testingTErrorfFull(t, p->file, p->line, "%s message doesn't contain expected substring:" diff("%s"),
p->name, p->msgGot, p->msgWant);
p = p->next;
}
}
#define allcallsCase(f, ...) { \
current = newCase(); \
if (caseError != NULL) \
return first; \
current->name = #f "()"; \
current->file = __FILE__; \
current->line = __LINE__; \
current->msgWant = "attempt to call " #f "() " allcallsMsgSuffix; \
f(__VA_ARGS__); \
if (first == NULL) \
first = current; \
if (last != NULL) \
last->next = current; \
last = current; \
if (caseError != NULL) \
return first; \
}
static struct errorCase *runCasesBeforeInit(void)
{
struct errorCase *first = NULL;
struct errorCase *last = NULL;
#define allcallsMsgSuffix "before uiInit()"
allcallsCase(uiQueueMain, NULL, NULL);
#include "allcalls.h" #include "allcalls.h"
#undef allcallsMsgSuffix #undef allcallsCase
return first;
} static const struct {
const char *name;
void (*f)(testingT *, void *);
} beforeInitCases[] = {
#define allcallsCase(f, ...) { #f, beforeInitCase ## f ## Impl },
allcallsCase(uiQueueMain, NULL, NULL)
#include "allcalls.h"
#undef allcallsCase
{ NULL, NULL },
};
testingTestInSet(beforeTests, FunctionsFailBeforeInit) testingTestInSet(beforeTests, FunctionsFailBeforeInit)
{ {
struct errorCase *cases; struct errorCase c;
size_t i;
caseError = NULL; for (i = 0; beforeInitCases[i].name != NULL; i++)
uiprivTestHookReportProgrammerError(catalogProgrammerError); testingTRun(t, beforeInitCases[i].name, beforeInitCases[i].f, &c);
cases = runCasesBeforeInit();
uiprivTestHookReportProgrammerError(NULL);
if (caseError != NULL) {
freeCases(cases);
testingTErrorf(t, "%s running tests", caseError);
if (caseError != caseErrorMemoryExhausted && caseError != caseErrorEncodingError)
free(caseError);
caseError = NULL;
testingTFailNow(t);
}
reportCases(t, cases);
freeCases(cases);
} }
#if 0
TODOTODO
static struct errorCase *runCasesWrongThread(void) static struct errorCase *runCasesWrongThread(void)
{ {
struct errorCase *first = NULL; struct errorCase *first = NULL;
@ -166,3 +112,5 @@ testingTest(FunctionsFailOnWrongThread)
reportCases(t, cases); reportCases(t, cases);
freeCases(cases); freeCases(cases);
} }
#endif