refactor setup for economy tests.

rename can_steal->check_steal for consistent naming, check_give->can_give, too.
This commit is contained in:
Enno Rehling 2014-07-05 21:12:34 -07:00
parent f14264e3bb
commit b9b627a171
3 changed files with 62 additions and 24 deletions

View File

@ -390,8 +390,7 @@ static void feedback_give_not_allowed(unit * u, order * ord)
"")); ""));
} }
static bool check_give(unit * u, unit * u2, const item_type * itype, static bool can_give(const unit * u, const unit * u2, const item_type * itype, int mask)
int mask)
{ {
if (u2) { if (u2) {
if (u->faction != u2->faction) { if (u->faction != u2->faction) {
@ -700,6 +699,14 @@ int give_control_cmd(unit * u, order * ord)
return 0; return 0;
} }
message *check_give(const unit *u, const unit *u2, order * ord) {
if (!can_give(u, u2, NULL, GIVE_ALLITEMS)) {
return msg_feedback(u, ord, "feedback_give_forbidden", "");
}
return 0;
}
static void give_cmd(unit * u, order * ord) static void give_cmd(unit * u, order * ord)
{ {
region *r = u->region; region *r = u->region;
@ -709,6 +716,7 @@ static void give_cmd(unit * u, order * ord)
const item_type *itype; const item_type *itype;
param_t p; param_t p;
plane *pl; plane *pl;
message *msg;
init_tokens(ord); init_tokens(ord);
skip_token(); skip_token();
@ -729,8 +737,9 @@ static void give_cmd(unit * u, order * ord)
return; return;
} }
if (!check_give(u, u2, NULL, GIVE_ALLITEMS)) { msg = check_give(u, u2, ord);
feedback_give_not_allowed(u, ord); if (msg) {
ADDMSG(&u->faction->msgs, msg);
return; return;
} }
@ -773,7 +782,7 @@ static void give_cmd(unit * u, order * ord)
msg_feedback(u, ord, "race_nogive", "race", u_race(u))); msg_feedback(u, ord, "race_nogive", "race", u_race(u)));
return; return;
} }
if (!check_give(u, u2, NULL, GIVE_HERBS)) { if (!can_give(u, u2, NULL, GIVE_HERBS)) {
feedback_give_not_allowed(u, ord); feedback_give_not_allowed(u, ord);
return; return;
} }
@ -831,7 +840,7 @@ static void give_cmd(unit * u, order * ord)
else if (p == P_ANY) { else if (p == P_ANY) {
const char *s; const char *s;
if (!check_give(u, u2, NULL, GIVE_ALLITEMS)) { if (!can_give(u, u2, NULL, GIVE_ALLITEMS)) {
feedback_give_not_allowed(u, ord); feedback_give_not_allowed(u, ord);
return; return;
} }
@ -888,7 +897,7 @@ static void give_cmd(unit * u, order * ord)
if (itype != NULL) { if (itype != NULL) {
item *i = *i_find(&u->items, itype); item *i = *i_find(&u->items, itype);
if (i != NULL) { if (i != NULL) {
if (check_give(u, u2, itype, 0)) { if (can_give(u, u2, itype, 0)) {
n = i->number - get_reservation(u, itype->rtype); n = i->number - get_reservation(u, itype->rtype);
give_item(n, itype, u, u2, ord); give_item(n, itype, u, u2, ord);
} else { } else {
@ -941,7 +950,7 @@ static void give_cmd(unit * u, order * ord)
itype = finditemtype(s, u->faction->locale); itype = finditemtype(s, u->faction->locale);
if (itype != NULL) { if (itype != NULL) {
if (check_give(u, u2, itype, 0)) { if (can_give(u, u2, itype, 0)) {
give_item(n, itype, u, u2, ord); give_item(n, itype, u, u2, ord);
} else { } else {
feedback_give_not_allowed(u, ord); feedback_give_not_allowed(u, ord);
@ -2747,7 +2756,7 @@ static int max_skill(region * r, faction * f, skill_t sk)
return w; return w;
} }
message * can_steal(const unit * u, struct order *ord) { message * check_steal(const unit * u, struct order *ord) {
plane *pl; plane *pl;
if (fval(u_race(u), RCF_NOSTEAL)) { if (fval(u_race(u), RCF_NOSTEAL)) {
@ -2778,7 +2787,7 @@ static void steal_cmd(unit * u, struct order *ord, request ** stealorders)
assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH)); assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH));
msg = can_steal(u, ord); msg = check_steal(u, ord);
if (msg) { if (msg) {
ADDMSG(&u->faction->msgs, msg); ADDMSG(&u->faction->msgs, msg);
return; return;

View File

@ -57,7 +57,8 @@ extern "C" {
extern int give_control_cmd(struct unit *u, struct order *ord); extern int give_control_cmd(struct unit *u, struct order *ord);
extern void give_control(struct unit * u, struct unit * u2); extern void give_control(struct unit * u, struct unit * u2);
struct message * can_steal(const struct unit * u, struct order *ord); struct message * check_steal(const struct unit * u, struct order *ord);
struct message * check_give(const struct unit * u, const struct unit * u2, struct order *ord);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -57,19 +57,20 @@ static void test_give_control_ship(CuTest * tc)
test_cleanup(); test_cleanup();
} }
static struct { struct steal {
struct unit *u; struct unit *u;
struct region *r; struct region *r;
struct faction *f; struct faction *f;
} steal; };
static void setup_steal(terrain_type *ter, race *rc) { static void setup_steal(struct steal *env, terrain_type *ter, race *rc) {
steal.r = test_create_region(0, 0, ter); env->r = test_create_region(0, 0, ter);
steal.f = test_create_faction(0); env->f = test_create_faction(rc);
steal.u = test_create_unit(steal.f, steal.r); env->u = test_create_unit(env->f, env->r);
} }
static void test_steal_okay(CuTest * tc) { static void test_steal_okay(CuTest * tc) {
struct steal env;
race *rc; race *rc;
terrain_type *ter; terrain_type *ter;
@ -77,12 +78,13 @@ static void test_steal_okay(CuTest * tc) {
ter = test_create_terrain("plain", LAND_REGION); ter = test_create_terrain("plain", LAND_REGION);
rc = test_create_race("human"); rc = test_create_race("human");
rc->flags = 0; rc->flags = 0;
setup_steal(ter, rc); setup_steal(&env, ter, rc);
CuAssertPtrEquals(tc, 0, can_steal(steal.u, 0)); CuAssertPtrEquals(tc, 0, check_steal(env.u, 0));
test_cleanup(); test_cleanup();
} }
static void test_steal_nosteal(CuTest * tc) { static void test_steal_nosteal(CuTest * tc) {
struct steal env;
race *rc; race *rc;
terrain_type *ter; terrain_type *ter;
message *msg; message *msg;
@ -91,13 +93,14 @@ static void test_steal_nosteal(CuTest * tc) {
ter = test_create_terrain("plain", LAND_REGION); ter = test_create_terrain("plain", LAND_REGION);
rc = test_create_race("human"); rc = test_create_race("human");
rc->flags = RCF_NOSTEAL; rc->flags = RCF_NOSTEAL;
setup_steal(ter, rc); setup_steal(&env, ter, rc);
CuAssertPtrNotNull(tc, msg=can_steal(steal.u, 0)); CuAssertPtrNotNull(tc, msg = check_steal(env.u, 0));
msg_release(msg); msg_release(msg);
test_cleanup(); test_cleanup();
} }
static void test_steal_ocean(CuTest * tc) { static void test_steal_ocean(CuTest * tc) {
struct steal env;
race *rc; race *rc;
terrain_type *ter; terrain_type *ter;
message *msg; message *msg;
@ -105,12 +108,36 @@ static void test_steal_ocean(CuTest * tc) {
test_cleanup(); test_cleanup();
ter = test_create_terrain("ocean", SEA_REGION); ter = test_create_terrain("ocean", SEA_REGION);
rc = test_create_race("human"); rc = test_create_race("human");
setup_steal(ter, rc); setup_steal(&env, ter, rc);
CuAssertPtrNotNull(tc, msg = can_steal(steal.u, 0)); CuAssertPtrNotNull(tc, msg = check_steal(env.u, 0));
msg_release(msg); msg_release(msg);
test_cleanup(); test_cleanup();
} }
struct give {
struct unit *src, *dst;
struct region *r;
struct faction *f;
};
static void setup_give(struct give *env) {
terrain_type *ter = test_create_terrain("plain", LAND_REGION);
struct race * rc = test_create_race("human");
env->r = test_create_region(0, 0, ter);
env->f = test_create_faction(0);
env->src = test_create_unit(env->f, env->r);
env->dst = test_create_unit(env->f, env->r);
}
static void test_give_okay(CuTest * tc) {
struct give env;
test_cleanup();
setup_give(&env);
CuAssertPtrEquals(tc, 0, check_give(env.src, env.dst, 0));
test_cleanup();
}
CuSuite *get_economy_suite(void) CuSuite *get_economy_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -119,5 +146,6 @@ CuSuite *get_economy_suite(void)
SUITE_ADD_TEST(suite, test_steal_okay); SUITE_ADD_TEST(suite, test_steal_okay);
SUITE_ADD_TEST(suite, test_steal_ocean); SUITE_ADD_TEST(suite, test_steal_ocean);
SUITE_ADD_TEST(suite, test_steal_nosteal); SUITE_ADD_TEST(suite, test_steal_nosteal);
SUITE_ADD_TEST(suite, test_give_okay);
return suite; return suite;
} }