forked from github/server
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:
parent
072f927d5b
commit
f14264e3bb
6 changed files with 135 additions and 57 deletions
|
@ -2747,6 +2747,24 @@ static int max_skill(region * r, faction * f, skill_t sk)
|
||||||
return w;
|
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)
|
static void steal_cmd(unit * u, struct order *ord, request ** stealorders)
|
||||||
{
|
{
|
||||||
const resource_type *rring = get_resourcetype(R_RING_OF_NIMBLEFINGER);
|
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;
|
unit *u2 = NULL;
|
||||||
region *r = u->region;
|
region *r = u->region;
|
||||||
faction *f = NULL;
|
faction *f = NULL;
|
||||||
plane *pl;
|
message * msg;
|
||||||
|
|
||||||
assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH));
|
assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH));
|
||||||
|
|
||||||
if (fval(u_race(u), RCF_NOSTEAL)) {
|
msg = can_steal(u, ord);
|
||||||
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "race_nosteal", "race",
|
if (msg) {
|
||||||
u_race(u)));
|
ADDMSG(&u->faction->msgs, msg);
|
||||||
return;
|
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);
|
init_tokens(ord);
|
||||||
skip_token();
|
skip_token();
|
||||||
id = read_unitid(u->faction, r);
|
id = read_unitid(u->faction, r);
|
||||||
|
|
|
@ -57,6 +57,8 @@ extern "C" {
|
||||||
extern int give_control_cmd(struct unit *u, struct order *ord);
|
extern int give_control_cmd(struct unit *u, struct order *ord);
|
||||||
extern void give_control(struct unit * u, struct unit * u2);
|
extern void give_control(struct unit * u, struct unit * u2);
|
||||||
|
|
||||||
|
struct message * can_steal(const struct unit * u, struct order *ord);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -2,60 +2,122 @@
|
||||||
#include <kernel/types.h>
|
#include <kernel/types.h>
|
||||||
#include "economy.h"
|
#include "economy.h"
|
||||||
|
|
||||||
|
#include <util/message.h>
|
||||||
#include <kernel/unit.h>
|
#include <kernel/unit.h>
|
||||||
|
#include <kernel/race.h>
|
||||||
#include <kernel/region.h>
|
#include <kernel/region.h>
|
||||||
#include <kernel/building.h>
|
#include <kernel/building.h>
|
||||||
#include <kernel/ship.h>
|
#include <kernel/ship.h>
|
||||||
|
#include <kernel/terrain.h>
|
||||||
|
|
||||||
#include <CuTest.h>
|
#include <CuTest.h>
|
||||||
#include <tests.h>
|
#include <tests.h>
|
||||||
|
|
||||||
static void test_give_control_building(CuTest * tc)
|
static void test_give_control_building(CuTest * tc)
|
||||||
{
|
{
|
||||||
unit *u1, *u2;
|
unit *u1, *u2;
|
||||||
building *b;
|
building *b;
|
||||||
struct faction *f;
|
struct faction *f;
|
||||||
region *r;
|
region *r;
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
test_create_world();
|
test_create_world();
|
||||||
f = test_create_faction(0);
|
f = test_create_faction(0);
|
||||||
r = findregion(0, 0);
|
r = findregion(0, 0);
|
||||||
b = test_create_building(r, 0);
|
b = test_create_building(r, 0);
|
||||||
u1 = test_create_unit(f, r);
|
u1 = test_create_unit(f, r);
|
||||||
u_set_building(u1, b);
|
u_set_building(u1, b);
|
||||||
u2 = test_create_unit(f, r);
|
u2 = test_create_unit(f, r);
|
||||||
u_set_building(u2, b);
|
u_set_building(u2, b);
|
||||||
CuAssertPtrEquals(tc, u1, building_owner(b));
|
CuAssertPtrEquals(tc, u1, building_owner(b));
|
||||||
give_control(u1, u2);
|
give_control(u1, u2);
|
||||||
CuAssertPtrEquals(tc, u2, building_owner(b));
|
CuAssertPtrEquals(tc, u2, building_owner(b));
|
||||||
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_give_control_ship(CuTest * tc)
|
static void test_give_control_ship(CuTest * tc)
|
||||||
{
|
{
|
||||||
unit *u1, *u2;
|
unit *u1, *u2;
|
||||||
ship *sh;
|
ship *sh;
|
||||||
struct faction *f;
|
struct faction *f;
|
||||||
region *r;
|
region *r;
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
test_create_world();
|
test_create_world();
|
||||||
f = test_create_faction(0);
|
f = test_create_faction(0);
|
||||||
r = findregion(0, 0);
|
r = findregion(0, 0);
|
||||||
sh = test_create_ship(r, 0);
|
sh = test_create_ship(r, 0);
|
||||||
u1 = test_create_unit(f, r);
|
u1 = test_create_unit(f, r);
|
||||||
u_set_ship(u1, sh);
|
u_set_ship(u1, sh);
|
||||||
u2 = test_create_unit(f, r);
|
u2 = test_create_unit(f, r);
|
||||||
u_set_ship(u2, sh);
|
u_set_ship(u2, sh);
|
||||||
CuAssertPtrEquals(tc, u1, ship_owner(sh));
|
CuAssertPtrEquals(tc, u1, ship_owner(sh));
|
||||||
give_control(u1, u2);
|
give_control(u1, u2);
|
||||||
CuAssertPtrEquals(tc, u2, ship_owner(sh));
|
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)
|
CuSuite *get_economy_suite(void)
|
||||||
{
|
{
|
||||||
CuSuite *suite = CuSuiteNew();
|
CuSuite *suite = CuSuiteNew();
|
||||||
SUITE_ADD_TEST(suite, test_give_control_building);
|
SUITE_ADD_TEST(suite, test_give_control_building);
|
||||||
SUITE_ADD_TEST(suite, test_give_control_ship);
|
SUITE_ADD_TEST(suite, test_give_control_ship);
|
||||||
return suite;
|
SUITE_ADD_TEST(suite, test_steal_okay);
|
||||||
|
SUITE_ADD_TEST(suite, test_steal_ocean);
|
||||||
|
SUITE_ADD_TEST(suite, test_steal_nosteal);
|
||||||
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,11 +49,10 @@ void game_done(void)
|
||||||
|
|
||||||
void game_init(void)
|
void game_init(void)
|
||||||
{
|
{
|
||||||
kernel_init();
|
kernel_init();
|
||||||
register_triggers();
|
register_triggers();
|
||||||
register_xmas();
|
register_xmas();
|
||||||
|
|
||||||
register_reports();
|
|
||||||
register_nr();
|
register_nr();
|
||||||
register_cr();
|
register_cr();
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
#include "plane.h"
|
#include "plane.h"
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
#include "race.h"
|
#include "race.h"
|
||||||
|
#include "reports.h"
|
||||||
#include "region.h"
|
#include "region.h"
|
||||||
#include "save.h"
|
#include "save.h"
|
||||||
#include "ship.h"
|
#include "ship.h"
|
||||||
|
@ -2744,8 +2745,12 @@ void attrib_init(void)
|
||||||
|
|
||||||
void kernel_init(void)
|
void kernel_init(void)
|
||||||
{
|
{
|
||||||
attrib_init();
|
register_reports();
|
||||||
translation_init();
|
if (!mt_find("missing_message")) {
|
||||||
|
mt_register(mt_new_va("missing_message", "name:string", 0));
|
||||||
|
}
|
||||||
|
attrib_init();
|
||||||
|
translation_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
static order * defaults[MAXLOCALES];
|
static order * defaults[MAXLOCALES];
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
#include <platform.h>
|
||||||
|
#include <kernel/config.h>
|
||||||
#include <CuTest.h>
|
#include <CuTest.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <util/log.h>
|
#include <util/log.h>
|
||||||
|
|
||||||
|
|
||||||
CuSuite *get_tests_suite(void);
|
CuSuite *get_tests_suite(void);
|
||||||
CuSuite *get_callback_suite(void);
|
CuSuite *get_callback_suite(void);
|
||||||
CuSuite *get_jsonconf_suite(void);
|
CuSuite *get_jsonconf_suite(void);
|
||||||
|
@ -39,6 +42,7 @@ int RunAllTests(void)
|
||||||
int flags = log_flags;
|
int flags = log_flags;
|
||||||
|
|
||||||
log_flags = LOG_FLUSH | LOG_CPERROR;
|
log_flags = LOG_FLUSH | LOG_CPERROR;
|
||||||
|
kernel_init();
|
||||||
|
|
||||||
/* self-test */
|
/* self-test */
|
||||||
CuSuiteAddSuite(suite, get_tests_suite());
|
CuSuiteAddSuite(suite, get_tests_suite());
|
||||||
|
|
Loading…
Reference in a new issue