trying to solve spy message bug #1604

Conflicts:
	src/kernel/CMakeLists.txt
This commit is contained in:
Steffen Mecke 2015-01-13 13:34:26 +01:00
parent fe3d96517e
commit 2cc8c1f871
5 changed files with 105 additions and 6 deletions

View File

@ -3466,6 +3466,7 @@
</message>
<message name="spyreport_mage" section="events">
<type>
<arg name="spy" type="unit"/>
<arg name="target" type="unit"/>
<arg name="type" type="string"/>
</type>
@ -3474,6 +3475,7 @@
</message>
<message name="spyreport_skills" section="events">
<type>
<arg name="spy" type="unit"/>
<arg name="target" type="unit"/>
<arg name="skills" type="string"/>
</type>
@ -3482,6 +3484,7 @@
</message>
<message name="spyreport_items" section="events">
<type>
<arg name="spy" type="unit"/>
<arg name="target" type="unit"/>
<arg name="items" type="items"/>
</type>

View File

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

94
src/kernel/spy.test.c Normal file
View File

@ -0,0 +1,94 @@
#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, p;
const message_type *expected[3];
variant v;
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);
/* I'm not sure how to test for correct number and maybe even type of parameters */
for (p = 0; p != msg->type->nparameters; ++p) {
v = msg->parameters[p];
if (v.v || !v.v)
v = msg->parameters[p];
}
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

@ -58,15 +58,15 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* Spionage des Spions */
void spy_message(int spy, const unit * u, const unit * target)
{
const char *str = report_kampfstatus(target, u->faction->locale);
/* const char *str = report_kampfstatus(target, u->faction->locale);*/
ADDMSG(&u->faction->msgs, msg_message("spyreport", "spy target status", u,
target, str));
target));
if (spy > 20) {
sc_mage *mage = get_mage(target);
/* for mages, spells and magic school */
if (mage) {
ADDMSG(&u->faction->msgs, msg_message("spyreport_mage", "target type",
ADDMSG(&u->faction->msgs, msg_message("spyreport_mage", "spy target type", u,
target, magic_school[mage->magietyp]));
}
}
@ -75,7 +75,7 @@ void spy_message(int spy, const unit * u, const unit * target)
if (fv && fv != target->faction) {
/* true faction */
ADDMSG(&u->faction->msgs, msg_message("spyreport_faction",
"target faction", target, target->faction));
"spy target faction", u, target, target->faction));
add_seen_faction(u->faction, target->faction);
}
}
@ -103,12 +103,12 @@ void spy_message(int spy, const unit * u, const unit * target)
}
}
if (found) {
ADDMSG(&u->faction->msgs, msg_message("spyreport_skills", "target skills",
ADDMSG(&u->faction->msgs, msg_message("spyreport_skills", "spy target skills", u,
target, buf));
}
if (target->items) {
ADDMSG(&u->faction->msgs, msg_message("spyreport_items", "target items",
ADDMSG(&u->faction->msgs, msg_message("spyreport_items", "target items", u,
target, target->items));
}
}

View File

@ -84,6 +84,7 @@ int RunAllTests(void)
RUN_TESTS(suite, upkeep);
RUN_TESTS(suite, vortex);
RUN_TESTS(suite, wormhole);
RUN_TESTS(suite, spy);
printf("\ntest summary: %d tests, %d failed\n", suite->count, suite->failCount);
log_flags = flags;