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,
int mask)
static bool can_give(const unit * u, const unit * u2, const item_type * itype, int mask)
{
if (u2) {
if (u->faction != u2->faction) {
@ -700,6 +699,14 @@ int give_control_cmd(unit * u, order * ord)
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)
{
region *r = u->region;
@ -709,6 +716,7 @@ static void give_cmd(unit * u, order * ord)
const item_type *itype;
param_t p;
plane *pl;
message *msg;
init_tokens(ord);
skip_token();
@ -729,9 +737,10 @@ static void give_cmd(unit * u, order * ord)
return;
}
if (!check_give(u, u2, NULL, GIVE_ALLITEMS)) {
feedback_give_not_allowed(u, ord);
return;
msg = check_give(u, u2, ord);
if (msg) {
ADDMSG(&u->faction->msgs, msg);
return;
}
/* Damit Tarner nicht durch die Fehlermeldung enttarnt werden können */
@ -773,7 +782,7 @@ static void give_cmd(unit * u, order * ord)
msg_feedback(u, ord, "race_nogive", "race", u_race(u)));
return;
}
if (!check_give(u, u2, NULL, GIVE_HERBS)) {
if (!can_give(u, u2, NULL, GIVE_HERBS)) {
feedback_give_not_allowed(u, ord);
return;
}
@ -831,7 +840,7 @@ static void give_cmd(unit * u, order * ord)
else if (p == P_ANY) {
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);
return;
}
@ -888,7 +897,7 @@ static void give_cmd(unit * u, order * ord)
if (itype != NULL) {
item *i = *i_find(&u->items, itype);
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);
give_item(n, itype, u, u2, ord);
} else {
@ -941,7 +950,7 @@ static void give_cmd(unit * u, order * ord)
itype = finditemtype(s, u->faction->locale);
if (itype != NULL) {
if (check_give(u, u2, itype, 0)) {
if (can_give(u, u2, itype, 0)) {
give_item(n, itype, u, u2, ord);
} else {
feedback_give_not_allowed(u, ord);
@ -2747,7 +2756,7 @@ static int max_skill(region * r, faction * f, skill_t sk)
return w;
}
message * can_steal(const unit * u, struct order *ord) {
message * check_steal(const unit * u, struct order *ord) {
plane *pl;
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));
msg = can_steal(u, ord);
msg = check_steal(u, ord);
if (msg) {
ADDMSG(&u->faction->msgs, msg);
return;

View File

@ -57,7 +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);
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
}

View File

@ -57,19 +57,20 @@ static void test_give_control_ship(CuTest * tc)
test_cleanup();
}
static struct {
struct steal {
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 setup_steal(struct steal *env, terrain_type *ter, race *rc) {
env->r = test_create_region(0, 0, ter);
env->f = test_create_faction(rc);
env->u = test_create_unit(env->f, env->r);
}
static void test_steal_okay(CuTest * tc) {
struct steal env;
race *rc;
terrain_type *ter;
@ -77,12 +78,13 @@ static void test_steal_okay(CuTest * tc) {
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));
setup_steal(&env, ter, rc);
CuAssertPtrEquals(tc, 0, check_steal(env.u, 0));
test_cleanup();
}
static void test_steal_nosteal(CuTest * tc) {
struct steal env;
race *rc;
terrain_type *ter;
message *msg;
@ -91,13 +93,14 @@ static void test_steal_nosteal(CuTest * tc) {
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));
setup_steal(&env, ter, rc);
CuAssertPtrNotNull(tc, msg = check_steal(env.u, 0));
msg_release(msg);
test_cleanup();
}
static void test_steal_ocean(CuTest * tc) {
struct steal env;
race *rc;
terrain_type *ter;
message *msg;
@ -105,12 +108,36 @@ static void test_steal_ocean(CuTest * tc) {
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));
setup_steal(&env, ter, rc);
CuAssertPtrNotNull(tc, msg = check_steal(env.u, 0));
msg_release(msg);
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 *suite = CuSuiteNew();
@ -119,5 +146,6 @@ CuSuite *get_economy_suite(void)
SUITE_ADD_TEST(suite, test_steal_okay);
SUITE_ADD_TEST(suite, test_steal_ocean);
SUITE_ADD_TEST(suite, test_steal_nosteal);
SUITE_ADD_TEST(suite, test_give_okay);
return suite;
}