From 87b09cd3c658d0345754afb1f9760e33818e06f8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 23 Mar 2016 23:06:45 +0100 Subject: [PATCH] refactor tests to use test locale where possible --- src/kernel/region.c | 2 +- src/magic.test.c | 7 ++- src/monsters.test.c | 105 +++++++++++++++----------------------------- src/move.test.c | 4 -- src/tests.c | 9 +++- 5 files changed, 48 insertions(+), 79 deletions(-) diff --git a/src/kernel/region.c b/src/kernel/region.c index c3d0559bf..c4e2042e4 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -638,7 +638,7 @@ int rhorses(const region * r) void rsetmoney(region * r, int value) { - assert(r->land || value==0); + assert(r && (r->land || value==0)); assert(value >= 0); if (r->land) { r->land->money = value; diff --git a/src/magic.test.c b/src/magic.test.c index ad698625a..ff6cb1e29 100644 --- a/src/magic.test.c +++ b/src/magic.test.c @@ -387,12 +387,11 @@ void test_multi_cast(CuTest *tc) { sp->cast = cast_fireball; CuAssertPtrEquals(tc, sp, find_spell("fireball")); - u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); - u->faction->locale = lang = get_or_create_locale("de"); - locale_setstring(lang, parameters[P_ANY], "ALLE"); - init_parameters(lang); + lang = test_create_locale(); locale_setstring(lang, mkname("spell", sp->sname), "Feuerball"); 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); unit_add_spell(u, 0, sp, 1); CuAssertPtrEquals(tc, sp, unit_getspell(u, "Feuerball", lang)); diff --git a/src/monsters.test.c b/src/monsters.test.c index c619ecf2a..1ccd1d18d 100644 --- a/src/monsters.test.c +++ b/src/monsters.test.c @@ -26,56 +26,26 @@ extern void plan_monsters(struct faction *f); 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) { char cmd[32]; - order *order; - for (order = unit->orders; order; order = order->next) { - if (strcmp(expected, get_command(order, cmd, sizeof(cmd))) == 0) { - return order; + order *ord; + for (ord = unit->orders; ord; ord = ord->next) { + if (strcmp(expected, get_command(ord, cmd, sizeof(cmd))) == 0) { + return ord; } } 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; + region *r; test_cleanup(); - init_language(); - - test_create_world(); + test_create_horse(); + default_locale = test_create_locale(); *player = test_create_faction(NULL); *monsters = get_or_create_monsters(); 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(*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); - *m = test_create_unit(*monsters, *r); + *m = test_create_unit(*monsters, r); } static void test_monsters_attack(CuTest * tc) { faction *f, *f2; - region *r; unit *u, *m; - create_monsters(&f, &f2, &r, &u, &m); + create_monsters(&f, &f2, &u, &m); guard(m, GUARD_TAX); @@ -107,7 +77,7 @@ static void test_monsters_attack(CuTest * tc) plan_monsters(f2); - CuAssertPtrNotNull(tc, find_order("ATTACKIERE 1", m)); + CuAssertPtrNotNull(tc, find_order("attack 1", m)); test_cleanup(); } @@ -117,8 +87,8 @@ static void test_monsters_attack_ocean(CuTest * tc) region *r; unit *u, *m; - create_monsters(&f, &f2, &r, &u, &m); - r = findregion(-1, 0); + create_monsters(&f, &f2, &u, &m); + r = findregion(-1, 0); // ocean u = test_create_unit(u->faction, r); unit_setid(u, 2); m = test_create_unit(m->faction, r); @@ -128,21 +98,20 @@ static void test_monsters_attack_ocean(CuTest * tc) plan_monsters(f2); - CuAssertPtrNotNull(tc, find_order("ATTACKIERE 2", m)); + CuAssertPtrNotNull(tc, find_order("attack 2", m)); test_cleanup(); } static void test_monsters_waiting(CuTest * tc) { faction *f, *f2; - region *r; unit *u, *m; - create_monsters(&f, &f2, &r, &u, &m); + create_monsters(&f, &f2, &u, &m); guard(m, GUARD_TAX); fset(m, UFL_ISNEW); monster_attacks(m, false, false); - CuAssertPtrEquals(tc, 0, find_order("ATTACKIERE 1", m)); + CuAssertPtrEquals(tc, 0, find_order("attack 1", m)); test_cleanup(); } @@ -153,8 +122,8 @@ static void test_seaserpent_piracy(CuTest * tc) unit *u, *m; race *rc; - create_monsters(&f, &f2, &r, &u, &m); - r = findregion(-1, 0); + create_monsters(&f, &f2, &u, &m); + r = findregion(-1, 0); // ocean u = test_create_unit(u->faction, r); unit_setid(u, 2); 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"); plan_monsters(f2); - CuAssertPtrNotNull(tc, find_order("PIRATERIE", m)); - CuAssertPtrNotNull(tc, find_order("ATTACKIERE 2", m)); + CuAssertPtrNotNull(tc, find_order("piracy", m)); + CuAssertPtrNotNull(tc, find_order("attack 2", m)); test_cleanup(); } static void test_monsters_attack_not(CuTest * tc) { faction *f, *f2; - region *r; unit *u, *m; - create_monsters(&f, &f2, &r, &u, &m); + create_monsters(&f, &f2, &u, &m); guard(m, GUARD_TAX); guard(u, GUARD_TAX); @@ -186,24 +154,23 @@ static void test_monsters_attack_not(CuTest * tc) plan_monsters(f2); - CuAssertPtrEquals(tc, 0, find_order("ATTACKIERE 1", m)); + CuAssertPtrEquals(tc, 0, find_order("attack 1", m)); test_cleanup(); } static void test_dragon_attacks_the_rich(CuTest * tc) { faction *f, *f2; - region *r; unit *u, *m; const item_type *i_silver; - init_language(); - create_monsters(&f, &f2, &r, &u, &m); + create_monsters(&f, &f2, &u, &m); + init_resources(); guard(m, GUARD_TAX); set_level(m, SK_WEAPONLESS, 10); - rsetmoney(r, 1); + rsetmoney(findregion(0, 0), 1); rsetmoney(findregion(1, 0), 0); i_silver = it_find("money"); assert(i_silver); @@ -213,8 +180,8 @@ static void test_dragon_attacks_the_rich(CuTest * tc) plan_monsters(f2); - CuAssertPtrNotNull(tc, find_order("ATTACKIERE 1", m)); - CuAssertPtrNotNull(tc, find_order("PLUENDERE", m)); + CuAssertPtrNotNull(tc, find_order("attack 1", m)); + CuAssertPtrNotNull(tc, find_order("loot", m)); test_cleanup(); } @@ -224,27 +191,27 @@ static void test_dragon_moves(CuTest * tc) region *r; 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); rsetmoney(r, 0); - rsetmoney(findregion(1, 0), 1000); set_level(m, SK_WEAPONLESS, 10); config_set("rules.monsters.attack_chance", ".0"); plan_monsters(f2); - CuAssertPtrNotNull(tc, find_order("NACH O", m)); + CuAssertPtrNotNull(tc, find_order("move east", m)); test_cleanup(); } static void test_monsters_learn_exp(CuTest * tc) { faction *f, *f2; - region *r; unit *u, *m; skill* sk; - create_monsters(&f, &f2, &r, &u, &m); + create_monsters(&f, &f2, &u, &m); config_set("study.from_use", "1"); u_setrace(u, u_race(m)); diff --git a/src/move.test.c b/src/move.test.c index ade4c4230..4c0a07bc5 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -284,7 +284,6 @@ struct drift_fixture { unit *u; terrain_type *t_ocean; ship_type *st_boat; - struct locale *lang; ship *sh; }; @@ -292,7 +291,6 @@ struct drift_fixture { void setup_drift (struct drift_fixture *fix) { test_cleanup(); config_set("rules.ship.storms", "0"); - fix->lang = get_or_create_locale("de"); test_create_world(); 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); u_set_ship(fix->u, fix->sh = test_create_ship(fix->u->region, fix->st_boat)); assert(fix->f && fix->u && fix->sh); - fix->f->locale = get_or_create_locale("de"); - } static void test_ship_no_overload(CuTest *tc) { diff --git a/src/tests.c b/src/tests.c index f6a798b7d..a0d54a4cc 100644 --- a/src/tests.c +++ b/src/tests.c @@ -54,7 +54,7 @@ struct region *test_create_region(int x, int y, const terrain_type *terrain) if (!terrain) { terrain_type *t = get_or_create_terrain("plain"); 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); } else { @@ -86,6 +86,12 @@ struct locale * test_create_locale(void) { for (i = 0; i <= ST_FLEE; ++i) { 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) { locale_setstring(loc, parameters[i], parameters[i]); test_translate_param(loc, i, parameters[i]); @@ -116,6 +122,7 @@ void test_cleanup(void) { int i; + default_locale = 0; free_gamedata(); free_terrains(); free_resources();