forked from github/server
refactoring: rewrite tests to use test_find_messagetype instead of assert_messages.
tests: add cleanup of message_types to reduce global state.
This commit is contained in:
parent
1806030baa
commit
7e64f3177d
|
@ -142,10 +142,8 @@ static struct unit *create_recruiter(void) {
|
||||||
static void test_heroes_dont_recruit(CuTest * tc) {
|
static void test_heroes_dont_recruit(CuTest * tc) {
|
||||||
unit *u;
|
unit *u;
|
||||||
order *ord;
|
order *ord;
|
||||||
const message_type *msg_types[1];
|
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
msg_types[0] = register_msg("error_herorecruit", 3, "unit:unit", "region:region", "command:order");
|
|
||||||
|
|
||||||
u = create_recruiter();
|
u = create_recruiter();
|
||||||
fset(u, UFL_HERO);
|
fset(u, UFL_HERO);
|
||||||
|
@ -155,7 +153,7 @@ static void test_heroes_dont_recruit(CuTest * tc) {
|
||||||
economics(u->region);
|
economics(u->region);
|
||||||
|
|
||||||
CuAssertIntEquals(tc, 1, u->number);
|
CuAssertIntEquals(tc, 1, u->number);
|
||||||
assert_messages(tc, u->faction->msgs->begin, msg_types, 1, true, 0);
|
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error_herorecruit"));
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1676,6 +1676,7 @@ void attrib_init(void)
|
||||||
void kernel_init(void)
|
void kernel_init(void)
|
||||||
{
|
{
|
||||||
register_reports();
|
register_reports();
|
||||||
|
mt_clear();
|
||||||
if (!mt_find("missing_message")) {
|
if (!mt_find("missing_message")) {
|
||||||
mt_register(mt_new_va("missing_message", "name:string", 0));
|
mt_register(mt_new_va("missing_message", "name:string", 0));
|
||||||
mt_register(mt_new_va("missing_feedback", "unit:unit", "region:region", "command:order", "name:string", 0));
|
mt_register(mt_new_va("missing_feedback", "unit:unit", "region:region", "command:order", "name:string", 0));
|
||||||
|
|
|
@ -17,6 +17,7 @@ void test_message(CuTest *tc) {
|
||||||
message *msg;
|
message *msg;
|
||||||
message_type *mtype = mt_new("custom", NULL);
|
message_type *mtype = mt_new("custom", NULL);
|
||||||
|
|
||||||
|
test_cleanup();
|
||||||
mt_register(mtype);
|
mt_register(mtype);
|
||||||
CuAssertPtrEquals(tc, mtype, (void *)mt_find("custom"));
|
CuAssertPtrEquals(tc, mtype, (void *)mt_find("custom"));
|
||||||
CuAssertIntEquals(tc, 0, mtype->nparameters);
|
CuAssertIntEquals(tc, 0, mtype->nparameters);
|
||||||
|
@ -41,6 +42,7 @@ static void test_merge_split(CuTest *tc) {
|
||||||
struct mlist **split;
|
struct mlist **split;
|
||||||
message_type *mtype = mt_new("custom", NULL);
|
message_type *mtype = mt_new("custom", NULL);
|
||||||
|
|
||||||
|
test_cleanup();
|
||||||
mt_register(mtype);
|
mt_register(mtype);
|
||||||
add_message(&mlist, msg_message(mtype->name, ""));
|
add_message(&mlist, msg_message(mtype->name, ""));
|
||||||
add_message(&append, msg_message(mtype->name, ""));
|
add_message(&append, msg_message(mtype->name, ""));
|
||||||
|
@ -55,6 +57,7 @@ static void test_merge_split(CuTest *tc) {
|
||||||
CuAssertPtrEquals(tc, append->begin, mlist->begin->next);
|
CuAssertPtrEquals(tc, append->begin, mlist->begin->next);
|
||||||
split_messages(mlist, split);
|
split_messages(mlist, split);
|
||||||
CuAssertPtrEquals(tc, 0, mlist->begin->next);
|
CuAssertPtrEquals(tc, 0, mlist->begin->next);
|
||||||
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
CuSuite *get_messages_suite(void) {
|
CuSuite *get_messages_suite(void) {
|
||||||
|
|
|
@ -745,14 +745,10 @@ static void test_peasant_luck_effect(CuTest *tc) {
|
||||||
|
|
||||||
static void test_luck_message(CuTest *tc) {
|
static void test_luck_message(CuTest *tc) {
|
||||||
region* r;
|
region* r;
|
||||||
const message_type *msg_types[1];
|
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
r = test_create_region(0, 0, NULL);
|
r = test_create_region(0, 0, NULL);
|
||||||
rsetpeasants(r, 1);
|
rsetpeasants(r, 1);
|
||||||
|
|
||||||
msg_types[0] = register_msg("peasantluck_success", 1, "births:int");
|
|
||||||
|
|
||||||
demographics();
|
demographics();
|
||||||
|
|
||||||
CuAssertPtrEquals_Msg(tc, "unexpected message", (void *)NULL, r->msgs);
|
CuAssertPtrEquals_Msg(tc, "unexpected message", (void *)NULL, r->msgs);
|
||||||
|
@ -764,7 +760,7 @@ static void test_luck_message(CuTest *tc) {
|
||||||
|
|
||||||
demographics();
|
demographics();
|
||||||
|
|
||||||
assert_messages(tc, r->msgs->begin, msg_types, 1, true, 0);
|
CuAssertPtrNotNull(tc, test_find_messagetype(r->msgs, "peasantluck_success"));
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,20 +24,10 @@
|
||||||
|
|
||||||
#include <CuTest.h>
|
#include <CuTest.h>
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
M_BASE,
|
|
||||||
M_MAGE,
|
|
||||||
M_SKILLS,
|
|
||||||
M_FACTION,
|
|
||||||
M_ITEMS,
|
|
||||||
NUM_TYPES
|
|
||||||
} m_type;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
region *r;
|
region *r;
|
||||||
unit *spy;
|
unit *spy;
|
||||||
unit *victim;
|
unit *victim;
|
||||||
const message_type *msg_types[NUM_TYPES];
|
|
||||||
} spy_fixture;
|
} spy_fixture;
|
||||||
|
|
||||||
static void setup_spy(spy_fixture *fix) {
|
static void setup_spy(spy_fixture *fix) {
|
||||||
|
@ -45,12 +35,6 @@ static void setup_spy(spy_fixture *fix) {
|
||||||
fix->r = test_create_region(0, 0, NULL);
|
fix->r = test_create_region(0, 0, NULL);
|
||||||
fix->spy = test_create_unit(test_create_faction(NULL), fix->r);
|
fix->spy = test_create_unit(test_create_faction(NULL), fix->r);
|
||||||
fix->victim = test_create_unit(test_create_faction(NULL), fix->r);
|
fix->victim = test_create_unit(test_create_faction(NULL), fix->r);
|
||||||
fix->msg_types[M_BASE] = register_msg("spyreport", 3, "spy:unit", "target:unit", "status:string");
|
|
||||||
fix->msg_types[M_MAGE] = register_msg("spyreport_mage", 3, "spy:unit", "target:unit", "type:string");
|
|
||||||
fix->msg_types[M_SKILLS] = register_msg("spyreport_skills", 3, "spy:unit", "target:unit", "skills:string");
|
|
||||||
fix->msg_types[M_FACTION] = register_msg("spyreport_faction", 3, "spy:unit", "target:unit", "faction:faction");
|
|
||||||
fix->msg_types[M_ITEMS] = register_msg("spyreport_items", 3, "spy:unit", "target:unit", "items:items");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_simple_spy_message(CuTest *tc) {
|
static void test_simple_spy_message(CuTest *tc) {
|
||||||
|
@ -60,8 +44,7 @@ static void test_simple_spy_message(CuTest *tc) {
|
||||||
|
|
||||||
spy_message(0, fix.spy, fix.victim);
|
spy_message(0, fix.spy, fix.victim);
|
||||||
|
|
||||||
assert_messages(tc, fix.spy->faction->msgs->begin, fix.msg_types, 1, true, M_BASE);
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport"));
|
||||||
|
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
@ -92,12 +75,11 @@ static void test_all_spy_message(CuTest *tc) {
|
||||||
|
|
||||||
spy_message(99, fix.spy, fix.victim);
|
spy_message(99, fix.spy, fix.victim);
|
||||||
|
|
||||||
assert_messages(tc, fix.spy->faction->msgs->begin, fix.msg_types, 5, true,
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport"));
|
||||||
M_BASE,
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_mage"));
|
||||||
M_MAGE,
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_skills"));
|
||||||
M_FACTION,
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_faction"));
|
||||||
M_SKILLS,
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_items"));
|
||||||
M_ITEMS);
|
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
58
src/tests.c
58
src/tests.c
|
@ -80,6 +80,11 @@ void test_cleanup(void)
|
||||||
free_races();
|
free_races();
|
||||||
free_spellbooks();
|
free_spellbooks();
|
||||||
free_gamedata();
|
free_gamedata();
|
||||||
|
mt_clear();
|
||||||
|
if (!mt_find("missing_message")) {
|
||||||
|
mt_register(mt_new_va("missing_message", "name:string", 0));
|
||||||
|
mt_register(mt_new_va("missing_feedback", "unit:unit", "region:region", "command:order", "name:string", 0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
terrain_type *
|
terrain_type *
|
||||||
|
@ -255,59 +260,6 @@ struct message * test_find_messagetype(struct message_list *msgs, const char *na
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const message_type *register_msg(const char *type, int n_param, ...) {
|
|
||||||
char **argv;
|
|
||||||
va_list args;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
va_start(args, n_param);
|
|
||||||
|
|
||||||
argv = malloc(sizeof(char *) * (n_param + 1));
|
|
||||||
for (i = 0; i < n_param; ++i) {
|
|
||||||
argv[i] = va_arg(args, char *);
|
|
||||||
}
|
|
||||||
argv[n_param] = 0;
|
|
||||||
va_end(args);
|
|
||||||
return mt_register(mt_new(type, (const char **)argv));
|
|
||||||
}
|
|
||||||
|
|
||||||
void assert_messages(struct CuTest * tc, struct mlist *msglist, const message_type **types,
|
|
||||||
int num_msgs, bool exact_match, ...) {
|
|
||||||
char buf[100];
|
|
||||||
va_list args;
|
|
||||||
int found = 0, argc = -1;
|
|
||||||
struct message *msg;
|
|
||||||
bool match = true;
|
|
||||||
|
|
||||||
va_start(args, exact_match);
|
|
||||||
|
|
||||||
while (msglist) {
|
|
||||||
msg = msglist->msg;
|
|
||||||
if (found >= num_msgs) {
|
|
||||||
if (exact_match) {
|
|
||||||
slprintf(buf, sizeof(buf), "too many messages: %s", msg->type->name);
|
|
||||||
CuFail(tc, buf);
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (exact_match || match)
|
|
||||||
argc = va_arg(args, int);
|
|
||||||
|
|
||||||
match = strcmp(types[argc]->name, msg->type->name) == 0;
|
|
||||||
if (match)
|
|
||||||
++found;
|
|
||||||
else if (exact_match)
|
|
||||||
CuAssertStrEquals(tc, types[argc]->name, msg->type->name);
|
|
||||||
|
|
||||||
msglist = msglist->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
CuAssertIntEquals_Msg(tc, "not enough messages", num_msgs, found);
|
|
||||||
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
void disabled_test(void *suite, void (*test)(CuTest *), const char *name) {
|
void disabled_test(void *suite, void (*test)(CuTest *), const char *name) {
|
||||||
(void)test;
|
(void)test;
|
||||||
fprintf(stderr, "%s: SKIP\n", name);
|
fprintf(stderr, "%s: SKIP\n", name);
|
||||||
|
|
|
@ -44,10 +44,6 @@ extern "C" {
|
||||||
struct message * test_find_messagetype(struct message_list *msgs, const char *name);
|
struct message * test_find_messagetype(struct message_list *msgs, const char *name);
|
||||||
struct message * test_get_last_message(struct message_list *mlist);
|
struct message * test_get_last_message(struct message_list *mlist);
|
||||||
|
|
||||||
const struct message_type *register_msg(const char *type, int n_param, ...);
|
|
||||||
void assert_messages(struct CuTest * tc, struct mlist *msglist, const struct message_type **types,
|
|
||||||
int num_msgs, bool exact_match, ...);
|
|
||||||
|
|
||||||
void disabled_test(void *suite, void (*)(struct CuTest *), const char *name);
|
void disabled_test(void *suite, void (*)(struct CuTest *), const char *name);
|
||||||
|
|
||||||
#define DISABLE_TEST(SUITE, TEST) disabled_test(SUITE, TEST, #TEST)
|
#define DISABLE_TEST(SUITE, TEST) disabled_test(SUITE, TEST, #TEST)
|
||||||
|
|
|
@ -32,6 +32,32 @@ const char *mt_name(const message_type * mtype)
|
||||||
return mtype->name;
|
return mtype->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
arg_type *argtypes = NULL;
|
||||||
|
|
||||||
|
void
|
||||||
|
register_argtype(const char *name, void(*free_arg) (variant),
|
||||||
|
variant(*copy_arg) (variant), variant_type type)
|
||||||
|
{
|
||||||
|
arg_type *atype = (arg_type *)malloc(sizeof(arg_type));
|
||||||
|
atype->name = name;
|
||||||
|
atype->next = argtypes;
|
||||||
|
atype->release = free_arg;
|
||||||
|
atype->copy = copy_arg;
|
||||||
|
atype->vtype = type;
|
||||||
|
argtypes = atype;
|
||||||
|
}
|
||||||
|
|
||||||
|
static arg_type *find_argtype(const char *name)
|
||||||
|
{
|
||||||
|
arg_type *atype = argtypes;
|
||||||
|
while (atype != NULL) {
|
||||||
|
if (strcmp(atype->name, name) == 0)
|
||||||
|
return atype;
|
||||||
|
atype = atype->next;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
message_type *mt_new(const char *name, const char *args[])
|
message_type *mt_new(const char *name, const char *args[])
|
||||||
{
|
{
|
||||||
int i, nparameters = 0;
|
int i, nparameters = 0;
|
||||||
|
@ -50,8 +76,8 @@ message_type *mt_new(const char *name, const char *args[])
|
||||||
mtype->name = _strdup(name);
|
mtype->name = _strdup(name);
|
||||||
mtype->nparameters = nparameters;
|
mtype->nparameters = nparameters;
|
||||||
if (nparameters > 0) {
|
if (nparameters > 0) {
|
||||||
mtype->pnames = (const char **)malloc(sizeof(char *) * nparameters);
|
mtype->pnames = (char **)malloc(sizeof(char *) * nparameters);
|
||||||
mtype->types = (const arg_type **)malloc(sizeof(arg_type *) * nparameters);
|
mtype->types = (arg_type **)malloc(sizeof(arg_type *) * nparameters);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
mtype->pnames = NULL;
|
mtype->pnames = NULL;
|
||||||
|
@ -96,32 +122,6 @@ message_type *mt_new_va(const char *name, ...)
|
||||||
return mt_new(name, args);
|
return mt_new(name, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
arg_type *argtypes = NULL;
|
|
||||||
|
|
||||||
void
|
|
||||||
register_argtype(const char *name, void(*free_arg) (variant),
|
|
||||||
variant(*copy_arg) (variant), variant_type type)
|
|
||||||
{
|
|
||||||
arg_type *atype = (arg_type *)malloc(sizeof(arg_type));
|
|
||||||
atype->name = name;
|
|
||||||
atype->next = argtypes;
|
|
||||||
atype->release = free_arg;
|
|
||||||
atype->copy = copy_arg;
|
|
||||||
atype->vtype = type;
|
|
||||||
argtypes = atype;
|
|
||||||
}
|
|
||||||
|
|
||||||
const arg_type *find_argtype(const char *name)
|
|
||||||
{
|
|
||||||
arg_type *atype = argtypes;
|
|
||||||
while (atype != NULL) {
|
|
||||||
if (strcmp(atype->name, name) == 0)
|
|
||||||
return atype;
|
|
||||||
atype = atype->next;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static variant copy_arg(const arg_type * atype, variant data)
|
static variant copy_arg(const arg_type * atype, variant data)
|
||||||
{
|
{
|
||||||
assert(atype != NULL);
|
assert(atype != NULL);
|
||||||
|
@ -161,6 +161,28 @@ message *msg_create(const struct message_type *mtype, variant args[])
|
||||||
#define MT_MAXHASH 1021
|
#define MT_MAXHASH 1021
|
||||||
static quicklist *messagetypes[MT_MAXHASH];
|
static quicklist *messagetypes[MT_MAXHASH];
|
||||||
|
|
||||||
|
static void mt_free(void *val) {
|
||||||
|
message_type *mtype = (message_type *)val;
|
||||||
|
int i;
|
||||||
|
for (i = 0; i != mtype->nparameters; ++i) {
|
||||||
|
free(mtype->pnames[i]);
|
||||||
|
}
|
||||||
|
free(mtype->pnames);
|
||||||
|
free(mtype->types);
|
||||||
|
free(mtype->name);
|
||||||
|
free(mtype);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mt_clear(void) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i != MT_MAXHASH; ++i) {
|
||||||
|
quicklist *ql = messagetypes[i];
|
||||||
|
ql_foreach(ql, mt_free);
|
||||||
|
ql_free(ql);
|
||||||
|
messagetypes[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const message_type *mt_find(const char *name)
|
const message_type *mt_find(const char *name)
|
||||||
{
|
{
|
||||||
unsigned int hash = hashstring(name) % MT_MAXHASH;
|
unsigned int hash = hashstring(name) % MT_MAXHASH;
|
||||||
|
|
|
@ -28,10 +28,10 @@ extern "C" {
|
||||||
|
|
||||||
typedef struct message_type {
|
typedef struct message_type {
|
||||||
unsigned int key;
|
unsigned int key;
|
||||||
const char *name;
|
char *name;
|
||||||
int nparameters;
|
int nparameters;
|
||||||
const char **pnames;
|
char **pnames;
|
||||||
const struct arg_type **types;
|
struct arg_type ** types;
|
||||||
} message_type;
|
} message_type;
|
||||||
|
|
||||||
typedef struct message {
|
typedef struct message {
|
||||||
|
@ -40,8 +40,9 @@ extern "C" {
|
||||||
int refcount;
|
int refcount;
|
||||||
} message;
|
} message;
|
||||||
|
|
||||||
extern struct message_type *mt_new(const char *name, const char **args);
|
void mt_clear(void);
|
||||||
extern struct message_type *mt_new_va(const char *name, ...);
|
struct message_type *mt_new(const char *name, const char **args);
|
||||||
|
struct message_type *mt_new_va(const char *name, ...);
|
||||||
/* mt_new("simple_sentence", "subject:string", "predicate:string",
|
/* mt_new("simple_sentence", "subject:string", "predicate:string",
|
||||||
* "object:string", "lang:locale", NULL); */
|
* "object:string", "lang:locale", NULL); */
|
||||||
|
|
||||||
|
@ -61,9 +62,8 @@ extern "C" {
|
||||||
|
|
||||||
extern void register_argtype(const char *name, void(*free_arg) (variant),
|
extern void register_argtype(const char *name, void(*free_arg) (variant),
|
||||||
variant(*copy_arg) (variant), variant_type);
|
variant(*copy_arg) (variant), variant_type);
|
||||||
extern const struct arg_type *find_argtype(const char *name);
|
|
||||||
|
|
||||||
extern void(*msg_log_create) (const struct message * msg);
|
void(*msg_log_create) (const struct message * msg);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue