server/src/spy.test.c

185 lines
5.1 KiB
C
Raw Normal View History

2015-01-18 17:40:54 +01:00
#include <platform.h>
#include <magic.h>
#include <kernel/config.h>
2015-01-18 17:40:54 +01:00
#include <kernel/types.h>
#include <kernel/region.h>
#include <kernel/unit.h>
#include <kernel/faction.h>
2015-07-07 13:38:14 +02:00
#include <kernel/ship.h>
#include <kernel/order.h>
2015-01-18 17:40:54 +01:00
#include <kernel/item.h>
#include <kernel/messages.h>
#include <util/attrib.h>
#include <util/language.h>
2015-01-18 17:40:54 +01:00
#include <util/message.h>
#include <util/crmessage.h>
#include <tests.h>
#include <attributes/otherfaction.h>
#include "spy.h"
#include <assert.h>
2015-01-18 17:40:54 +01:00
#include <stdio.h>
#include <CuTest.h>
typedef struct {
region *r;
unit *spy;
unit *victim;
} spy_fixture;
static void setup_spy(spy_fixture *fix) {
test_setup();
2015-01-18 17:40:54 +01:00
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 void test_simple_spy_message(CuTest *tc) {
spy_fixture fix;
setup_spy(&fix);
spy_message(0, fix.spy, fix.victim);
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport"));
2015-01-18 17:40:54 +01:00
test_cleanup();
}
static void set_factionstealth(unit *u, faction *f) {
attrib *a = a_find(u->attribs, &at_otherfaction);
if (!a)
2015-07-07 09:44:24 +02:00
a = a_add(&u->attribs, make_otherfaction(f));
2015-01-18 17:40:54 +01:00
else
2015-07-07 09:44:24 +02:00
a->data.v = f;
2015-01-18 17:40:54 +01:00
}
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);
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport"));
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_mage"));
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_skills"));
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_faction"));
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_items"));
2015-01-18 17:40:54 +01:00
test_cleanup();
}
2015-07-07 13:38:14 +02:00
static void setup_sabotage(void) {
struct locale *lang;
2015-01-18 17:40:54 +01:00
test_setup();
lang = get_or_create_locale("de");
locale_setstring(lang, parameters[P_SHIP], "SCHIFF");
locale_setstring(lang, parameters[P_ANY], "ALLE");
init_parameters(lang);
init_locales();
2015-07-07 13:38:14 +02:00
}
static void test_sabotage_self(CuTest *tc) {
unit *u;
region *r;
order *ord;
2015-07-07 13:38:14 +02:00
setup_sabotage();
r = test_create_region(0, 0, 0);
assert(r);
u = test_create_unit(test_create_faction(NULL), r);
2015-07-07 09:44:24 +02:00
assert(u && u->faction && u->region == r);
u->ship = test_create_ship(r, test_create_shiptype("boat"));
assert(u->ship);
2015-07-07 13:38:14 +02:00
ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF");
assert(ord);
CuAssertIntEquals(tc, 0, sabotage_cmd(u, ord));
CuAssertPtrEquals(tc, 0, r->ships);
free_order(ord);
test_cleanup();
}
2015-01-18 17:40:54 +01:00
2015-07-07 13:38:14 +02:00
static void test_sabotage_other_fail(CuTest *tc) {
unit *u, *u2;
region *r;
order *ord;
message *msg;
2015-07-07 13:38:14 +02:00
setup_sabotage();
r = test_create_region(0, 0, 0);
2015-07-07 13:38:14 +02:00
assert(r);
u = test_create_unit(test_create_faction(NULL), r);
u2 = test_create_unit(test_create_faction(NULL), r);
assert(u && u2);
u2->ship = test_create_ship(r, test_create_shiptype("boat"));
assert(u2->ship);
u->ship = u2->ship;
ship_update_owner(u->ship);
assert(ship_owner(u->ship) == u);
ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF");
assert(ord);
CuAssertIntEquals(tc, 0, sabotage_cmd(u2, ord));
msg = test_get_last_message(u2->faction->msgs);
CuAssertStrEquals(tc, "destroy_ship_1", test_get_messagetype(msg));
msg = test_get_last_message(u->faction->msgs);
CuAssertStrEquals(tc, "destroy_ship_3", test_get_messagetype(msg));
2015-07-07 13:38:14 +02:00
CuAssertPtrNotNull(tc, r->ships);
free_order(ord);
2015-07-07 13:38:14 +02:00
test_cleanup();
}
static void test_sabotage_other_success(CuTest *tc) {
unit *u, *u2;
region *r;
order *ord;
setup_sabotage();
r = test_create_region(0, 0, 0);
2015-07-07 13:38:14 +02:00
assert(r);
u = test_create_unit(test_create_faction(NULL), r);
u2 = test_create_unit(test_create_faction(NULL), r);
assert(u && u2);
u2->ship = test_create_ship(r, test_create_shiptype("boat"));
assert(u2->ship);
u->ship = u2->ship;
ship_update_owner(u->ship);
assert(ship_owner(u->ship) == u);
ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF");
assert(ord);
set_level(u2, SK_SPY, 1);
CuAssertIntEquals(tc, 0, sabotage_cmd(u2, ord));
CuAssertPtrEquals(tc, 0, r->ships);
free_order(ord);
2015-07-07 13:38:14 +02:00
test_cleanup();
}
2015-01-18 17:40:54 +01:00
CuSuite *get_spy_suite(void)
{
2015-07-07 09:44:24 +02:00
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_simple_spy_message);
SUITE_ADD_TEST(suite, test_all_spy_message);
SUITE_ADD_TEST(suite, test_sabotage_self);
2015-07-07 13:38:14 +02:00
SUITE_ADD_TEST(suite, test_sabotage_other_fail);
SUITE_ADD_TEST(suite, test_sabotage_other_success);
2015-07-07 09:44:24 +02:00
return suite;
2015-01-18 17:40:54 +01:00
}