refining spy tests

This commit is contained in:
Steffen Mecke 2015-01-18 17:40:54 +01:00
parent d4aa6e834e
commit 79d2c76c3f
5 changed files with 147 additions and 89 deletions

View File

@ -178,6 +178,7 @@ set(TESTS_SRC
move.test.c
skill.test.c
upkeep.test.c
spy.test.c
${ATTRIBUTES_TESTS}
${UTIL_TESTS}
${KERNEL_TESTS}

View File

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

View File

@ -1,87 +0,0 @@
#include <platform.h>
#include "types.h"
#include "spy.h"
#include "magic.h"
#include <kernel/region.h>
#include <kernel/unit.h>
#include <kernel/faction.h>
#include <kernel/messages.h>
#include <util/attrib.h>
#include <util/message.h>
#include <util/crmessage.h>
#include <tests.h>
#include <stdio.h>
#include <CuTest.h>
typedef struct {
region *r;
unit *spy;
unit *victim;
} spy_fixture;
static void setup_spy(spy_fixture *fix) {
test_cleanup();
fix->r = test_create_region(0, 0, NULL);
fix->spy = test_create_unit(test_create_faction(NULL), fix->r);
fix->victim = test_create_unit(test_create_faction(NULL), fix->r);
}
static 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));
}
static void test_spy_message(CuTest *tc) {
spy_fixture fix;
struct mlist *msglist;
struct message *msg;
int m;
const message_type *expected[3];
setup_spy(&fix);
enable_skill(SK_MAGIC, true);
set_level(fix.victim, SK_MINING, 2);
set_level(fix.victim, SK_MAGIC, 2);
create_mage(fix.victim, M_DRAIG);
expected[0] = register_msg("spyreport", 3, "spy:unit", "target:unit", "status:string");
expected[1] = register_msg("spyreport_mage", 3, "spy:unit", "target:unit", "type:string");
expected[2] = register_msg("spyreport_skills", 3, "spy:unit", "target:unit", "skills:string");
register_msg("spyreport_faction", 3, "spy:unit", "target:unit", "faction:faction");
register_msg("spyreport_items", 3, "spy:unit", "target:unit", "items:items");
spy_message(99, fix.spy, fix.victim);
msglist = fix.spy->faction->msgs->begin;
m = 0;
while (msglist) {
msg = msglist->msg;
CuAssertStrEquals(tc, expected[m]->name, msg->type->name);
msglist = msglist->next;
++m;
}
CuAssertIntEquals(tc, 3, m);
test_cleanup();
}
CuSuite *get_spy_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_spy_message);
return suite;
}

View File

@ -108,7 +108,7 @@ void spy_message(int spy, const unit * u, const unit * target)
}
if (target->items) {
ADDMSG(&u->faction->msgs, msg_message("spyreport_items", "target items", u,
ADDMSG(&u->faction->msgs, msg_message("spyreport_items", "spy target items", u,
target, target->items));
}
}

145
src/spy.test.c Normal file
View File

@ -0,0 +1,145 @@
#include <platform.h>
#include <magic.h>
#include <kernel/types.h>
#include <kernel/region.h>
#include <kernel/unit.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/messages.h>
#include <util/attrib.h>
#include <util/message.h>
#include <util/crmessage.h>
#include <tests.h>
#include <attributes/otherfaction.h>
#include "spy.h"
#include <stdio.h>
#include <CuTest.h>
typedef enum {
M_BASE,
M_MAGE,
M_SKILLS,
M_FACTION,
M_ITEMS,
NUM_TYPES
} m_type;
typedef struct {
region *r;
unit *spy;
unit *victim;
const message_type *msg_types[NUM_TYPES];
} spy_fixture;
static 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));
}
static void setup_spy(spy_fixture *fix) {
test_cleanup();
fix->r = test_create_region(0, 0, NULL);
fix->spy = 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 assert_messages(CuTest * tc, struct mlist *msglist, const message_type **types, int num_msgs, ...) {
va_list args;
int m;
struct message *msg;
va_start(args, num_msgs);
m = 0;
while (msglist) {
int argc = va_arg(args, int);
msg = msglist->msg;
CuAssertStrEquals(tc, types[argc]->name, msg->type->name);
msglist = msglist->next;
++m;
}
CuAssertIntEquals(tc, num_msgs, m);
va_end(args);
}
static void test_simple_spy_message(CuTest *tc) {
spy_fixture fix;
setup_spy(&fix);
spy_message(0, fix.spy, fix.victim);
assert_messages(tc, fix.spy->faction->msgs->begin, fix.msg_types, 1, M_BASE);
test_cleanup();
}
static void set_factionstealth(unit *u, faction *f) {
attrib *a = a_find(u->attribs, &at_otherfaction);
if (!a)
a = a_add(&u->attribs, make_otherfaction(f));
else
a->data.v = f;
}
static void test_all_spy_message(CuTest *tc) {
spy_fixture fix;
setup_spy(&fix);
enable_skill(SK_MAGIC, true);
set_level(fix.victim, SK_MINING, 2);
set_level(fix.victim, SK_MAGIC, 2);
create_mage(fix.victim, M_DRAIG);
set_factionstealth(fix.victim, fix.spy->faction);
item_type *itype;
itype = it_get_or_create(rt_get_or_create("sword"));
new_weapontype(itype, 0, 0.0, NULL, 0, 0, 0, SK_MELEE, 2);
i_change(&fix.victim->items, itype, 1);
spy_message(99, fix.spy, fix.victim);
assert_messages(tc, fix.spy->faction->msgs->begin, fix.msg_types, 5,
M_BASE,
M_MAGE,
M_FACTION,
M_SKILLS,
M_ITEMS);
test_cleanup();
}
CuSuite *get_spy_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_simple_spy_message);
SUITE_ADD_TEST(suite, test_all_spy_message);
return suite;
}