test rename with missing name (not crashing, error message)

This commit is contained in:
Enno Rehling 2015-10-13 14:27:42 +02:00
parent 6de604701d
commit 96be12218e
2 changed files with 64 additions and 21 deletions

View File

@ -1622,7 +1622,8 @@ bool renamed_building(const building * b)
static int rename_cmd(unit * u, order * ord, char **s, const char *s2)
{
if (!s2 || !s2[0]) {
assert(s2);
if (!s2[0]) {
cmistake(u, ord, 84, MSG_EVENT);
return 0;
}
@ -1638,26 +1639,24 @@ static int rename_cmd(unit * u, order * ord, char **s, const char *s2)
return 0;
}
int
rename_building(unit * u, order * ord, building * b, const char *name)
{
static bool try_rename(unit *u, building *b, order *ord) {
unit *owner = b ? building_owner(b) : 0;
bool foreign = !(owner && owner->faction == u->faction);
if (!b) {
cmistake(u, ord, u->building ? 6 : 145, MSG_EVENT);
return -1;
return false;
}
if (!fval(b->type, BTF_NAMECHANGE) && renamed_building(b)) {
cmistake(u, ord, 278, MSG_EVENT);
return -1;
return false;
}
if (foreign) {
if (renamed_building(b)) {
cmistake(u, ord, 246, MSG_EVENT);
return -1;
return false;
}
if (owner) {
@ -1671,15 +1670,22 @@ rename_building(unit * u, order * ord, building * b, const char *name)
msg_message("renamed_building_notseen",
"building region", b, u->region));
}
}
}
else {
if (owner != u) {
cmistake(u, ord, 148, MSG_PRODUCE);
return -1;
return false;
}
}
}
return true;
}
int
rename_building(unit * u, order * ord, building * b, const char *name)
{
assert(name);
if (!try_rename(u, b, ord)) {
return -1;
}
return rename_cmd(u, ord, &b->name, name);
}
@ -1718,9 +1724,10 @@ int name_cmd(struct unit *u, struct order *ord)
if (foreign) {
b = getbuilding(u->region);
}
return rename_building(u, ord, b, getstrtoken());
if (try_rename(u, b, ord)) {
s = &b->name;
}
break;
case P_FACTION:
if (foreign) {
faction *f;
@ -1896,6 +1903,9 @@ int name_cmd(struct unit *u, struct order *ord)
if (name) {
rename_cmd(u, ord, s, name);
}
else {
cmistake(u, ord, 84, MSG_EVENT);
}
}
return 0;

View File

@ -786,9 +786,18 @@ static void test_name_unit(CuTest *tc) {
order *ord;
u = setup_name_cmd();
ord = create_order(K_NAME, u->faction->locale, "UNIT Hodor");
name_cmd(u, ord);
CuAssertStrEquals(tc, "Hodor", u->_name);
free_order(ord);
ord = create_order(K_NAME, u->faction->locale, "UNIT");
name_cmd(u, ord);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error84"));
CuAssertStrEquals(tc, "Hodor", u->_name);
free_order(ord);
test_cleanup();
}
@ -797,14 +806,21 @@ static void test_name_region(CuTest *tc) {
order *ord;
u = setup_name_cmd();
ord = create_order(K_NAME, u->faction->locale, "REGION Hodor");
ord = create_order(K_NAME, u->faction->locale, "REGION Hodor");
name_cmd(u, ord);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error145"));
u->building = test_create_building(u->region, 0);
name_cmd(u, ord);
CuAssertStrEquals(tc, "Hodor", u->region->land->name);
free_order(ord);
ord = create_order(K_NAME, u->faction->locale, "REGION");
name_cmd(u, ord);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error84"));
CuAssertStrEquals(tc, "Hodor", u->region->land->name);
free_order(ord);
test_cleanup();
}
@ -814,14 +830,22 @@ static void test_name_building(CuTest *tc) {
order *ord;
u = setup_name_cmd();
ord = create_order(K_NAME, u->faction->locale, "BUILDING Hodor");
ord = create_order(K_NAME, u->faction->locale, "BUILDING Hodor");
name_cmd(u, ord);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error145"));
u->building = test_create_building(u->region, 0);
name_cmd(u, ord);
CuAssertStrEquals(tc, "Hodor", u->building->name);
free_order(ord);
ord = create_order(K_NAME, u->faction->locale, "BUILDING");
name_cmd(u, ord);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error84"));
CuAssertStrEquals(tc, "Hodor", u->building->name);
free_order(ord);
/* TODO: test BTF_NAMECHANGE:
btype->flags |= BTF_NAMECHANGE;
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error278"));
@ -836,9 +860,18 @@ static void test_name_ship(CuTest *tc) {
u = setup_name_cmd();
u->ship = test_create_ship(u->region, 0);
ord = create_order(K_NAME, u->faction->locale, "SHIP Hodor");
name_cmd(u, ord);
CuAssertStrEquals(tc, "Hodor", u->ship->name);
free_order(ord);
ord = create_order(K_NAME, u->faction->locale, "SHIP");
name_cmd(u, ord);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error84"));
CuAssertStrEquals(tc, "Hodor", u->ship->name);
free_order(ord);
test_cleanup();
}