From 96be12218e1cf3be1935d8988af42e08fd874360 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 13 Oct 2015 14:27:42 +0200 Subject: [PATCH] test rename with missing name (not crashing, error message) --- src/laws.c | 46 ++++++++++++++++++++++++++++------------------ src/laws.test.c | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/laws.c b/src/laws.c index 549acb2b2..2fd9ce1d8 100755 --- a/src/laws.c +++ b/src/laws.c @@ -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,48 +1639,53 @@ 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) { if (cansee(owner->faction, u->region, u, 0)) { ADDMSG(&owner->faction->msgs, msg_message("renamed_building_seen", - "building renamer region", b, u, u->region)); + "building renamer region", b, u, u->region)); } else { ADDMSG(&owner->faction->msgs, msg_message("renamed_building_notseen", - "building region", b, u->region)); + "building region", b, u->region)); + } + if (owner != u) { + cmistake(u, ord, 148, MSG_PRODUCE); + return false; } } } - else { - if (owner != u) { - cmistake(u, ord, 148, MSG_PRODUCE); - return -1; - } - } + 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; diff --git a/src/laws.test.c b/src/laws.test.c index 8c4a7d341..5918001d9 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -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,15 +830,23 @@ 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); -/* TODO: test BTF_NAMECHANGE: + 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")); test_clear_messages(u->faction); @@ -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(); }