#include "../irc_cmd.h"
#include "test.h"
struct test_irc_cmd_ctx {
const struct irc_line *line;
int test;
};
static void on_TEST_1 (const struct irc_line *line, void *arg)
{
struct test_irc_cmd_ctx *ctx = arg;
log_debug("%s: %s", line->command, line->args[0]);
assert(line == ctx->line);
assert(ctx->test = 1);
ctx->test = 0;
}
static void on_TEST_2 (const struct irc_line *line, void *arg)
{
struct test_irc_cmd_ctx *ctx = arg;
log_debug("%s: %s", line->command, line->args[0]);
assert(line == ctx->line);
assert(ctx->test = 2);
ctx->test = 0;
}
static const struct irc_cmd_handler _handlers[] = {
{ "TEST_1", on_TEST_1 },
{ "TEST_2", on_TEST_2 },
{ NULL, NULL }
};
void assert_cmd_invoke (struct irc_cmd_handlers *handlers, struct test_irc_cmd_ctx *ctx, const char *command, int test)
{
struct irc_line line;
// setup line
ctx->line = &line;
line.source = NULL;
line.command = command;
line.args[0] = "this is a test";
ctx->test = test;
irc_cmd_invoke(handlers, &line);
if (test < 0)
assert(ctx->test == -1);
else
assert(!ctx->test);
}
void test_irc_cmd (void)
{
struct test_irc_cmd_ctx ctx;
struct irc_cmd_handlers handlers;
// setup our handlers
irc_cmd_init(&handlers);
irc_cmd_add(&handlers, _handlers, &ctx);
// test first command
assert_cmd_invoke(&handlers, &ctx, "TEST_1", 1);
// test second command
assert_cmd_invoke(&handlers, &ctx, "TEST_2", 2);
// test unknown command
assert_cmd_invoke(&handlers, &ctx, "TEST_3", -1);
// remove unknown handler (wrong ctx arg)
irc_cmd_remove(&handlers, _handlers, &handlers);
// ensure it still works
assert_cmd_invoke(&handlers, &ctx, "TEST_1", 1);
// remove the right handler
irc_cmd_remove(&handlers, _handlers, &ctx);
// ensure it doesn't work anymore
assert_cmd_invoke(&handlers, &ctx, "TEST_1", -1);
// do clear, although this will be empty...
irc_cmd_clear(&handlers);
}