From cafa29426f5a295a4d131d6f135970f11e9f9513 Mon Sep 17 00:00:00 2001 From: Steffen Mecke Date: Tue, 31 May 2016 10:37:01 +0200 Subject: [PATCH] simplified message_test --- src/monsters.test.c | 74 ++------------------------------------------- src/tests.c | 23 ++++++++++++++ src/tests.h | 1 + 3 files changed, 26 insertions(+), 72 deletions(-) diff --git a/src/monsters.test.c b/src/monsters.test.c index 41b655cd6..387ac3d5d 100644 --- a/src/monsters.test.c +++ b/src/monsters.test.c @@ -188,74 +188,6 @@ static void test_dragon_attacks_the_rich(CuTest * tc) test_cleanup(); } -typedef struct msg_info { - const char *name; - int numpar; - const char **argv; - const char **pnames, **tnames; - variant *values; -} msg_info; - -static msg_info *test_create_messagetype(const struct locale *lang, const char *name, int num_args, ...) { - va_list vargs; - const message_type *mtype; - char zBuffer[128]; - int i; - msg_info *info = malloc(sizeof(msg_info)); - info->argv = calloc(num_args + 1, sizeof(char *)); - info->pnames = calloc(num_args, sizeof(char *)); - info->tnames = calloc(num_args, sizeof(char *)); - info->values = calloc(num_args, sizeof(variant_type)); - - va_start(vargs, num_args); - - info->numpar = num_args; - info->name = name; - - for(i = 0; ipnames[i] = va_arg(vargs, char *); - info->tnames[i] = va_arg(vargs, char *); - sprintf(zBuffer, "%s:%s", info->pnames[i], info->tnames[i]); - info->argv[i] = _strdup(zBuffer); - } - - mtype = mt_register(mt_new(name, info->argv)); - nrt_register(mtype, lang, "mocktext", 0, "mocksection"); - - for(i = 0; itypes[i]->vtype == VAR_VOIDPTR) { - info->values[i].v = va_arg(vargs, char *); - } else if (mtype->types[i]->vtype == VAR_INT) { - info->values[i].i = va_arg(vargs, int); - } else { - assert(!"unknown variant type"); - } - } - va_end(vargs); - return info; -} - -static void assert_message(CuTest * tc, message *msg, msg_info *info) -{ - const message_type *mtype = msg->type; - assert(mtype); - int i; - - CuAssertStrEquals(tc, info->name, mtype->name); - CuAssertIntEquals(tc, info->numpar, mtype->nparameters); - for (i = 0; i != mtype->nparameters; ++i) { - CuAssertStrEquals(tc, info->pnames[i], mtype->pnames[i]); - CuAssertStrEquals(tc, info->tnames[i], mtype->types[i]->name); - if (mtype->types[i]->vtype == VAR_VOIDPTR) { - CuAssertPtrEquals(tc, info->values[i].v, msg->parameters[i].v); - } else if (mtype->types[i]->vtype == VAR_INT) { - CuAssertIntEquals(tc, info->values[i].i, msg->parameters[i].i); - } else { - assert(!"unknown variant type"); - } - } -} - extern void random_growl(const unit *u, region *tr, int rand); static void test_dragon_moves(CuTest * tc) @@ -264,7 +196,6 @@ static void test_dragon_moves(CuTest * tc) region *r; unit *u, *m; struct message *msg; - struct msg_info *msginfo; create_monsters(&f, &f2, &u, &m); rsetmoney(findregion(1, 0), 1000); @@ -278,13 +209,12 @@ static void test_dragon_moves(CuTest * tc) CuAssertPtrNotNull(tc, find_order("move east", m)); - register_reports(); - msginfo = test_create_messagetype(f->locale, "dragon_growl", 4, "dragon", "unit", "number", "int", "target", "region", "choice", "int", m, 1, findregion(1,0), 3); + mt_register(mt_new_va("dragon_growl", "dragon:unit", "number:int", "target:region", "choice:int", 0)); random_growl(m, findregion(1, 0), 3); msg = test_get_last_message(r->msgs); - assert_message(tc, msg, msginfo); + assert_message(tc, msg, "dragon_growl", 4, m, 1, findregion(1,0), 3); test_cleanup(); } diff --git a/src/tests.c b/src/tests.c index a0d54a4cc..c50065e9c 100644 --- a/src/tests.c +++ b/src/tests.c @@ -390,6 +390,29 @@ void test_clear_messages(faction *f) { } } +void assert_message(CuTest * tc, message *msg, char *name, int numpar, ...) +{ + va_list vargs; + const message_type *mtype = msg->type; + assert(mtype); + int i; + + va_start(vargs, numpar); + + CuAssertStrEquals(tc, name, mtype->name); + CuAssertIntEquals(tc, numpar, mtype->nparameters); + for (i = 0; i != mtype->nparameters; ++i) { + variant value = va_arg(vargs, variant); + if (mtype->types[i]->vtype == VAR_VOIDPTR) { + CuAssertPtrEquals(tc, value.v, msg->parameters[i].v); + } else if (mtype->types[i]->vtype == VAR_INT) { + CuAssertIntEquals(tc, value.i, msg->parameters[i].i); + } else { + assert(!"unknown variant type"); + } + } +} + void disabled_test(void *suite, void (*test)(CuTest *), const char *name) { (void)test; fprintf(stderr, "%s: SKIP\n", name); diff --git a/src/tests.h b/src/tests.h index 9836c42ab..fafb1755d 100644 --- a/src/tests.h +++ b/src/tests.h @@ -54,6 +54,7 @@ extern "C" { struct message * test_find_messagetype(struct message_list *msgs, const char *name); struct message * test_get_last_message(struct message_list *mlist); void test_clear_messages(struct faction *f); + void assert_message(struct CuTest * tc, struct message *msg, char *name, int numpar, ...); void disabled_test(void *suite, void (*)(struct CuTest *), const char *name);