From 79d2c76c3f3bdb3984e7613598f4570323177d70 Mon Sep 17 00:00:00 2001 From: Steffen Mecke Date: Sun, 18 Jan 2015 17:40:54 +0100 Subject: [PATCH] refining spy tests --- src/CMakeLists.txt | 1 + src/kernel/CMakeLists.txt | 1 - src/kernel/spy.test.c | 87 ----------------------- src/spy.c | 2 +- src/spy.test.c | 145 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 89 deletions(-) delete mode 100644 src/kernel/spy.test.c create mode 100644 src/spy.test.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e972d962f..44d781db3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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} diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index e7170e7b2..bc2c20418 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -22,7 +22,6 @@ spellbook.test.c curse.test.c jsonconf.test.c messages.test.c -spy.test.c ) SET(_FILES diff --git a/src/kernel/spy.test.c b/src/kernel/spy.test.c deleted file mode 100644 index 07a414220..000000000 --- a/src/kernel/spy.test.c +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include "types.h" -#include "spy.h" -#include "magic.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -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; ifaction->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; -} diff --git a/src/spy.c b/src/spy.c index 67b3cd48a..a7e481954 100644 --- a/src/spy.c +++ b/src/spy.c @@ -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)); } } diff --git a/src/spy.test.c b/src/spy.test.c new file mode 100644 index 000000000..20bfd949e --- /dev/null +++ b/src/spy.test.c @@ -0,0 +1,145 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "spy.h" + +#include + +#include + +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; ir = 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; +}