Added more event tests. DeleteHandler obviously does not work... and I really need to add subtests.

This commit is contained in:
Pietro Gagliardi 2019-05-18 21:21:35 -04:00
parent 43f7d1a661
commit 3b1f5e0e54
2 changed files with 110 additions and 15 deletions

View File

@ -2,6 +2,7 @@
#include "test.h"
struct handler {
const char *name;
bool run;
void *sender;
void *args;
@ -16,8 +17,22 @@ static void handler(void *sender, void *args, void *data)
h->args = args;
}
#define basicTest(name, whichGlobal, whichSender, whichArgs) \
testingTest(name) \
#define checkHandlerRun(h, whichSender, whichArgs) \
if (!h.run) \
testingTErrorf(t, "%s not run", h.name); \
if (h.sender != whichSender) \
diff_2str(t, "incorrect sender seen by", h.name, \
"%p", h.sender, whichSender); \
if (h.args != whichArgs) \
diff_2str(t, "incorrect args seen by", h.name, \
"%p", h.args, whichArgs);
#define checkHandlerNotRun(h) \
if (h.run) \
testingTErrorf(t, "%s run; should not have been", h.name);
#define TestBasicEventsSingleHandler(subname, whichGlobal, whichSender, whichArgs) \
testingTest(BasicEventsSingleHandler ## subname) \
{ \
uiEvent *e; \
uiEventOptions opts; \
@ -27,24 +42,103 @@ static void handler(void *sender, void *args, void *data)
opts.Global = whichGlobal; \
e = uiNewEvent(&opts); \
memset(&h, 0, sizeof (struct handler)); \
h.name = "handler"; \
uiEventAddHandler(e, handler, whichSender, &h); \
uiEventFire(e, whichSender, whichArgs); \
if (!h.run) \
testingTErrorf(t, "handler not run"); \
if (h.sender != whichSender) \
diff(t, "incorrect sender seen by handler", \
"%p", h.sender, whichSender); \
if (h.args != whichArgs) \
diff(t, "incorrect args seen by handler", \
"%p", h.args, whichArgs); \
checkHandlerRun(h, whichSender, whichArgs); \
}
basicTest(BasicEvents_Global_Args, true, NULL, &h)
basicTest(BasicEvents_Global_NoArgs, true, NULL, NULL)
basicTest(BasicEvents_Nonglobal_Args, false, &opts, &h)
basicTest(BasicEvents_Nonglobal_NoArgs, false, &opts, NULL)
TestBasicEventsSingleHandler(Global_Args, true, NULL, &h)
TestBasicEventsSingleHandler(Global_NoArgs, true, NULL, NULL)
TestBasicEventsSingleHandler(Nonglobal_Args, false, &opts, &h)
TestBasicEventsSingleHandler(Nonglobal_NoArgs, false, &opts, NULL)
testingTest(AddDeleteEventHandler)
#define whichGlobal true
#define whichSender NULL
#define whichArgs h
testingTest(BasicEventsAddDeleteEventHandlers)
{
uiEvent *e;
uiEventOptions opts;
struct handler hbase[6];
struct handler h[6];
int firstHandler, middleHandler, lastHandler;
memset(&opts, 0, sizeof (uiEventOptions));
opts.Size = sizeof (uiEventOptions);
opts.Global = whichGlobal;
e = uiNewEvent(&opts);
memset(hbase, 0, 6 * sizeof (struct handler));
hbase[0].name = "first handler";
hbase[1].name = "middle handler";
hbase[2].name = "last handler";
hbase[3].name = "new handler 1";
hbase[4].name = "new handler 2";
hbase[5].name = "new handler 3";
testingTLogf(t, "*** initial handlers");
firstHandler = uiEventAddHandler(e, handler, whichSender, h + 0);
middleHandler = uiEventAddHandler(e, handler, whichSender, h + 1);
uiEventAddHandler(e, handler, whichSender, h + 2);
memmove(h, hbase, 6 * sizeof (struct handler));
uiEventFire(e, whichSender, whichArgs);
checkHandlerRun(h[0], whichSender, whichArgs);
checkHandlerRun(h[1], whichSender, whichArgs);
checkHandlerRun(h[2], whichSender, whichArgs);
checkHandlerNotRun(h[3]);
checkHandlerNotRun(h[4]);
checkHandlerNotRun(h[5]);
testingTLogf(t, "*** deleting middle handler");
uiEventDeleteHandler(e, middleHandler);
memmove(h, hbase, 6 * sizeof (struct handler));
uiEventFire(e, whichSender, whichArgs);
checkHandlerRun(h[0], whichSender, whichArgs);
checkHandlerNotRun(h[1]);
checkHandlerRun(h[2], whichSender, whichArgs);
checkHandlerNotRun(h[3]);
checkHandlerNotRun(h[4]);
checkHandlerNotRun(h[5]);
testingTLogf(t, "*** adding handler after deleting middle handler");
uiEventAddHandler(e, handler, whichSender, h + 3);
memmove(h, hbase, 6 * sizeof (struct handler));
uiEventFire(e, whichSender, whichArgs);
checkHandlerRun(h[0], whichSender, whichArgs);
checkHandlerNotRun(h[1]);
checkHandlerRun(h[2], whichSender, whichArgs);
checkHandlerRun(h[3], whichSender, whichArgs);
checkHandlerNotRun(h[4]);
checkHandlerNotRun(h[5]);
testingTLogf(t, "*** deleting first handler and adding another");
uiEventDeleteHandler(e, firstHandler);
lastHandler = uiEventAddHandler(e, handler, whichSender, h + 4);
memmove(h, hbase, 6 * sizeof (struct handler));
uiEventFire(e, whichSender, whichArgs);
checkHandlerNotRun(h[0]);
checkHandlerNotRun(h[1]);
checkHandlerRun(h[2], whichSender, whichArgs);
checkHandlerRun(h[3], whichSender, whichArgs);
checkHandlerRun(h[4], whichSender, whichArgs);
checkHandlerNotRun(h[5]);
testingTLogf(t, "*** deleting last handler and adding another");
uiEventDeleteHandler(e, lastHandler);
uiEventAddHandler(e, handler, whichSender, h + 5);
memmove(h, hbase, 6 * sizeof (struct handler));
uiEventFire(e, whichSender, whichArgs);
checkHandlerNotRun(h[0]);
checkHandlerNotRun(h[1]);
checkHandlerRun(h[2], whichSender, whichArgs);
checkHandlerRun(h[3], whichSender, whichArgs);
checkHandlerNotRun(h[4]);
checkHandlerRun(h[5], whichSender, whichArgs);
}
testingTest(EventErrors)

View File

@ -4,6 +4,7 @@
#include "lib/timer.h"
#define diff(t, clause, fmt, got, want) testingTErrorf(t, "%s:\ngot " fmt "\nwant " fmt, clause, got, want)
#define diff_2str(t, clause, clause2, fmt, got, want) testingTErrorf(t, "%s %s:\ngot " fmt "\nwant " fmt, clause, clause2, got, want)
#define diff2(t, clause, fmts, got1, got2, want1, want2) testingTErrorf(t, "%s:\ngot " fmts "\nwant " fmts, clause, got1, got2, want1, want2)
#define diffFatal(t, clause, fmt, got, want) testingTFatalf(t, "%s:\ngot " fmt "\nwant " fmt, clause, got, want)
#define diff2Fatal(t, clause, fmts, got1, got2, want1, want2) testingTFatalf(t, "%s:\ngot " fmts "\nwant " fmts, clause, got1, got2, want1, want2)