refactor tests to use test locale where possible

This commit is contained in:
Enno Rehling 2016-03-23 23:06:45 +01:00
parent aba0be505e
commit 87b09cd3c6
5 changed files with 48 additions and 79 deletions

View File

@ -638,7 +638,7 @@ int rhorses(const region * r)
void rsetmoney(region * r, int value) void rsetmoney(region * r, int value)
{ {
assert(r->land || value==0); assert(r && (r->land || value==0));
assert(value >= 0); assert(value >= 0);
if (r->land) { if (r->land) {
r->land->money = value; r->land->money = value;

View File

@ -387,12 +387,11 @@ void test_multi_cast(CuTest *tc) {
sp->cast = cast_fireball; sp->cast = cast_fireball;
CuAssertPtrEquals(tc, sp, find_spell("fireball")); CuAssertPtrEquals(tc, sp, find_spell("fireball"));
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); lang = test_create_locale();
u->faction->locale = lang = get_or_create_locale("de");
locale_setstring(lang, parameters[P_ANY], "ALLE");
init_parameters(lang);
locale_setstring(lang, mkname("spell", sp->sname), "Feuerball"); locale_setstring(lang, mkname("spell", sp->sname), "Feuerball");
CuAssertStrEquals(tc, "Feuerball", spell_name(sp, lang)); CuAssertStrEquals(tc, "Feuerball", spell_name(sp, lang));
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
set_level(u, SK_MAGIC, 10); set_level(u, SK_MAGIC, 10);
unit_add_spell(u, 0, sp, 1); unit_add_spell(u, 0, sp, 1);
CuAssertPtrEquals(tc, sp, unit_getspell(u, "Feuerball", lang)); CuAssertPtrEquals(tc, sp, unit_getspell(u, "Feuerball", lang));

View File

@ -26,56 +26,26 @@
extern void plan_monsters(struct faction *f); extern void plan_monsters(struct faction *f);
extern int monster_attacks(unit * monster, bool respect_buildings, bool rich_only); extern int monster_attacks(unit * monster, bool respect_buildings, bool rich_only);
static void init_language(void)
{
struct locale* lang;
int i;
lang = get_or_create_locale("de");
locale_setstring(lang, "skill::unarmed", "Waffenloser Kampf");
locale_setstring(lang, "keyword::attack", "ATTACKIERE");
locale_setstring(lang, "keyword::study", "LERNE");
locale_setstring(lang, "keyword::tax", "TREIBE");
locale_setstring(lang, "keyword::loot", "PLUENDERE");
locale_setstring(lang, "keyword::piracy", "PIRATERIE");
locale_setstring(lang, "keyword::guard", "BEWACHE");
locale_setstring(lang, "keyword::move", "NACH");
locale_setstring(lang, "keyword::message", "BOTSCHAFT");
locale_setstring(lang, "REGION", "REGION");
locale_setstring(lang, "east", "O");
for (i = 0; i < MAXKEYWORDS; ++i) {
if (!locale_getstring(lang, mkname("keyword", keywords[i])))
locale_setstring(lang, mkname("keyword", keywords[i]), keywords[i]);
}
for (i = 0; i < MAXSKILLS; ++i) {
if (!locale_getstring(lang, mkname("skill", skillnames[i])))
locale_setstring(lang, mkname("skill", skillnames[i]), skillnames[i]);
}
init_keywords(lang);
init_skills(lang);
}
static order *find_order(const char *expected, const unit *unit) static order *find_order(const char *expected, const unit *unit)
{ {
char cmd[32]; char cmd[32];
order *order; order *ord;
for (order = unit->orders; order; order = order->next) { for (ord = unit->orders; ord; ord = ord->next) {
if (strcmp(expected, get_command(order, cmd, sizeof(cmd))) == 0) { if (strcmp(expected, get_command(ord, cmd, sizeof(cmd))) == 0) {
return order; return ord;
} }
} }
return NULL; return NULL;
} }
static void create_monsters(faction **player, faction **monsters, region **r, unit **u, unit **m) { static void create_monsters(faction **player, faction **monsters, unit **u, unit **m) {
race* rc; race* rc;
region *r;
test_cleanup(); test_cleanup();
init_language(); test_create_horse();
default_locale = test_create_locale();
test_create_world();
*player = test_create_faction(NULL); *player = test_create_faction(NULL);
*monsters = get_or_create_monsters(); *monsters = get_or_create_monsters();
assert(rc_find((*monsters)->race->_name)); assert(rc_find((*monsters)->race->_name));
@ -84,22 +54,22 @@ static void create_monsters(faction **player, faction **monsters, region **r, un
fset(rc, RCF_NPC); fset(rc, RCF_NPC);
fset(*monsters, FFL_NOIDLEOUT); fset(*monsters, FFL_NOIDLEOUT);
assert(fval(*monsters, FFL_NPC) && fval((*monsters)->race, RCF_UNARMEDGUARD) && fval((*monsters)->race, RCF_NPC) && fval(*monsters, FFL_NOIDLEOUT)); assert(fval(*monsters, FFL_NPC) && fval((*monsters)->race, RCF_UNARMEDGUARD) && fval((*monsters)->race, RCF_NPC) && fval(*monsters, FFL_NOIDLEOUT));
(*monsters)->locale = default_locale;
*r = findregion(0, 0); test_create_region(-1, 0, test_create_terrain("ocean", SEA_REGION | SAIL_INTO | SWIM_INTO | FLY_INTO));
test_create_region(1, 0, 0);
r = test_create_region(0, 0, 0);
*u = test_create_unit(*player, *r); *u = test_create_unit(*player, r);
unit_setid(*u, 1); unit_setid(*u, 1);
*m = test_create_unit(*monsters, *r); *m = test_create_unit(*monsters, r);
} }
static void test_monsters_attack(CuTest * tc) static void test_monsters_attack(CuTest * tc)
{ {
faction *f, *f2; faction *f, *f2;
region *r;
unit *u, *m; unit *u, *m;
create_monsters(&f, &f2, &r, &u, &m); create_monsters(&f, &f2, &u, &m);
guard(m, GUARD_TAX); guard(m, GUARD_TAX);
@ -107,7 +77,7 @@ static void test_monsters_attack(CuTest * tc)
plan_monsters(f2); plan_monsters(f2);
CuAssertPtrNotNull(tc, find_order("ATTACKIERE 1", m)); CuAssertPtrNotNull(tc, find_order("attack 1", m));
test_cleanup(); test_cleanup();
} }
@ -117,8 +87,8 @@ static void test_monsters_attack_ocean(CuTest * tc)
region *r; region *r;
unit *u, *m; unit *u, *m;
create_monsters(&f, &f2, &r, &u, &m); create_monsters(&f, &f2, &u, &m);
r = findregion(-1, 0); r = findregion(-1, 0); // ocean
u = test_create_unit(u->faction, r); u = test_create_unit(u->faction, r);
unit_setid(u, 2); unit_setid(u, 2);
m = test_create_unit(m->faction, r); m = test_create_unit(m->faction, r);
@ -128,21 +98,20 @@ static void test_monsters_attack_ocean(CuTest * tc)
plan_monsters(f2); plan_monsters(f2);
CuAssertPtrNotNull(tc, find_order("ATTACKIERE 2", m)); CuAssertPtrNotNull(tc, find_order("attack 2", m));
test_cleanup(); test_cleanup();
} }
static void test_monsters_waiting(CuTest * tc) static void test_monsters_waiting(CuTest * tc)
{ {
faction *f, *f2; faction *f, *f2;
region *r;
unit *u, *m; unit *u, *m;
create_monsters(&f, &f2, &r, &u, &m); create_monsters(&f, &f2, &u, &m);
guard(m, GUARD_TAX); guard(m, GUARD_TAX);
fset(m, UFL_ISNEW); fset(m, UFL_ISNEW);
monster_attacks(m, false, false); monster_attacks(m, false, false);
CuAssertPtrEquals(tc, 0, find_order("ATTACKIERE 1", m)); CuAssertPtrEquals(tc, 0, find_order("attack 1", m));
test_cleanup(); test_cleanup();
} }
@ -153,8 +122,8 @@ static void test_seaserpent_piracy(CuTest * tc)
unit *u, *m; unit *u, *m;
race *rc; race *rc;
create_monsters(&f, &f2, &r, &u, &m); create_monsters(&f, &f2, &u, &m);
r = findregion(-1, 0); r = findregion(-1, 0); // ocean
u = test_create_unit(u->faction, r); u = test_create_unit(u->faction, r);
unit_setid(u, 2); unit_setid(u, 2);
m = test_create_unit(m->faction, r); m = test_create_unit(m->faction, r);
@ -166,18 +135,17 @@ static void test_seaserpent_piracy(CuTest * tc)
config_set("rules.monsters.attack_chance", "1"); config_set("rules.monsters.attack_chance", "1");
plan_monsters(f2); plan_monsters(f2);
CuAssertPtrNotNull(tc, find_order("PIRATERIE", m)); CuAssertPtrNotNull(tc, find_order("piracy", m));
CuAssertPtrNotNull(tc, find_order("ATTACKIERE 2", m)); CuAssertPtrNotNull(tc, find_order("attack 2", m));
test_cleanup(); test_cleanup();
} }
static void test_monsters_attack_not(CuTest * tc) static void test_monsters_attack_not(CuTest * tc)
{ {
faction *f, *f2; faction *f, *f2;
region *r;
unit *u, *m; unit *u, *m;
create_monsters(&f, &f2, &r, &u, &m); create_monsters(&f, &f2, &u, &m);
guard(m, GUARD_TAX); guard(m, GUARD_TAX);
guard(u, GUARD_TAX); guard(u, GUARD_TAX);
@ -186,24 +154,23 @@ static void test_monsters_attack_not(CuTest * tc)
plan_monsters(f2); plan_monsters(f2);
CuAssertPtrEquals(tc, 0, find_order("ATTACKIERE 1", m)); CuAssertPtrEquals(tc, 0, find_order("attack 1", m));
test_cleanup(); test_cleanup();
} }
static void test_dragon_attacks_the_rich(CuTest * tc) static void test_dragon_attacks_the_rich(CuTest * tc)
{ {
faction *f, *f2; faction *f, *f2;
region *r;
unit *u, *m; unit *u, *m;
const item_type *i_silver; const item_type *i_silver;
init_language(); create_monsters(&f, &f2, &u, &m);
create_monsters(&f, &f2, &r, &u, &m); init_resources();
guard(m, GUARD_TAX); guard(m, GUARD_TAX);
set_level(m, SK_WEAPONLESS, 10); set_level(m, SK_WEAPONLESS, 10);
rsetmoney(r, 1); rsetmoney(findregion(0, 0), 1);
rsetmoney(findregion(1, 0), 0); rsetmoney(findregion(1, 0), 0);
i_silver = it_find("money"); i_silver = it_find("money");
assert(i_silver); assert(i_silver);
@ -213,8 +180,8 @@ static void test_dragon_attacks_the_rich(CuTest * tc)
plan_monsters(f2); plan_monsters(f2);
CuAssertPtrNotNull(tc, find_order("ATTACKIERE 1", m)); CuAssertPtrNotNull(tc, find_order("attack 1", m));
CuAssertPtrNotNull(tc, find_order("PLUENDERE", m)); CuAssertPtrNotNull(tc, find_order("loot", m));
test_cleanup(); test_cleanup();
} }
@ -224,27 +191,27 @@ static void test_dragon_moves(CuTest * tc)
region *r; region *r;
unit *u, *m; unit *u, *m;
create_monsters(&f, &f2, &r, &u, &m); create_monsters(&f, &f2, &u, &m);
rsetmoney(findregion(1, 0), 1000);
r = findregion(0, 0); // plain
rsetpeasants(r, 0); rsetpeasants(r, 0);
rsetmoney(r, 0); rsetmoney(r, 0);
rsetmoney(findregion(1, 0), 1000);
set_level(m, SK_WEAPONLESS, 10); set_level(m, SK_WEAPONLESS, 10);
config_set("rules.monsters.attack_chance", ".0"); config_set("rules.monsters.attack_chance", ".0");
plan_monsters(f2); plan_monsters(f2);
CuAssertPtrNotNull(tc, find_order("NACH O", m)); CuAssertPtrNotNull(tc, find_order("move east", m));
test_cleanup(); test_cleanup();
} }
static void test_monsters_learn_exp(CuTest * tc) static void test_monsters_learn_exp(CuTest * tc)
{ {
faction *f, *f2; faction *f, *f2;
region *r;
unit *u, *m; unit *u, *m;
skill* sk; skill* sk;
create_monsters(&f, &f2, &r, &u, &m); create_monsters(&f, &f2, &u, &m);
config_set("study.from_use", "1"); config_set("study.from_use", "1");
u_setrace(u, u_race(m)); u_setrace(u, u_race(m));

View File

@ -284,7 +284,6 @@ struct drift_fixture {
unit *u; unit *u;
terrain_type *t_ocean; terrain_type *t_ocean;
ship_type *st_boat; ship_type *st_boat;
struct locale *lang;
ship *sh; ship *sh;
}; };
@ -292,7 +291,6 @@ struct drift_fixture {
void setup_drift (struct drift_fixture *fix) { void setup_drift (struct drift_fixture *fix) {
test_cleanup(); test_cleanup();
config_set("rules.ship.storms", "0"); config_set("rules.ship.storms", "0");
fix->lang = get_or_create_locale("de");
test_create_world(); test_create_world();
test_create_shiptype("drifter"); test_create_shiptype("drifter");
@ -304,8 +302,6 @@ void setup_drift (struct drift_fixture *fix) {
set_level(fix->u, SK_SAILING, fix->st_boat->sumskill); set_level(fix->u, SK_SAILING, fix->st_boat->sumskill);
u_set_ship(fix->u, fix->sh = test_create_ship(fix->u->region, fix->st_boat)); u_set_ship(fix->u, fix->sh = test_create_ship(fix->u->region, fix->st_boat));
assert(fix->f && fix->u && fix->sh); assert(fix->f && fix->u && fix->sh);
fix->f->locale = get_or_create_locale("de");
} }
static void test_ship_no_overload(CuTest *tc) { static void test_ship_no_overload(CuTest *tc) {

View File

@ -54,7 +54,7 @@ struct region *test_create_region(int x, int y, const terrain_type *terrain)
if (!terrain) { if (!terrain) {
terrain_type *t = get_or_create_terrain("plain"); terrain_type *t = get_or_create_terrain("plain");
t->size = 1000; t->size = 1000;
fset(t, LAND_REGION|CAVALRY_REGION|FOREST_REGION); fset(t, LAND_REGION|CAVALRY_REGION|FOREST_REGION|FLY_INTO|WALK_INTO|SAIL_INTO);
terraform_region(r, t); terraform_region(r, t);
} }
else { else {
@ -86,6 +86,12 @@ struct locale * test_create_locale(void) {
for (i = 0; i <= ST_FLEE; ++i) { for (i = 0; i <= ST_FLEE; ++i) {
locale_setstring(loc, combatstatus[i], combatstatus[i]+7); locale_setstring(loc, combatstatus[i], combatstatus[i]+7);
} }
for (i = 0; i != MAXKEYWORDS; ++i) {
locale_setstring(loc, mkname("keyword", keywords[i]), keywords[i]);
}
for (i = 0; i != MAXSKILLS; ++i) {
locale_setstring(loc, mkname("skill", skillnames[i]), skillnames[i]);
}
for (i = 0; i != MAXPARAMS; ++i) { for (i = 0; i != MAXPARAMS; ++i) {
locale_setstring(loc, parameters[i], parameters[i]); locale_setstring(loc, parameters[i], parameters[i]);
test_translate_param(loc, i, parameters[i]); test_translate_param(loc, i, parameters[i]);
@ -116,6 +122,7 @@ void test_cleanup(void)
{ {
int i; int i;
default_locale = 0;
free_gamedata(); free_gamedata();
free_terrains(); free_terrains();
free_resources(); free_resources();