test giving too many and too few units.

start moving give-to-peasants logic into its own function.
This commit is contained in:
Enno Rehling 2014-12-10 22:14:25 +01:00
parent 33a5e0aa9d
commit f1662077c3
3 changed files with 41 additions and 7 deletions

View file

@ -378,6 +378,11 @@ message * give_men(int n, unit * u, unit * u2, struct order *ord)
return NULL; return NULL;
} }
message * disband_men(int n, unit * u, struct order *ord) {
getunitpeasants = true;
return give_men(n, u, NULL, ord);
}
void give_unit(unit * u, unit * u2, order * ord) void give_unit(unit * u, unit * u2, order * ord)
{ {
region *r = u->region; region *r = u->region;
@ -426,7 +431,7 @@ void give_unit(unit * u, unit * u2, order * ord)
} }
} }
} }
msg = give_men(u->number, u, NULL, ord); msg = disband_men(u->number, u, ord);
if (msg) { if (msg) {
ADDMSG(&u->faction->msgs, msg); ADDMSG(&u->faction->msgs, msg);
} }

View file

@ -23,6 +23,7 @@ extern "C" {
int give_item(int want, const struct item_type *itype, int give_item(int want, const struct item_type *itype,
struct unit *src, struct unit *dest, struct order *ord); struct unit *src, struct unit *dest, struct order *ord);
struct message * disband_men(int n, struct unit * u, struct order *ord);
struct message * give_men(int n, struct unit *u, struct unit *u2, struct message * give_men(int n, struct unit *u, struct unit *u2,
struct order *ord); struct order *ord);
void give_unit(struct unit *u, struct unit *u2, struct order *ord); void give_unit(struct unit *u, struct unit *u2, struct order *ord);

View file

@ -30,8 +30,8 @@ struct give {
static void setup_give(struct give *env) { static void setup_give(struct give *env) {
struct terrain_type *ter = test_create_terrain("plain", LAND_REGION); struct terrain_type *ter = test_create_terrain("plain", LAND_REGION);
env->r = test_create_region(0, 0, ter); env->r = test_create_region(0, 0, ter);
env->src = test_create_unit(env->f1, env->r); env->src = env->f1 ? test_create_unit(env->f1, env->r) : 0;
env->dst = test_create_unit(env->f2, env->r); env->dst = env->f2 ? test_create_unit(env->f2, env->r) : 0;
env->itype = it_get_or_create(rt_get_or_create("money")); env->itype = it_get_or_create(rt_get_or_create("money"));
env->itype->flags |= ITF_HERB; env->itype->flags |= ITF_HERB;
} }
@ -47,6 +47,31 @@ static void test_give_men(CuTest * tc) {
test_cleanup(); test_cleanup();
} }
static void test_give_men_too_many(CuTest * tc) {
struct give env;
test_cleanup();
env.f2 = env.f1 = test_create_faction(0);
setup_give(&env);
CuAssertPtrEquals(tc, 0, give_men(2, env.src, env.dst, NULL));
CuAssertIntEquals(tc, 2, env.dst->number);
CuAssertIntEquals(tc, 0, env.src->number);
test_cleanup();
}
static void test_give_men_none(CuTest * tc) {
struct give env;
message * msg;
test_cleanup();
env.f2 = env.f1 = test_create_faction(0);
setup_give(&env);
msg = give_men(0, env.src, env.dst, NULL);
CuAssertStrEquals(tc, "error96", (const char *)msg->parameters[3].v);
CuAssertIntEquals(tc, 1, env.dst->number);
CuAssertIntEquals(tc, 1, env.src->number);
test_cleanup();
}
static void test_give_men_other_faction(CuTest * tc) { static void test_give_men_other_faction(CuTest * tc) {
struct give env; struct give env;
message * msg; message * msg;
@ -84,8 +109,8 @@ static void test_give_men_not_to_self(CuTest * tc) {
test_cleanup(); test_cleanup();
env.f2 = env.f1 = test_create_faction(0); env.f2 = env.f1 = test_create_faction(0);
setup_give(&env); setup_give(&env);
msg = give_men(1, env.src, NULL, NULL); msg = give_men(1, env.src, env.src, NULL);
CuAssertStrEquals(tc, "error159", (const char *)msg->parameters[3].v); CuAssertStrEquals(tc, "error10", (const char *)msg->parameters[3].v);
CuAssertIntEquals(tc, 1, env.src->number); CuAssertIntEquals(tc, 1, env.src->number);
test_cleanup(); test_cleanup();
} }
@ -95,11 +120,12 @@ static void test_give_peasants(CuTest * tc) {
message * msg; message * msg;
int peasants; int peasants;
test_cleanup(); test_cleanup();
env.f2 = env.f1 = test_create_faction(0); env.f1 = test_create_faction(0);
env.f2 = 0;
setup_give(&env); setup_give(&env);
peasants = env.r->land->peasants; peasants = env.r->land->peasants;
getunitpeasants = 1; getunitpeasants = 1;
msg = give_men(1, env.src, NULL, NULL); msg = disband_men(1, env.src, NULL);
CuAssertStrEquals(tc, "give_person_peasants", (const char*)msg->parameters[0].v); CuAssertStrEquals(tc, "give_person_peasants", (const char*)msg->parameters[0].v);
CuAssertIntEquals(tc, 0, env.src->number); CuAssertIntEquals(tc, 0, env.src->number);
CuAssertIntEquals(tc, peasants+1, env.r->land->peasants); CuAssertIntEquals(tc, peasants+1, env.r->land->peasants);
@ -182,6 +208,8 @@ CuSuite *get_give_suite(void)
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_give); SUITE_ADD_TEST(suite, test_give);
SUITE_ADD_TEST(suite, test_give_men); SUITE_ADD_TEST(suite, test_give_men);
SUITE_ADD_TEST(suite, test_give_men_none);
SUITE_ADD_TEST(suite, test_give_men_too_many);
SUITE_ADD_TEST(suite, test_give_men_other_faction); SUITE_ADD_TEST(suite, test_give_men_other_faction);
SUITE_ADD_TEST(suite, test_give_men_requires_contact); SUITE_ADD_TEST(suite, test_give_men_requires_contact);
SUITE_ADD_TEST(suite, test_give_men_not_to_self); SUITE_ADD_TEST(suite, test_give_men_not_to_self);