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" #include "test.h"
struct handler { struct handler {
const char *name;
bool run; bool run;
void *sender; void *sender;
void *args; void *args;
@ -16,8 +17,22 @@ static void handler(void *sender, void *args, void *data)
h->args = args; h->args = args;
} }
#define basicTest(name, whichGlobal, whichSender, whichArgs) \ #define checkHandlerRun(h, whichSender, whichArgs) \
testingTest(name) \ 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; \ uiEvent *e; \
uiEventOptions opts; \ uiEventOptions opts; \
@ -27,24 +42,103 @@ static void handler(void *sender, void *args, void *data)
opts.Global = whichGlobal; \ opts.Global = whichGlobal; \
e = uiNewEvent(&opts); \ e = uiNewEvent(&opts); \
memset(&h, 0, sizeof (struct handler)); \ memset(&h, 0, sizeof (struct handler)); \
h.name = "handler"; \
uiEventAddHandler(e, handler, whichSender, &h); \ uiEventAddHandler(e, handler, whichSender, &h); \
uiEventFire(e, whichSender, whichArgs); \ uiEventFire(e, whichSender, whichArgs); \
if (!h.run) \ checkHandlerRun(h, whichSender, whichArgs); \
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); \
} }
basicTest(BasicEvents_Global_Args, true, NULL, &h) TestBasicEventsSingleHandler(Global_Args, true, NULL, &h)
basicTest(BasicEvents_Global_NoArgs, true, NULL, NULL) TestBasicEventsSingleHandler(Global_NoArgs, true, NULL, NULL)
basicTest(BasicEvents_Nonglobal_Args, false, &opts, &h) TestBasicEventsSingleHandler(Nonglobal_Args, false, &opts, &h)
basicTest(BasicEvents_Nonglobal_NoArgs, false, &opts, NULL) 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) testingTest(EventErrors)

View File

@ -4,6 +4,7 @@
#include "lib/timer.h" #include "lib/timer.h"
#define diff(t, clause, fmt, got, want) testingTErrorf(t, "%s:\ngot " fmt "\nwant " fmt, clause, got, want) #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 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 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) #define diff2Fatal(t, clause, fmts, got1, got2, want1, want2) testingTFatalf(t, "%s:\ngot " fmts "\nwant " fmts, clause, got1, got2, want1, want2)