forked from github/server
test coverage for can_steal.
add a default message for when no messages are loaded, so that msg_feedback/msg_message doesn't return NULL when XML has not been laoded. initialize some core stuff before running tests (there probably needs to be more).
This commit is contained in:
parent
072f927d5b
commit
f14264e3bb
|
@ -2747,6 +2747,24 @@ static int max_skill(region * r, faction * f, skill_t sk)
|
|||
return w;
|
||||
}
|
||||
|
||||
message * can_steal(const unit * u, struct order *ord) {
|
||||
plane *pl;
|
||||
|
||||
if (fval(u_race(u), RCF_NOSTEAL)) {
|
||||
return msg_feedback(u, ord, "race_nosteal", "race", u_race(u));
|
||||
}
|
||||
|
||||
if (fval(u->region->terrain, SEA_REGION) && u_race(u) != get_race(RC_AQUARIAN)) {
|
||||
return msg_feedback(u, ord, "error_onlandonly", "");
|
||||
}
|
||||
|
||||
pl = rplane(u->region);
|
||||
if (pl && fval(pl, PFL_NOATTACK)) {
|
||||
return msg_feedback(u, ord, "error270", "");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void steal_cmd(unit * u, struct order *ord, request ** stealorders)
|
||||
{
|
||||
const resource_type *rring = get_resourcetype(R_RING_OF_NIMBLEFINGER);
|
||||
|
@ -2756,27 +2774,15 @@ static void steal_cmd(unit * u, struct order *ord, request ** stealorders)
|
|||
unit *u2 = NULL;
|
||||
region *r = u->region;
|
||||
faction *f = NULL;
|
||||
plane *pl;
|
||||
message * msg;
|
||||
|
||||
assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH));
|
||||
|
||||
if (fval(u_race(u), RCF_NOSTEAL)) {
|
||||
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "race_nosteal", "race",
|
||||
u_race(u)));
|
||||
return;
|
||||
msg = can_steal(u, ord);
|
||||
if (msg) {
|
||||
ADDMSG(&u->faction->msgs, msg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fval(r->terrain, SEA_REGION) && u_race(u) != get_race(RC_AQUARIAN)) {
|
||||
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_onlandonly", ""));
|
||||
return;
|
||||
}
|
||||
|
||||
pl = rplane(r);
|
||||
if (pl && fval(pl, PFL_NOATTACK)) {
|
||||
cmistake(u, ord, 270, MSG_INCOME);
|
||||
return;
|
||||
}
|
||||
|
||||
init_tokens(ord);
|
||||
skip_token();
|
||||
id = read_unitid(u->faction, r);
|
||||
|
|
|
@ -57,6 +57,8 @@ extern "C" {
|
|||
extern int give_control_cmd(struct unit *u, struct order *ord);
|
||||
extern void give_control(struct unit * u, struct unit * u2);
|
||||
|
||||
struct message * can_steal(const struct unit * u, struct order *ord);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -2,60 +2,122 @@
|
|||
#include <kernel/types.h>
|
||||
#include "economy.h"
|
||||
|
||||
#include <util/message.h>
|
||||
#include <kernel/unit.h>
|
||||
#include <kernel/race.h>
|
||||
#include <kernel/region.h>
|
||||
#include <kernel/building.h>
|
||||
#include <kernel/ship.h>
|
||||
#include <kernel/terrain.h>
|
||||
|
||||
#include <CuTest.h>
|
||||
#include <tests.h>
|
||||
|
||||
static void test_give_control_building(CuTest * tc)
|
||||
{
|
||||
unit *u1, *u2;
|
||||
building *b;
|
||||
struct faction *f;
|
||||
region *r;
|
||||
unit *u1, *u2;
|
||||
building *b;
|
||||
struct faction *f;
|
||||
region *r;
|
||||
|
||||
test_cleanup();
|
||||
test_create_world();
|
||||
f = test_create_faction(0);
|
||||
r = findregion(0, 0);
|
||||
b = test_create_building(r, 0);
|
||||
u1 = test_create_unit(f, r);
|
||||
u_set_building(u1, b);
|
||||
u2 = test_create_unit(f, r);
|
||||
u_set_building(u2, b);
|
||||
CuAssertPtrEquals(tc, u1, building_owner(b));
|
||||
give_control(u1, u2);
|
||||
CuAssertPtrEquals(tc, u2, building_owner(b));
|
||||
test_cleanup();
|
||||
test_create_world();
|
||||
f = test_create_faction(0);
|
||||
r = findregion(0, 0);
|
||||
b = test_create_building(r, 0);
|
||||
u1 = test_create_unit(f, r);
|
||||
u_set_building(u1, b);
|
||||
u2 = test_create_unit(f, r);
|
||||
u_set_building(u2, b);
|
||||
CuAssertPtrEquals(tc, u1, building_owner(b));
|
||||
give_control(u1, u2);
|
||||
CuAssertPtrEquals(tc, u2, building_owner(b));
|
||||
test_cleanup();
|
||||
}
|
||||
|
||||
static void test_give_control_ship(CuTest * tc)
|
||||
{
|
||||
unit *u1, *u2;
|
||||
ship *sh;
|
||||
struct faction *f;
|
||||
region *r;
|
||||
unit *u1, *u2;
|
||||
ship *sh;
|
||||
struct faction *f;
|
||||
region *r;
|
||||
|
||||
test_cleanup();
|
||||
test_create_world();
|
||||
f = test_create_faction(0);
|
||||
r = findregion(0, 0);
|
||||
sh = test_create_ship(r, 0);
|
||||
u1 = test_create_unit(f, r);
|
||||
u_set_ship(u1, sh);
|
||||
u2 = test_create_unit(f, r);
|
||||
u_set_ship(u2, sh);
|
||||
CuAssertPtrEquals(tc, u1, ship_owner(sh));
|
||||
give_control(u1, u2);
|
||||
CuAssertPtrEquals(tc, u2, ship_owner(sh));
|
||||
test_cleanup();
|
||||
test_create_world();
|
||||
f = test_create_faction(0);
|
||||
r = findregion(0, 0);
|
||||
sh = test_create_ship(r, 0);
|
||||
u1 = test_create_unit(f, r);
|
||||
u_set_ship(u1, sh);
|
||||
u2 = test_create_unit(f, r);
|
||||
u_set_ship(u2, sh);
|
||||
CuAssertPtrEquals(tc, u1, ship_owner(sh));
|
||||
give_control(u1, u2);
|
||||
CuAssertPtrEquals(tc, u2, ship_owner(sh));
|
||||
test_cleanup();
|
||||
}
|
||||
|
||||
static struct {
|
||||
struct unit *u;
|
||||
struct region *r;
|
||||
struct faction *f;
|
||||
} steal;
|
||||
|
||||
static void setup_steal(terrain_type *ter, race *rc) {
|
||||
steal.r = test_create_region(0, 0, ter);
|
||||
steal.f = test_create_faction(0);
|
||||
steal.u = test_create_unit(steal.f, steal.r);
|
||||
}
|
||||
|
||||
static void test_steal_okay(CuTest * tc) {
|
||||
race *rc;
|
||||
terrain_type *ter;
|
||||
|
||||
test_cleanup();
|
||||
ter = test_create_terrain("plain", LAND_REGION);
|
||||
rc = test_create_race("human");
|
||||
rc->flags = 0;
|
||||
setup_steal(ter, rc);
|
||||
CuAssertPtrEquals(tc, 0, can_steal(steal.u, 0));
|
||||
test_cleanup();
|
||||
}
|
||||
|
||||
static void test_steal_nosteal(CuTest * tc) {
|
||||
race *rc;
|
||||
terrain_type *ter;
|
||||
message *msg;
|
||||
|
||||
test_cleanup();
|
||||
ter = test_create_terrain("plain", LAND_REGION);
|
||||
rc = test_create_race("human");
|
||||
rc->flags = RCF_NOSTEAL;
|
||||
setup_steal(ter, rc);
|
||||
CuAssertPtrNotNull(tc, msg=can_steal(steal.u, 0));
|
||||
msg_release(msg);
|
||||
test_cleanup();
|
||||
}
|
||||
|
||||
static void test_steal_ocean(CuTest * tc) {
|
||||
race *rc;
|
||||
terrain_type *ter;
|
||||
message *msg;
|
||||
|
||||
test_cleanup();
|
||||
ter = test_create_terrain("ocean", SEA_REGION);
|
||||
rc = test_create_race("human");
|
||||
setup_steal(ter, rc);
|
||||
CuAssertPtrNotNull(tc, msg = can_steal(steal.u, 0));
|
||||
msg_release(msg);
|
||||
test_cleanup();
|
||||
}
|
||||
|
||||
CuSuite *get_economy_suite(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_give_control_building);
|
||||
SUITE_ADD_TEST(suite, test_give_control_ship);
|
||||
return suite;
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_give_control_building);
|
||||
SUITE_ADD_TEST(suite, test_give_control_ship);
|
||||
SUITE_ADD_TEST(suite, test_steal_okay);
|
||||
SUITE_ADD_TEST(suite, test_steal_ocean);
|
||||
SUITE_ADD_TEST(suite, test_steal_nosteal);
|
||||
return suite;
|
||||
}
|
||||
|
|
|
@ -49,11 +49,10 @@ void game_done(void)
|
|||
|
||||
void game_init(void)
|
||||
{
|
||||
kernel_init();
|
||||
kernel_init();
|
||||
register_triggers();
|
||||
register_xmas();
|
||||
|
||||
register_reports();
|
||||
register_nr();
|
||||
register_cr();
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include "plane.h"
|
||||
#include "pool.h"
|
||||
#include "race.h"
|
||||
#include "reports.h"
|
||||
#include "region.h"
|
||||
#include "save.h"
|
||||
#include "ship.h"
|
||||
|
@ -2744,8 +2745,12 @@ void attrib_init(void)
|
|||
|
||||
void kernel_init(void)
|
||||
{
|
||||
attrib_init();
|
||||
translation_init();
|
||||
register_reports();
|
||||
if (!mt_find("missing_message")) {
|
||||
mt_register(mt_new_va("missing_message", "name:string", 0));
|
||||
}
|
||||
attrib_init();
|
||||
translation_init();
|
||||
}
|
||||
|
||||
static order * defaults[MAXLOCALES];
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
#include <platform.h>
|
||||
#include <kernel/config.h>
|
||||
#include <CuTest.h>
|
||||
#include <stdio.h>
|
||||
#include <util/log.h>
|
||||
|
||||
|
||||
CuSuite *get_tests_suite(void);
|
||||
CuSuite *get_callback_suite(void);
|
||||
CuSuite *get_jsonconf_suite(void);
|
||||
|
@ -39,6 +42,7 @@ int RunAllTests(void)
|
|||
int flags = log_flags;
|
||||
|
||||
log_flags = LOG_FLUSH | LOG_CPERROR;
|
||||
kernel_init();
|
||||
|
||||
/* self-test */
|
||||
CuSuiteAddSuite(suite, get_tests_suite());
|
||||
|
|
Loading…
Reference in New Issue