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
curse.test.c
jsonconf.test.c
messages.test.c
)
SET(_FILES

View File

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

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern "C" {
#endif
#include <kernel/types.h>
#include <util/message.h>
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, spell);
RUN_TESTS(suite, ally);
RUN_TESTS(suite, messages);
/* gamecode */
RUN_TESTS(suite, battle);
RUN_TESTS(suite, economy);

View File

@ -146,7 +146,7 @@ message *msg_create(const struct message_type *mtype, variant args[])
return NULL;
}
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;
for (i = 0; i != mtype->nparameters; ++i) {
msg->parameters[i] = copy_arg(mtype->types[i], args[i]);