add simple tests for pay_cmd.

This commit is contained in:
Enno Rehling 2014-12-17 19:53:19 +01:00
parent 916271ef3a
commit bd6b9761b0
4 changed files with 76 additions and 7 deletions

View File

@ -21,12 +21,10 @@ static void test_getunit(CuTest *tc) {
struct region *r;
struct locale *lang;
struct terrain_type *t_plain;
struct critbit_tree ** cb;
test_cleanup();
lang = get_or_create_locale("de");
cb = (struct critbit_tree **)get_translations(lang, UT_PARAMS);
add_translation(cb, "TEMP", P_TEMP);
test_translate_param(lang, P_TEMP, "TEMP");
/* note that the english order is FIGHT, not COMBAT, so this is a poor example */
t_plain = test_create_terrain("plain", LAND_REGION);
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, t_plain));

View File

@ -329,7 +329,6 @@ static void test_reserve_cmd(CuTest *tc) {
region *r;
order *ord;
const resource_type *rtype;
const struct locale *loc;
test_cleanup();
test_create_world();
@ -342,9 +341,7 @@ static void test_reserve_cmd(CuTest *tc) {
u1 = test_create_unit(f, r);
u2 = test_create_unit(f, r);
assert(u1 && u2);
loc = get_locale("de");
assert(loc);
ord = create_order(K_RESERVE, loc, "200 SILBER");
ord = create_order(K_RESERVE, f->locale, "200 SILBER");
assert(ord);
i_change(&u1->items, rtype->itype, 100);
i_change(&u2->items, rtype->itype, 100);
@ -354,6 +351,68 @@ static void test_reserve_cmd(CuTest *tc) {
test_cleanup();
}
struct pay_fixture {
unit *u1;
unit *u2;
};
static void setup_pay_cmd(struct pay_fixture *fix) {
faction *f;
region *r;
building *b;
test_create_world();
f = test_create_faction(NULL);
r = findregion(0, 0);
assert(r && f);
b = test_create_building(r, bt_get_or_create("lighthouse"));
assert(b);
fix->u1 = test_create_unit(f, r);
fix->u2 = test_create_unit(f, r);
assert(fix->u1 && fix->u2);
u_set_building(fix->u1, b);
u_set_building(fix->u2, b);
assert(building_owner(b) == fix->u1);
test_translate_param(f->locale, P_NOT, "NOT");
}
static void test_pay_cmd(CuTest *tc) {
struct pay_fixture fix;
order *ord;
faction *f;
building *b;
test_cleanup();
setup_pay_cmd(&fix);
b = fix.u1->building;
f = fix.u1->faction;
ord = create_order(K_PAY, f->locale, "NOT");
assert(ord);
CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord));
CuAssertIntEquals(tc, BLD_DONTPAY, b->flags&BLD_DONTPAY);
test_cleanup();
}
static void test_pay_cmd_must_be_owner(CuTest *tc) {
struct pay_fixture fix;
order *ord;
faction *f;
building *b;
test_cleanup();
setup_pay_cmd(&fix);
b = fix.u1->building;
f = fix.u1->faction;
ord = create_order(K_PAY, f->locale, "NOT");
assert(ord);
CuAssertIntEquals(tc, 0, pay_cmd(fix.u2, ord));
CuAssertIntEquals(tc, 0, b->flags&BLD_DONTPAY);
test_cleanup();
}
static void test_new_units(CuTest *tc) {
unit *u;
faction *f;
@ -521,6 +580,8 @@ CuSuite *get_laws_suite(void)
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_pay_cmd);
SUITE_ADD_TEST(suite, test_pay_cmd_must_be_owner);
SUITE_ADD_TEST(suite, test_new_units);
SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit);
SUITE_ADD_TEST(suite, test_contact);

View File

@ -132,6 +132,14 @@ item_type * test_create_itemtype(const char * name) {
return itype;
}
void test_translate_param(const struct locale *lang, param_t param, const char *text) {
struct critbit_tree **cb;
assert(lang && text);
cb = (struct critbit_tree **)get_translations(lang, UT_PARAMS);
add_translation(cb, text, param);
}
/** creates a small world and some stuff in it.
* two terrains: 'plain' and 'ocean'
* one race: 'human'

View File

@ -1,6 +1,7 @@
#ifndef ERESSEA_TESTS_H
#define ERESSEA_TESTS_H
#include <kernel/types.h>
#include <stdlib.h>
#ifdef __cplusplus
@ -33,6 +34,7 @@ extern "C" {
struct building_type *test_create_buildingtype(const char *name);
int RunAllTests(void);
void test_translate_param(const struct locale *lang, param_t param, const char *text);
#ifdef __cplusplus
}