diff --git a/src/economy.c b/src/economy.c index 240b5712a..295d78ce5 100644 --- a/src/economy.c +++ b/src/economy.c @@ -715,7 +715,7 @@ message *check_give(const unit *u, const unit *u2, order * ord) { return 0; } -static void give_cmd(unit * u, order * ord) +void give_cmd(unit * u, order * ord) { region *r = u->region; unit *u2; @@ -725,8 +725,10 @@ static void give_cmd(unit * u, order * ord) param_t p; plane *pl; message *msg; + keyword_t kwd; - init_order(ord); + kwd = init_order(ord); + assert(kwd == K_GIVE); u2 = getunit(r, u->faction); s = getstrtoken(); n = s ? atoip(s) : 0; diff --git a/src/economy.h b/src/economy.h index 58891f188..da5e8e299 100644 --- a/src/economy.h +++ b/src/economy.h @@ -54,6 +54,7 @@ extern "C" { extern int recruit_archetypes(void); extern int give_control_cmd(struct unit *u, struct order *ord); 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_give(const struct unit * u, const struct unit * u2, struct order *ord); diff --git a/src/economy.test.c b/src/economy.test.c index 0374d4f45..b43a6d1fc 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -63,7 +63,7 @@ struct steal { 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->f = test_create_faction(rc); 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) { struct steal env; race *rc; - terrain_type *ter; + struct terrain_type *ter; test_cleanup(); ter = test_create_terrain("plain", LAND_REGION); @@ -114,50 +114,6 @@ static void test_steal_ocean(CuTest * tc) { 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 *suite = CuSuiteNew(); @@ -166,7 +122,5 @@ CuSuite *get_economy_suite(void) SUITE_ADD_TEST(suite, test_steal_okay); SUITE_ADD_TEST(suite, test_steal_ocean); 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; } diff --git a/src/give.h b/src/give.h index 9d2553e5e..1fec3af81 100644 --- a/src/give.h +++ b/src/give.h @@ -16,6 +16,10 @@ extern "C" { #endif + struct item_type; + struct order; + struct unit; + extern int give_item(int want, const struct item_type *itype, struct unit *src, struct unit *dest, struct order *ord); extern void give_men(int n, struct unit *u, struct unit *u2, diff --git a/src/give.test.c b/src/give.test.c new file mode 100644 index 000000000..ea5237e65 --- /dev/null +++ b/src/give.test.c @@ -0,0 +1,117 @@ +#include + +#include "give.h" +#include "economy.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +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; +} diff --git a/src/laws.h b/src/laws.h index 02fdc6c09..869534212 100755 --- a/src/laws.h +++ b/src/laws.h @@ -18,6 +18,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #ifndef H_GC_LAWS #define H_GC_LAWS + +#include + #ifdef __cplusplus extern "C" { #endif diff --git a/src/test_eressea.c b/src/test_eressea.c index 17958c7b2..0ddfd5c71 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -57,6 +57,7 @@ int RunAllTests(void) /* gamecode */ ADD_TESTS(suite, battle); ADD_TESTS(suite, economy); + ADD_TESTS(suite, give); ADD_TESTS(suite, laws); ADD_TESTS(suite, market); ADD_TESTS(suite, move);