refactor tests for giving, add test for GIVE HERBS.

This commit is contained in:
Enno Rehling 2014-11-03 08:33:07 +01:00
parent 9a0268ac96
commit 38a0a02565
7 changed files with 132 additions and 50 deletions

View File

@ -715,7 +715,7 @@ message *check_give(const unit *u, const unit *u2, order * ord) {
return 0; return 0;
} }
static void give_cmd(unit * u, order * ord) void give_cmd(unit * u, order * ord)
{ {
region *r = u->region; region *r = u->region;
unit *u2; unit *u2;
@ -725,8 +725,10 @@ static void give_cmd(unit * u, order * ord)
param_t p; param_t p;
plane *pl; plane *pl;
message *msg; message *msg;
keyword_t kwd;
init_order(ord); kwd = init_order(ord);
assert(kwd == K_GIVE);
u2 = getunit(r, u->faction); u2 = getunit(r, u->faction);
s = getstrtoken(); s = getstrtoken();
n = s ? atoip(s) : 0; n = s ? atoip(s) : 0;

View File

@ -54,6 +54,7 @@ extern "C" {
extern int recruit_archetypes(void); extern int recruit_archetypes(void);
extern int give_control_cmd(struct unit *u, struct order *ord); extern int give_control_cmd(struct unit *u, struct order *ord);
extern void give_control(struct unit * u, struct unit * u2); extern void give_control(struct unit * u, struct unit * u2);
void give_cmd(struct unit * u, struct order * ord);
struct message * check_steal(const struct unit * u, struct order *ord); struct message * check_steal(const struct unit * u, struct order *ord);
struct message * check_give(const struct unit * u, const struct unit * u2, struct order *ord); struct message * check_give(const struct unit * u, const struct unit * u2, struct order *ord);

View File

@ -63,7 +63,7 @@ struct steal {
struct faction *f; struct faction *f;
}; };
static void setup_steal(struct steal *env, terrain_type *ter, race *rc) { static void setup_steal(struct steal *env, struct terrain_type *ter, struct race *rc) {
env->r = test_create_region(0, 0, ter); env->r = test_create_region(0, 0, ter);
env->f = test_create_faction(rc); env->f = test_create_faction(rc);
env->u = test_create_unit(env->f, env->r); env->u = test_create_unit(env->f, env->r);
@ -72,7 +72,7 @@ static void setup_steal(struct steal *env, terrain_type *ter, race *rc) {
static void test_steal_okay(CuTest * tc) { static void test_steal_okay(CuTest * tc) {
struct steal env; struct steal env;
race *rc; race *rc;
terrain_type *ter; struct terrain_type *ter;
test_cleanup(); test_cleanup();
ter = test_create_terrain("plain", LAND_REGION); ter = test_create_terrain("plain", LAND_REGION);
@ -114,50 +114,6 @@ static void test_steal_ocean(CuTest * tc) {
test_cleanup(); test_cleanup();
} }
struct give {
struct unit *src, *dst;
struct region *r;
struct faction *f1, *f2;
};
static void setup_give(struct give *env) {
terrain_type *ter = test_create_terrain("plain", LAND_REGION);
env->r = test_create_region(0, 0, ter);
env->src = test_create_unit(env->f1, env->r);
env->dst = test_create_unit(env->f2, env->r);
}
static void test_give_okay(CuTest * tc) {
struct give env;
struct race * rc;
test_cleanup();
rc = test_create_race("human");
env.f2 = env.f1 = test_create_faction(rc);
setup_give(&env);
set_param(&global.parameters, "rules.give", "0");
CuAssertPtrEquals(tc, 0, check_give(env.src, env.dst, 0));
test_cleanup();
}
static void test_give_denied_by_rules(CuTest * tc) {
struct give env;
struct race * rc;
struct message *msg;
test_cleanup();
rc = test_create_race("human");
env.f1 = test_create_faction(rc);
env.f2 = test_create_faction(rc);
setup_give(&env);
set_param(&global.parameters, "rules.give", "0");
CuAssertPtrNotNull(tc, msg=check_give(env.src, env.dst, 0));
msg_release(msg);
test_cleanup();
}
CuSuite *get_economy_suite(void) CuSuite *get_economy_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -166,7 +122,5 @@ CuSuite *get_economy_suite(void)
SUITE_ADD_TEST(suite, test_steal_okay); SUITE_ADD_TEST(suite, test_steal_okay);
SUITE_ADD_TEST(suite, test_steal_ocean); SUITE_ADD_TEST(suite, test_steal_ocean);
SUITE_ADD_TEST(suite, test_steal_nosteal); SUITE_ADD_TEST(suite, test_steal_nosteal);
SUITE_ADD_TEST(suite, test_give_okay);
SUITE_ADD_TEST(suite, test_give_denied_by_rules);
return suite; return suite;
} }

View File

@ -16,6 +16,10 @@
extern "C" { extern "C" {
#endif #endif
struct item_type;
struct order;
struct unit;
extern int give_item(int want, const struct item_type *itype, extern int give_item(int want, const struct item_type *itype,
struct unit *src, struct unit *dest, struct order *ord); struct unit *src, struct unit *dest, struct order *ord);
extern void give_men(int n, struct unit *u, struct unit *u2, extern void give_men(int n, struct unit *u, struct unit *u2,

117
src/give.test.c Normal file
View File

@ -0,0 +1,117 @@
#include <platform.h>
#include "give.h"
#include "economy.h"
#include <kernel/config.h>
#include <kernel/item.h>
#include <kernel/terrain.h>
#include <kernel/order.h>
#include <kernel/unit.h>
#include <kernel/faction.h>
#include <util/base36.h>
#include <util/language.h>
#include <util/message.h>
#include <CuTest.h>
#include <tests.h>
#include <stdio.h>
#include <assert.h>
struct give {
struct unit *src, *dst;
struct region *r;
struct faction *f1, *f2;
struct item_type * itype;
};
static void setup_give(struct give *env) {
struct terrain_type *ter = test_create_terrain("plain", LAND_REGION);
env->r = test_create_region(0, 0, ter);
env->src = test_create_unit(env->f1, env->r);
env->dst = test_create_unit(env->f2, env->r);
env->itype = it_get_or_create(rt_get_or_create("money"));
env->itype->flags |= ITF_HERB;
}
static void test_give(CuTest * tc) {
struct give env;
test_cleanup();
env.f2 = env.f1 = test_create_faction(0);
setup_give(&env);
i_change(&env.src->items, env.itype, 10);
CuAssertIntEquals(tc, 0, give_item(10, env.itype, env.src, env.dst, 0));
CuAssertIntEquals(tc, 0, i_get(env.src->items, env.itype));
CuAssertIntEquals(tc, 10, i_get(env.dst->items, env.itype));
CuAssertIntEquals(tc, -1, give_item(10, env.itype, env.src, env.dst, 0));
CuAssertIntEquals(tc, 0, i_get(env.src->items, env.itype));
CuAssertIntEquals(tc, 10, i_get(env.dst->items, env.itype));
test_cleanup();
}
static void test_give_herbs(CuTest * tc) {
struct give env;
struct order *ord;
struct locale * lang;
char cmd[32];
test_cleanup();
test_create_world();
env.f2 = env.f1 = test_create_faction(0);
setup_give(&env);
i_change(&env.src->items, env.itype, 10);
lang = get_or_create_locale("test");
env.f1->locale = lang;
locale_setstring(lang, "KRAEUTER", "HERBS");
init_locale(lang);
_snprintf(cmd, sizeof(cmd), "%s HERBS", itoa36(env.dst->no));
ord = create_order(K_GIVE, lang, cmd);
assert(ord);
give_cmd(env.src, ord);
CuAssertIntEquals(tc, 0, i_get(env.src->items, env.itype));
CuAssertIntEquals(tc, 10, i_get(env.dst->items, env.itype));
test_cleanup();
}
static void test_give_okay(CuTest * tc) {
struct give env;
test_cleanup();
env.f2 = env.f1 = test_create_faction(0);
setup_give(&env);
set_param(&global.parameters, "rules.give", "0");
CuAssertPtrEquals(tc, 0, check_give(env.src, env.dst, 0));
test_cleanup();
}
static void test_give_denied_by_rules(CuTest * tc) {
struct give env;
struct message *msg;
test_cleanup();
env.f1 = test_create_faction(0);
env.f2 = test_create_faction(0);
setup_give(&env);
set_param(&global.parameters, "rules.give", "0");
CuAssertPtrNotNull(tc, msg = check_give(env.src, env.dst, 0));
msg_release(msg);
test_cleanup();
}
CuSuite *get_give_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_give);
SUITE_ADD_TEST(suite, test_give_herbs);
SUITE_ADD_TEST(suite, test_give_okay);
SUITE_ADD_TEST(suite, test_give_denied_by_rules);
return suite;
}

View File

@ -18,6 +18,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef H_GC_LAWS #ifndef H_GC_LAWS
#define H_GC_LAWS #define H_GC_LAWS
#include <kernel/types.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -57,6 +57,7 @@ int RunAllTests(void)
/* gamecode */ /* gamecode */
ADD_TESTS(suite, battle); ADD_TESTS(suite, battle);
ADD_TESTS(suite, economy); ADD_TESTS(suite, economy);
ADD_TESTS(suite, give);
ADD_TESTS(suite, laws); ADD_TESTS(suite, laws);
ADD_TESTS(suite, market); ADD_TESTS(suite, market);
ADD_TESTS(suite, move); ADD_TESTS(suite, move);