test giving men to other faction.

refactor give_men to return a message.
major milestone reached: 200 unit tests.
This commit is contained in:
Enno Rehling 2014-12-10 21:17:00 +01:00
parent 2a61dc4d11
commit 33a5e0aa9d
5 changed files with 95 additions and 32 deletions

View file

@ -210,16 +210,15 @@ struct order *ord)
return 0; return 0;
} }
void give_men(int n, unit * u, unit * u2, struct order *ord) message * give_men(int n, unit * u, unit * u2, struct order *ord)
{ {
ship *sh; ship *sh;
int k = 0; int k = 0;
int error = 0; int error = 0;
if (u2 && u->faction != u2->faction && u->faction->age < GiveRestriction()) { if (u2 && u->faction != u2->faction && u->faction->age < GiveRestriction()) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "giverestriction", return msg_feedback(u, ord, "giverestriction",
"turns", GiveRestriction())); "turns", GiveRestriction());
return;
} }
else if (u == u2) { else if (u == u2) {
error = 10; error = 10;
@ -246,9 +245,8 @@ void give_men(int n, unit * u, unit * u2, struct order *ord)
error = 75; error = 75;
} }
else if (u2 && !ucontact(u2, u)) { else if (u2 && !ucontact(u2, u)) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "feedback_no_contact", return msg_feedback(u, ord, "feedback_no_contact",
"target", u2)); "target", u2);
error = -1;
} }
else if (u2 && (has_skill(u, SK_MAGIC) || has_skill(u2, SK_MAGIC))) { else if (u2 && (has_skill(u, SK_MAGIC) || has_skill(u2, SK_MAGIC))) {
/* cannot give units to and from magicians */ /* cannot give units to and from magicians */
@ -367,18 +365,17 @@ void give_men(int n, unit * u, unit * u2, struct order *ord)
} }
} }
if (error > 0) { if (error > 0) {
cmistake(u, ord, error, MSG_COMMERCE); return msg_error(u, ord, error);
} }
else if (!u2) { else if (!u2) {
ADDMSG(&u->faction->msgs, return msg_message("give_person_peasants", "unit amount", u, n);
msg_message("give_person_peasants", "unit amount", u, n));
} }
else if (u2->faction != u->faction) { else if (u2->faction != u->faction) {
message *msg = msg_message("give_person", "unit target amount", u, u2, n); message *msg = msg_message("give_person", "unit target amount", u, u2, n);
add_message(&u->faction->msgs, msg);
add_message(&u2->faction->msgs, msg); add_message(&u2->faction->msgs, msg);
msg_release(msg); return msg;
} }
return NULL;
} }
void give_unit(unit * u, unit * u2, order * ord) void give_unit(unit * u, unit * u2, order * ord)
@ -411,6 +408,7 @@ void give_unit(unit * u, unit * u2, order * ord)
} }
else if (getunitpeasants) { else if (getunitpeasants) {
unit *u3; unit *u3;
message *msg;
for (u3 = r->units; u3; u3 = u3->next) for (u3 = r->units; u3; u3 = u3->next)
if (u3->faction == u->faction && u != u3) if (u3->faction == u->faction && u != u3)
@ -428,8 +426,13 @@ void give_unit(unit * u, unit * u2, order * ord)
} }
} }
} }
give_men(u->number, u, NULL, ord); msg = give_men(u->number, u, NULL, ord);
cmistake(u, ord, 153, MSG_COMMERCE); if (msg) {
ADDMSG(&u->faction->msgs, msg);
}
else {
cmistake(u, ord, 153, MSG_COMMERCE);
}
} }
else { else {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "feedback_unit_not_found", ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "feedback_unit_not_found",
@ -669,8 +672,11 @@ void give_cmd(unit * u, order * ord)
msg_feedback(u, ord, "race_noregroup", "race", u_race(u))); msg_feedback(u, ord, "race_noregroup", "race", u_race(u)));
} }
else { else {
n = u->number; message * msg;
give_men(n, u, u2, ord); msg = give_men(u->number, u, u2, ord);
if (msg) {
ADDMSG(&u->faction->msgs, msg);
}
} }
} }
else if (!(u_race(u)->ec_flags & GIVEITEM) && u2 != NULL) { else if (!(u_race(u)->ec_flags & GIVEITEM) && u2 != NULL) {
@ -717,12 +723,16 @@ void give_cmd(unit * u, order * ord)
} }
if (isparam(s, u->faction->locale, P_PERSON)) { if (isparam(s, u->faction->locale, P_PERSON)) {
message * msg;
if (!(u_race(u)->ec_flags & GIVEPERSON)) { if (!(u_race(u)->ec_flags & GIVEPERSON)) {
ADDMSG(&u->faction->msgs, ADDMSG(&u->faction->msgs,
msg_feedback(u, ord, "race_noregroup", "race", u_race(u))); msg_feedback(u, ord, "race_noregroup", "race", u_race(u)));
return; return;
} }
give_men(n, u, u2, ord); msg = give_men(u->number, u, u2, ord);
if (msg) {
ADDMSG(&u->faction->msgs, msg);
}
return; return;
} }

