Add missing test coverage for update_guards. Fix the weapon:minskill default.

This commit is contained in:
Enno Rehling 2014-10-14 18:32:04 +02:00
parent 815cf3edbc
commit ce3e9cd7d0
6 changed files with 191 additions and 90 deletions

View File

@ -226,7 +226,7 @@ int armedmen(const unit * u, bool siege_weapons)
const weapon_type *wtype = resource2weapon(itm->type->rtype);
if (wtype == NULL || (!siege_weapons && (wtype->flags & WTF_SIEGE)))
continue;
if (effskill(u, wtype->skill) >= 1)
if (effskill(u, wtype->skill) >= wtype->minskill)
n += itm->number;
/* if (effskill(u, wtype->skill) >= wtype->minskill) n += itm->number; */
if (n > u->number)

View File

@ -186,7 +186,7 @@ resource_type *rt_get_or_create(const char *name) {
return rtype;
}
void it_register(item_type * itype)
static void it_register(item_type * itype)
{
char buffer[64];
const char * name = itype->rtype->_name;
@ -270,6 +270,7 @@ weapon_type *new_weapontype(item_type * itype,
{
weapon_type *wtype;
assert(minskill > 0);
assert(resource2weapon(itype->rtype) == NULL);
wtype = calloc(sizeof(weapon_type), 1);

View File

@ -229,8 +229,6 @@ extern "C" {
item_type *it_find(const char *name);
void it_set_appearance(item_type *itype, const char *appearance);
void it_register(item_type * it);
void wt_register(weapon_type * wt);
extern const item_type *resource2item(const resource_type * rtype);
extern const resource_type *item2resource(const item_type * i);

View File

@ -119,7 +119,7 @@ extern "C" {
typedef struct race {
struct param *parameters;
const char *_name; /* neu: name[4]völker */
const char *_name;
float magres;
float maxaura; /* Faktor auf Maximale Aura */
float regaura; /* Faktor auf Regeneration */

View File

@ -631,7 +631,7 @@ static weapon_type *xml_readweapon(xmlXPathContextPtr xpath, item_type * itype)
xmlChar *propValue;
int k;
skill_t sk;
int minskill = xml_ivalue(node, "minskill", 0);
int minskill = xml_ivalue(node, "minskill", 1);
int offmod = xml_ivalue(node, "offmod", 0);
int defmod = xml_ivalue(node, "defmod", 0);
int reload = xml_ivalue(node, "reload", 0);

View File

@ -51,7 +51,7 @@ static void test_rename_building(CuTest * tc)
r = findregion(-1, 0);
b = new_building(btype, r, default_locale);
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
u = test_create_unit(f, r);
u_set_building(u, b);
@ -74,7 +74,7 @@ static void test_rename_building_twice(CuTest * tc)
r = findregion(-1, 0);
b = new_building(btype, r, default_locale);
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
u = test_create_unit(f, r);
u_set_building(u, b);
@ -97,7 +97,7 @@ static void test_fishing_feeds_2_people(CuTest * tc)
test_create_world();
r = findregion(-1, 0);
CuAssertStrEquals(tc, "ocean", r->terrain->_name); /* test_create_world needs coverage */
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
u = test_create_unit(f, r);
sh = new_ship(st_find("boat"), r, 0);
u_set_ship(u, sh);
@ -139,7 +139,7 @@ static void test_fishing_does_not_give_goblins_money(CuTest * tc)
r = findregion(-1, 0);
CuAssertStrEquals(tc, "ocean", r->terrain->_name); /* test_create_world needs coverage */
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
u = test_create_unit(f, r);
sh = new_ship(st_find("boat"), r, 0);
u_set_ship(u, sh);
@ -165,7 +165,7 @@ static void test_fishing_gets_reset(CuTest * tc)
rtype = get_resourcetype(R_SILVER);
r = findregion(-1, 0);
CuAssertStrEquals(tc, "ocean", r->terrain->_name); /* test_create_world needs coverage */
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
u = test_create_unit(f, r);
sh = new_ship(st_find("boat"), r, 0);
u_set_ship(u, sh);
@ -201,7 +201,7 @@ static void test_cannot_create_unit_above_limit(CuTest * tc)
test_cleanup();
test_create_world();
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
set_param(&global.parameters, "rules.limit.faction", "4");
CuAssertIntEquals(tc, 0, checkunitnumber(f, 4));
@ -225,7 +225,7 @@ static void test_reserve_cmd(CuTest *tc) {
rtype = get_resourcetype(R_SILVER);
assert(rtype && rtype->itype);
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
r = findregion(0, 0);
assert(r && f);
u1 = test_create_unit(f, r);
@ -251,7 +251,7 @@ static void test_new_units(CuTest *tc) {
const struct locale *loc;
test_cleanup();
test_create_world();
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
r = findregion(0, 0);
assert(r && f);
u = test_create_unit(f, r);
@ -266,6 +266,102 @@ static void test_new_units(CuTest *tc) {
test_cleanup();
}
typedef struct guard_fixture {
unit * u;
} guard_fixture;
void setup_guard(guard_fixture *fix, bool armed) {
region *r;
faction *f;
unit * u;
test_cleanup();
test_create_world();
f = test_create_faction(NULL);
r = findregion(0, 0);
assert(r && f);
u = test_create_unit(f, r);
fset(u, UFL_GUARD);
u->status = ST_FIGHT;
if (armed) {
item_type *itype;
weapon_type *wtype;
itype = it_get_or_create(rt_get_or_create("sword"));
wtype = new_weapontype(itype, 0, 0.0, NULL, 0, 0, 0, SK_MELEE, 2);
i_change(&u->items, itype, 1);
set_level(u, SK_MELEE, 2);
}
fix->u = u;
}
static void test_update_guards(CuTest *tc) {
guard_fixture fix;
setup_guard(&fix, true);
update_guards();
CuAssertTrue(tc, fval(fix.u, UFL_GUARD));
freset(fix.u, UFL_GUARD);
update_guards();
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
test_cleanup();
}
static void test_newbie_cannot_guard(CuTest *tc) {
guard_fixture fix;
setup_guard(&fix, true);
set_param(&global.parameters, "NewbieImmunity", "4");
CuAssertTrue(tc, IsImmune(fix.u->faction));
update_guards();
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
test_cleanup();
}
static void test_unarmed_cannot_guard(CuTest *tc) {
guard_fixture fix;
setup_guard(&fix, false);
update_guards();
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
test_cleanup();
}
static void test_unarmed_races_can_guard(CuTest *tc) {
guard_fixture fix;
race * rc;
setup_guard(&fix, false);
rc = rc_get_or_create(fix.u->race_->_name);
rc->flags |= RCF_UNARMEDGUARD;
update_guards();
CuAssertTrue(tc, fval(fix.u, UFL_GUARD));
test_cleanup();
}
static void test_low_skill_cannot_guard(CuTest *tc) {
guard_fixture fix;
setup_guard(&fix, true);
set_level(fix.u, SK_MELEE, 1);
fix.u->status = ST_FLEE;
update_guards();
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
test_cleanup();
}
static void test_fleeing_cannot_guard(CuTest *tc) {
guard_fixture fix;
setup_guard(&fix, true);
fix.u->status = ST_FLEE;
update_guards();
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
test_cleanup();
}
static void test_reserve_self(CuTest *tc) {
unit *u1, *u2;
faction *f;
@ -279,7 +375,7 @@ static void test_reserve_self(CuTest *tc) {
rtype = get_resourcetype(R_SILVER);
assert(rtype && rtype->itype);
f = test_create_faction(rc_find("human"));
f = test_create_faction(NULL);
r = findregion(0, 0);
assert(r && f);
u1 = test_create_unit(f, r);
@ -307,6 +403,12 @@ CuSuite *get_laws_suite(void)
SUITE_ADD_TEST(suite, test_fishing_does_not_give_goblins_money);
SUITE_ADD_TEST(suite, test_fishing_gets_reset);
SUITE_ADD_TEST(suite, test_unit_limit);
SUITE_ADD_TEST(suite, test_update_guards);
SUITE_ADD_TEST(suite, test_newbie_cannot_guard);
SUITE_ADD_TEST(suite, test_unarmed_cannot_guard);
SUITE_ADD_TEST(suite, test_unarmed_races_can_guard);
SUITE_ADD_TEST(suite, test_fleeing_cannot_guard);
SUITE_ADD_TEST(suite, test_low_skill_cannot_guard);
SUITE_ADD_TEST(suite, test_reserve_self);
SUITE_ADD_TEST(suite, test_reserve_cmd);
SUITE_ADD_TEST(suite, test_new_units);