Skip to content

Commit

Permalink
refactor tests that use castles
Browse files Browse the repository at this point in the history
  • Loading branch information
ennorehling committed Mar 9, 2024
1 parent 59a0015 commit a463a8a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 83 deletions.
14 changes: 5 additions & 9 deletions src/battle.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@ static void test_select_armor(CuTest *tc) {
test_teardown();
}

static building_type * setup_castle(void) {
return test_create_castle();
}

static void test_defenders_get_building_bonus(CuTest * tc)
{
unit *du, *au;
Expand All @@ -235,7 +231,7 @@ static void test_defenders_get_building_bonus(CuTest * tc)
building_type * btype;

test_setup();
btype = setup_castle();
btype = test_create_castle();
r = test_create_plain(0, 0);
bld = test_create_building(r, btype);

Expand Down Expand Up @@ -288,7 +284,7 @@ static void test_attackers_get_no_building_bonus(CuTest * tc)

test_setup();
r = test_create_plain(0, 0);
btype = setup_castle();
btype = test_create_castle();
btype->flags |= BTF_FORTIFICATION;
bld = test_create_building(r, btype);
bld->size = 10;
Expand Down Expand Up @@ -317,7 +313,7 @@ static void test_building_bonus_respects_size(CuTest * tc)
faction * f;

test_setup();
btype = setup_castle();
btype = test_create_castle();
r = test_create_plain(0, 0);
btype->flags |= BTF_FORTIFICATION;
bld = test_create_building(r, btype);
Expand Down Expand Up @@ -347,7 +343,7 @@ static void test_building_defense_bonus(CuTest * tc)
building_type * btype;

test_setup();
btype = setup_castle();
btype = test_create_castle();

btype->maxsize = -1; /* unlimited buildings get the castle bonus */
CuAssertIntEquals(tc, 0, bt_protection(btype, 0));
Expand Down Expand Up @@ -988,7 +984,7 @@ static void test_battle_skilldiff_building(CuTest *tc)
building_type *btype;

test_setup();
btype = setup_castle();
btype = test_create_castle();

r = test_create_plain(0, 0);
ud = test_create_unit(test_create_faction(), r);
Expand Down
37 changes: 3 additions & 34 deletions src/kernel/build.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static unit * setup_build(build_fixture *bf) {

test_create_itemtype("stone");
test_create_itemtype("log");
bf->btype = test_create_buildingtype("castle");
bf->btype = test_create_castle();
bf->stype = test_create_shiptype("boat");
bf->rc = test_create_race("human");
bf->r = test_create_plain(0, 0);
Expand All @@ -67,37 +67,6 @@ static unit * setup_build(build_fixture *bf) {
return bf->u;
}

static building_type *setup_castle(item_type *it_stone) {
building_type *btype;
building_stage *stage;
construction * cons;

btype = test_create_buildingtype("castle");
stage = btype->stages = calloc(1, sizeof(building_stage));
if (!stage) abort();
cons = &stage->construction;
cons->materials = calloc(2, sizeof(requirement));
if (!cons->materials) abort();
cons->materials[0].number = 1;
cons->materials[0].rtype = it_stone->rtype;
cons->minskill = 1;
cons->maxsize = 2;
cons->reqsize = 1;
cons->skill = SK_BUILDING;
stage = stage->next = calloc(1, sizeof(building_stage));
if (!stage) abort();
cons = &stage->construction;
cons->materials = calloc(2, sizeof(requirement));
if (!cons->materials) abort();
cons->materials[0].number = 1;
cons->materials[0].rtype = it_stone->rtype;
cons->minskill = 1;
cons->maxsize = 8;
cons->reqsize = 1;
cons->skill = SK_BUILDING;
return btype;
}

static void test_build_building_stages(CuTest *tc) {
building_type *btype;
item_type *it_stone;
Expand All @@ -106,7 +75,7 @@ static void test_build_building_stages(CuTest *tc) {
test_setup();
init_resources();
it_stone = test_create_itemtype("stone");
btype = setup_castle(it_stone);
btype = test_create_castle();
u = test_create_unit(test_create_faction(), test_create_plain(0, 0));
u->building = test_create_building(u->region, btype);
u->building->size = 1;
Expand All @@ -128,7 +97,7 @@ static void test_build_building_stage_continue(CuTest *tc) {
test_setup();
init_resources();
it_stone = test_create_itemtype("stone");
btype = setup_castle(it_stone);
btype = test_create_castle();
u = test_create_unit(test_create_faction(), test_create_plain(0, 0));
set_level(u, SK_BUILDING, 2);
i_change(&u->items, it_stone, 4);
Expand Down
43 changes: 17 additions & 26 deletions src/kernel/building.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static void test_buildingtype_exists(CuTest * tc)

test_setup();

btype2 = test_create_buildingtype("castle");
btype2 = test_create_castle();
assert(btype2);
btype = test_create_buildingtype("lighhouse");
btype->maxsize = 10;
Expand Down Expand Up @@ -356,7 +356,7 @@ static void test_active_building(CuTest *tc) {

test_setup();

btype = test_create_buildingtype("castle");
btype = test_create_castle();
assert(btype && btype->maxsize == -1);
b = test_create_building(r = test_create_plain(0, 0), btype);
u = test_create_unit(test_create_faction(), r);
Expand Down Expand Up @@ -384,20 +384,20 @@ static void test_safe_building(CuTest *tc) {
unit *u1, *u2;

test_setup();
btype = test_create_buildingtype("castle");
btype = test_create_buildingtype("house");
u1 = test_create_unit(test_create_faction(), test_create_plain(0, 0));
u2 = test_create_unit(test_create_faction(), test_create_plain(0, 0));
CuAssertIntEquals(tc, false, in_safe_building(u1, u2));
CuAssertTrue(tc, !in_safe_building(u1, u2));
u1->building = test_create_building(u1->region, btype);
CuAssertIntEquals(tc, false, in_safe_building(u1, u2));
CuAssertTrue(tc, !in_safe_building(u1, u2));
btype->flags |= BTF_FORTIFICATION;
CuAssertIntEquals(tc, true, in_safe_building(u1, u2));
CuAssertTrue(tc, in_safe_building(u1, u2));
u2->building = u1->building;
CuAssertIntEquals(tc, true, in_safe_building(u1, u2));
CuAssertTrue(tc, in_safe_building(u1, u2));
u1->number = 2;
CuAssertIntEquals(tc, false, in_safe_building(u1, u2));
CuAssertTrue(tc, !in_safe_building(u1, u2));
u1->building->size = 3;
CuAssertIntEquals(tc, false, in_safe_building(u1, u2));
CuAssertTrue(tc, !in_safe_building(u1, u2));
test_teardown();
}

Expand All @@ -407,7 +407,7 @@ static void test_visible_building(CuTest *tc) {

test_setup();
bt_light = test_create_buildingtype("lighthouse");
bt_castle = test_create_buildingtype("castle");
bt_castle = test_create_castle();

b = test_create_building(test_create_plain(0, 0), bt_light);
CuAssertPtrEquals(tc, bt_light, (void *)visible_building(b));
Expand All @@ -433,8 +433,8 @@ static void test_cmp_castle_size(CuTest *tc) {

test_setup();
r = test_create_plain(0, 0);
b1 = test_create_building(r, NULL);
b2 = test_create_building(r, NULL);
b1 = test_create_building(r, test_create_castle());
b2 = test_create_building(r, test_create_castle());
u1 = test_create_unit(test_create_faction(), r);
u_set_building(u1, b1);
u2 = test_create_unit(test_create_faction(), r);
Expand All @@ -451,21 +451,12 @@ static void test_wage(CuTest *tc) {
region *r;
building *b;
building_type *btype;
struct building_stage *stage;
race *rc_orc, *rc_elf;
test_setup();
rc_orc = test_create_race("orc");
rc_elf = test_create_race("elf");
rc_elf->maintenance = 13;
btype = test_create_buildingtype("castle");
stage = btype->stages;
stage->construction.maxsize = 2; /* site */
stage = stage->next = calloc(1, sizeof(struct building_stage));
stage->construction.maxsize = 8; /* tradepost */
stage = stage->next = calloc(1, sizeof(struct building_stage));
stage->construction.maxsize = 40; /* fortification */
stage = stage->next = calloc(1, sizeof(struct building_stage));
stage->construction.maxsize = 200; /* tower */
btype = test_create_castle();
r = test_create_plain(0, 0);
CuAssertIntEquals(tc, 10, wage(r, rc_elf));
CuAssertIntEquals(tc, 10, wage(r, rc_orc));
Expand Down Expand Up @@ -524,7 +515,7 @@ static void test_cmp_wage(CuTest *tc) {
building_type *btype;

test_setup();
btype = test_create_buildingtype("castle");
btype = test_create_castle();
btype->taxes = 100;
r = test_create_plain(0, 0);
b1 = test_create_building(r, btype);
Expand All @@ -545,7 +536,7 @@ static void test_cmp_taxes(CuTest *tc) {
unit *u1, *u2;

test_setup();
btype = test_create_buildingtype("castle");
btype = test_create_castle();
btype->taxes = 100;
r = test_create_plain(0, 0);
b1 = test_create_building(r, btype);
Expand Down Expand Up @@ -576,7 +567,7 @@ static void test_cmp_current_owner(CuTest *tc) {
btype->stages->construction.maxsize = 1;
btype->taxes = 200;
b1 = test_create_building(r, btype);
btype = test_create_buildingtype("castle");
btype = test_create_castle();
btype->stages->construction.maxsize = 1;
btype->taxes = 100;
b2 = test_create_building(r, btype);
Expand All @@ -603,7 +594,7 @@ static void test_building_effsize(CuTest *tc) {
construction *cons;

test_setup();
btype = test_create_buildingtype("castle");
btype = test_create_castle();
stage = btype->stages;
assert(stage);
cons = &stage->construction;
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/region.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ static void test_region_get_owner(CuTest *tc) {

test_setup();
r = test_create_plain(0, 0);
b1 = test_create_building(r, NULL);
b2 = test_create_building(r, NULL);
b1 = test_create_building(r, test_create_castle());
b2 = test_create_building(r, test_create_castle());
b1->size = 5;
b2->size = 10;
u1 = test_create_unit(test_create_faction(), r);
Expand Down
16 changes: 8 additions & 8 deletions src/laws.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void test_rename_building(CuTest * tc)

test_setup();
test_create_locale();
btype = test_create_buildingtype("castle");
btype = test_create_castle();
r = test_create_plain(0, 0);
b = new_building(btype, r, default_locale, 1);
f = test_create_faction();
Expand All @@ -98,7 +98,7 @@ static void test_rename_building_twice(CuTest * tc)

test_setup();
test_create_locale();
btype = test_create_buildingtype("castle");
btype = test_create_castle();
r = test_create_plain(0, 0);
b = new_building(btype, r, default_locale, 1);
f = test_create_faction();
Expand All @@ -125,7 +125,7 @@ static void test_enter_building(CuTest * tc)
r = test_create_plain(0, 0);
rc = test_create_race("human");
u = test_create_unit(test_create_faction_ex(rc, NULL), r);
b = test_create_building(r, test_create_buildingtype("castle"));
b = test_create_building(r, test_create_castle());

rc->flags = RCF_WALK;
u->building = 0;
Expand Down Expand Up @@ -700,7 +700,7 @@ static void setup_pay_cmd(struct pay_fixture *fix) {
test_setup();
f = test_create_faction();
r = test_create_plain(0, 0);
btcastle = test_create_buildingtype("castle");
btcastle = test_create_castle();
btcastle->taxes = 100;
b = test_create_building(r, btcastle);
assert(b);
Expand Down Expand Up @@ -1154,7 +1154,7 @@ static void test_name_region(CuTest *tc) {
order *ord;

u = setup_name_cmd();
u_set_building(u, test_create_building(u->region, NULL));
u_set_building(u, test_create_building(u->region, test_create_castle()));
f = u->faction;

ord = create_order(K_NAME, f->locale, "%s ' Hodor Hodor '", param_name(P_REGION, f->locale));
Expand Down Expand Up @@ -1399,7 +1399,7 @@ static void test_name_cmd(CuTest *tc) {
free_order(ord);

ord = create_order(K_NAME, f->locale, "%s ' Ho\tdor '", param_name(P_BUILDING, f->locale));
u_set_building(u, test_create_building(u->region, NULL));
u_set_building(u, test_create_building(u->region, test_create_castle()));
name_cmd(u, ord);
CuAssertStrEquals(tc, "Hodor", u->building->name);
free_order(ord);
Expand Down Expand Up @@ -1445,9 +1445,9 @@ static void test_name_cmd_2274(CuTest *tc) {
u1 = test_create_unit(test_create_faction(), r);
u2 = test_create_unit(test_create_faction(), r);
u3 = test_create_unit(u2->faction, r);
u_set_building(u1, test_create_building(r, NULL));
u_set_building(u1, test_create_building(r, test_create_castle()));
u1->building->size = 10;
u_set_building(u2, test_create_building(r, NULL));
u_set_building(u2, test_create_building(r, test_create_castle()));
u2->building->size = 20;

f = u2->faction;
Expand Down
6 changes: 2 additions & 4 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,15 @@ building * test_create_building(region * r, const building_type * btype)
building * b;
assert(r);
if (!btype) {
building_type *bt_castle = test_create_buildingtype("castle");
bt_castle->flags |= BTF_FORTIFICATION;
btype = bt_castle;
btype = test_create_buildingtype("test");
}
b = new_building(btype, r, default_locale, (btype->maxsize > 0) ? btype->maxsize : 1);
return b;
}

ship * test_create_ship(region * r, const ship_type * stype)
{
ship * s = new_ship(stype ? stype : test_create_shiptype("boat"), r, default_locale);
ship * s = new_ship(stype ? stype : test_create_shiptype("test"), r, default_locale);
s->size = s->type->construction ? s->type->construction->maxsize : 1;
return s;
}
Expand Down

0 comments on commit a463a8a

Please sign in to comment.