test coverage for can_steal.

add a default message for when no messages are loaded, so that msg_feedback/msg_message doesn't return NULL when XML has not been laoded.
initialize some core stuff before running tests (there probably needs to be more).
This commit is contained in:
Enno Rehling 2014-07-05 20:14:11 -07:00
parent 072f927d5b
commit f14264e3bb
6 changed files with 135 additions and 57 deletions

View File

@ -2747,6 +2747,24 @@ static int max_skill(region * r, faction * f, skill_t sk)
return w;
}
message * can_steal(const unit * u, struct order *ord) {
plane *pl;
if (fval(u_race(u), RCF_NOSTEAL)) {
return msg_feedback(u, ord, "race_nosteal", "race", u_race(u));
}
if (fval(u->region->terrain, SEA_REGION) && u_race(u) != get_race(RC_AQUARIAN)) {
return msg_feedback(u, ord, "error_onlandonly", "");
}
pl = rplane(u->region);
if (pl && fval(pl, PFL_NOATTACK)) {
return msg_feedback(u, ord, "error270", "");
}
return 0;
}
static void steal_cmd(unit * u, struct order *ord, request ** stealorders)
{
const resource_type *rring = get_resourcetype(R_RING_OF_NIMBLEFINGER);
@ -2756,27 +2774,15 @@ static void steal_cmd(unit * u, struct order *ord, request ** stealorders)
unit *u2 = NULL;
region *r = u->region;
faction *f = NULL;
plane *pl;
message * msg;
assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH));
if (fval(u_race(u), RCF_NOSTEAL)) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "race_nosteal", "race",
u_race(u)));
msg = can_steal(u, ord);
if (msg) {
ADDMSG(&u->faction->msgs, msg);
return;
}
if (fval(r->terrain, SEA_REGION) && u_race(u) != get_race(RC_AQUARIAN)) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_onlandonly", ""));
return;
}
pl = rplane(r);
if (pl && fval(pl, PFL_NOATTACK)) {
cmistake(u, ord, 270, MSG_INCOME);
return;
}
init_tokens(ord);
skip_token();
id = read_unitid(u->faction, r);

View File

@ -57,6 +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);
#ifdef __cplusplus
}
#endif

View File

@ -2,10 +2,13 @@
#include <kernel/types.h>
#include "economy.h"
#include <util/message.h>
#include <kernel/unit.h>
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/building.h>
#include <kernel/ship.h>
#include <kernel/terrain.h>
#include <CuTest.h>
#include <tests.h>
@ -29,6 +32,7 @@ static void test_give_control_building(CuTest * tc)
CuAssertPtrEquals(tc, u1, building_owner(b));
give_control(u1, u2);
CuAssertPtrEquals(tc, u2, building_owner(b));
test_cleanup();
}
static void test_give_control_ship(CuTest * tc)
@ -50,6 +54,61 @@ static void test_give_control_ship(CuTest * tc)
CuAssertPtrEquals(tc, u1, ship_owner(sh));
give_control(u1, u2);
CuAssertPtrEquals(tc, u2, ship_owner(sh));
test_cleanup();
}
static struct {
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 test_steal_okay(CuTest * tc) {
race *rc;
terrain_type *ter;
test_cleanup();
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));
test_cleanup();
}
static void test_steal_nosteal(CuTest * tc) {
race *rc;
terrain_type *ter;
message *msg;
test_cleanup();
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));
msg_release(msg);
test_cleanup();
}
static void test_steal_ocean(CuTest * tc) {
race *rc;
terrain_type *ter;
message *msg;
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));
msg_release(msg);
test_cleanup();
}
CuSuite *get_economy_suite(void)
@ -57,5 +116,8 @@ CuSuite *get_economy_suite(void)
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_give_control_building);
SUITE_ADD_TEST(suite, test_give_control_ship);
SUITE_ADD_TEST(suite, test_steal_okay);
SUITE_ADD_TEST(suite, test_steal_ocean);
SUITE_ADD_TEST(suite, test_steal_nosteal);
return suite;
}

View File

@ -53,7 +53,6 @@ void game_init(void)
register_triggers();
register_xmas();
register_reports();
register_nr();
register_cr();

View File

@ -46,6 +46,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "plane.h"
#include "pool.h"
#include "race.h"
#include "reports.h"
#include "region.h"
#include "save.h"
#include "ship.h"
@ -2744,6 +2745,10 @@ void attrib_init(void)
void kernel_init(void)
{
register_reports();
if (!mt_find("missing_message")) {
mt_register(mt_new_va("missing_message", "name:string", 0));
}
attrib_init();
translation_init();
}

View File

@ -1,7 +1,10 @@
#include <platform.h>
#include <kernel/config.h>
#include <CuTest.h>
#include <stdio.h>
#include <util/log.h>
CuSuite *get_tests_suite(void);
CuSuite *get_callback_suite(void);
CuSuite *get_jsonconf_suite(void);
@ -39,6 +42,7 @@ int RunAllTests(void)
int flags = log_flags;
log_flags = LOG_FLUSH | LOG_CPERROR;
kernel_init();
/* self-test */
CuSuiteAddSuite(suite, get_tests_suite());