add some simple tests for messages, prior to any refactorings.

This commit is contained in:
Enno Rehling 2015-01-15 16:40:19 +01:00
parent 9f9043e4dd
commit 490782e750
6 changed files with 53 additions and 7 deletions

View File

@ -21,6 +21,7 @@ race.test.c
spellbook.test.c spellbook.test.c
curse.test.c curse.test.c
jsonconf.test.c jsonconf.test.c
messages.test.c
) )
SET(_FILES SET(_FILES

View File

@ -17,7 +17,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/ **/
#include <platform.h> #include <platform.h>
#include <kernel/config.h>
#include "messages.h" #include "messages.h"
/* kernel includes */ /* kernel includes */
@ -140,7 +139,7 @@ struct message *msg_feedback(const struct unit *u, struct order *ord,
message *msg_message(const char *name, const char *sig, ...) message *msg_message(const char *name, const char *sig, ...)
/* msg_message("oops_error", "unit region command", u, r, cmd) */ /* msg_message("oops_error", "unit region command", u, r, cmd) */
{ {
va_list marker; va_list vargs;
const message_type *mtype = mt_find(name); const message_type *mtype = mt_find(name);
char paramname[64]; char paramname[64];
const char *ic = sig; const char *ic = sig;
@ -155,7 +154,7 @@ message *msg_message(const char *name, const char *sig, ...)
return NULL; return NULL;
} }
va_start(marker, sig); va_start(vargs, sig);
while (*ic && !isalnum(*ic)) while (*ic && !isalnum(*ic))
ic++; ic++;
while (*ic) { while (*ic) {
@ -172,9 +171,9 @@ message *msg_message(const char *name, const char *sig, ...)
} }
if (i != mtype->nparameters) { if (i != mtype->nparameters) {
if (mtype->types[i]->vtype == VAR_VOIDPTR) { if (mtype->types[i]->vtype == VAR_VOIDPTR) {
args[i].v = va_arg(marker, void *); args[i].v = va_arg(vargs, void *);
} else if (mtype->types[i]->vtype == VAR_INT) { } else if (mtype->types[i]->vtype == VAR_INT) {
args[i].i = va_arg(marker, int); args[i].i = va_arg(vargs, int);
} else { } else {
assert(!"unknown variant type"); assert(!"unknown variant type");
} }
@ -185,7 +184,7 @@ message *msg_message(const char *name, const char *sig, ...)
while (*ic && !isalnum(*ic)) while (*ic && !isalnum(*ic))
ic++; ic++;
} }
va_end(marker); va_end(vargs);
return msg_create(mtype, args); return msg_create(mtype, args);
} }

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern "C" { extern "C" {
#endif #endif
#include <kernel/types.h>
#include <util/message.h> #include <util/message.h>
struct faction; struct faction;

View File

@ -0,0 +1,44 @@
#include <platform.h>
#include "messages.h"
#include <CuTest.h>
#include <tests.h>
void test_missing_message(CuTest *tc) {
message *msg;
msg = msg_message("unknown", "unit", NULL);
CuAssertPtrNotNull(tc, msg);
CuAssertPtrNotNull(tc, msg->type);
CuAssertStrEquals(tc, msg->type->name, "missing_message");
msg_release(msg);
}
void test_message(CuTest *tc) {
message *msg;
// const char * args[] = { }
message_type *mtype = mt_new("custom", NULL);
mt_register(mtype);
CuAssertPtrEquals(tc, mtype, (void *)mt_find("custom"));
CuAssertIntEquals(tc, 0, mtype->nparameters);
CuAssertPtrEquals(tc, NULL, (void *)mtype->pnames);
CuAssertPtrEquals(tc, NULL, (void *)mtype->types);
msg = msg_message("custom", "");
CuAssertPtrNotNull(tc, msg);
CuAssertIntEquals(tc, 1, msg->refcount);
CuAssertPtrEquals(tc, NULL, msg->parameters);
CuAssertPtrEquals(tc, mtype, (void *)msg->type);
CuAssertPtrEquals(tc, msg, msg_addref(msg));
CuAssertIntEquals(tc, 2, msg->refcount);
msg_release(msg);
CuAssertIntEquals(tc, 1, msg->refcount);
msg_release(msg);
test_cleanup();
}
CuSuite *get_messages_suite(void) {
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_missing_message);
SUITE_ADD_TEST(suite, test_message);
return suite;
}

View File

@ -71,6 +71,7 @@ int RunAllTests(void)
RUN_TESTS(suite, building); RUN_TESTS(suite, building);
RUN_TESTS(suite, spell); RUN_TESTS(suite, spell);
RUN_TESTS(suite, ally); RUN_TESTS(suite, ally);
RUN_TESTS(suite, messages);
/* gamecode */ /* gamecode */
RUN_TESTS(suite, battle); RUN_TESTS(suite, battle);
RUN_TESTS(suite, economy); RUN_TESTS(suite, economy);

View File

@ -146,7 +146,7 @@ message *msg_create(const struct message_type *mtype, variant args[])
return NULL; return NULL;
} }
msg->type = mtype; msg->type = mtype;
msg->parameters = (variant *) calloc(mtype->nparameters, sizeof(variant)); msg->parameters = (variant *)mtype->nparameters ? calloc(mtype->nparameters, sizeof(variant)) : NULL;
msg->refcount = 1; msg->refcount = 1;
for (i = 0; i != mtype->nparameters; ++i) { for (i = 0; i != mtype->nparameters; ++i) {
msg->parameters[i] = copy_arg(mtype->types[i], args[i]); msg->parameters[i] = copy_arg(mtype->types[i], args[i]);