View file

@ -19,10 +19,11 @@ extern "C" {
struct item_type; struct item_type;
struct order; struct order;
struct unit; struct unit;
struct message;
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);
void 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);
void give_cmd(struct unit * u, struct order * ord); void give_cmd(struct unit * u, struct order * ord);

View file

@ -41,21 +41,66 @@ static void test_give_men(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);
give_men(1, env.src, env.dst, NULL); CuAssertPtrEquals(tc, 0, give_men(1, env.src, env.dst, NULL));
CuAssertIntEquals(tc, 2, env.dst->number); CuAssertIntEquals(tc, 2, env.dst->number);
CuAssertIntEquals(tc, 0, env.src->number); CuAssertIntEquals(tc, 0, env.src->number);
test_cleanup(); test_cleanup();
} }
static void test_give_men_other_faction(CuTest * tc) {
struct give env;
message * msg;
test_cleanup();
env.f1 = test_create_faction(0);
env.f2 = test_create_faction(0);
setup_give(&env);
usetcontact(env.dst, env.src);
msg = give_men(1, env.src, env.dst, NULL);
CuAssertStrEquals(tc, "give_person", (const char *)msg->parameters[0].v);
CuAssertIntEquals(tc, 2, env.dst->number);
CuAssertIntEquals(tc, 0, env.src->number);
test_cleanup();
}
static void test_give_men_requires_contact(CuTest * tc) {
struct give env;
message * msg;
test_cleanup();
env.f1 = test_create_faction(0);
env.f2 = test_create_faction(0);
setup_give(&env);
msg = give_men(1, env.src, env.dst, NULL);
CuAssertStrEquals(tc, "feedback_no_contact", (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_not_to_self(CuTest * tc) {
struct give env;
message * msg;
test_cleanup();
env.f2 = env.f1 = test_create_faction(0);
setup_give(&env);
msg = give_men(1, env.src, NULL, NULL);
CuAssertStrEquals(tc, "error159", (const char *)msg->parameters[3].v);
CuAssertIntEquals(tc, 1, env.src->number);
test_cleanup();
}
static void test_give_peasants(CuTest * tc) { static void test_give_peasants(CuTest * tc) {
struct give env; struct give env;
message * msg;
int peasants; int peasants;
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);
peasants = env.r->land->peasants; peasants = env.r->land->peasants;
getunitpeasants = 1; getunitpeasants = 1;
give_men(1, env.src, NULL, NULL); msg = give_men(1, env.src, NULL, NULL);
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);
test_cleanup(); test_cleanup();
@ -137,6 +182,9 @@ 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_other_faction);
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_peasants); SUITE_ADD_TEST(suite, test_give_peasants);
SUITE_ADD_TEST(suite, test_give_herbs); SUITE_ADD_TEST(suite, test_give_herbs);
SUITE_ADD_TEST(suite, test_give_okay); SUITE_ADD_TEST(suite, test_give_okay);

View file

@ -251,18 +251,22 @@ void addmessage(region * r, faction * f, const char *s, msg_t mtype, int level)
caddmessage(r, f, s, mtype, level); caddmessage(r, f, s, mtype, level);
} }
message * msg_error(const unit * u, struct order *ord, int mno) {
static char msgname[20];
if (fval(u->faction, FFL_NPC))
return 0;
sprintf(msgname, "error%d", mno);
return msg_feedback(u, ord, msgname, "");
}
message * cmistake(const unit * u, struct order *ord, int mno, int mtype) message * cmistake(const unit * u, struct order *ord, int mno, int mtype)
{ {
message * result; message * result;
static char msgname[20]; unused_arg(mtype);
unused_arg(mtype); result = msg_error(u, ord, mno);
ADDMSG(&u->faction->msgs, result);
if (fval(u->faction, FFL_NPC)) return result;
return 0;
sprintf(msgname, "error%d", mno);
result = msg_feedback(u, ord, msgname, "");
ADDMSG(&u->faction->msgs, result);
return result;
} }
extern unsigned int new_hashstring(const char *s); extern unsigned int new_hashstring(const char *s);

View file

@ -55,8 +55,8 @@ extern "C" {
#define ADDMSG(msgs, mcreate) { message * m = mcreate; if (m) { assert(m->refcount>=1); add_message(msgs, m); msg_release(m); } } #define ADDMSG(msgs, mcreate) { message * m = mcreate; if (m) { assert(m->refcount>=1); add_message(msgs, m); msg_release(m); } }
extern struct message * cmistake(const struct unit *u, struct order *ord, int mno, struct message * cmistake(const struct unit *u, struct order *ord, int mno, int mtype);
int mtype); struct message * msg_error(const struct unit * u, struct order *ord, int mno);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif