From d73a629e79ed7a7af09792671c9021a43c044c31 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 20:01:44 +0100 Subject: [PATCH] add a test for regular give_cmd. also, speed up the function a little bit. --- src/give.c | 11 +++++--- src/give.test.c | 70 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/give.c b/src/give.c index 8f87cded3..b973b90f3 100644 --- a/src/give.c +++ b/src/give.c @@ -600,16 +600,19 @@ void give_cmd(unit * u, order * ord) } if (u2 && u_race(u2) == get_race(RC_SPELL)) { - ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "feedback_unit_not_found", - "")); + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, + "feedback_unit_not_found", "")); return; } - else if (u2 && !alliedunit(u2, u->faction, HELP_GIVE) && !ucontact(u2, u)) { cmistake(u, ord, 40, MSG_COMMERCE); return; } - + else if (p == NOKEYWORD) { + /* the most likely case: giving items to someone. + * let's catch this and save ourselves the rest of the param_t checks. + */ + } else if (p == P_HERBS) { bool given = false; if ((u_race(u)->ec_flags & ECF_KEEP_ITEM) && u2 != NULL) { diff --git a/src/give.test.c b/src/give.test.c index c719ddd5c..0d6149750 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -23,6 +23,7 @@ #include struct give { + struct locale * lang; struct unit *src, *dst; struct region *r; struct faction *f1, *f2; @@ -46,10 +47,15 @@ static void setup_give(struct give *env) { ally * al = ally_add(&env->f2->allies, env->f1); al->status = HELP_GIVE; } + if (env->lang) { + locale_setstring(env->lang, env->itype->rtype->_name, "SILBER"); + init_locale(env->lang); + env->f1->locale = env->lang; + } } static void test_give_unit_to_peasants(CuTest * tc) { - struct give env; + struct give env = { 0 }; test_setup(); env.f1 = test_create_faction(0); env.f2 = 0; @@ -62,7 +68,7 @@ static void test_give_unit_to_peasants(CuTest * tc) { } static void test_give_unit(CuTest * tc) { - struct give env; + struct give env = { 0 }; test_setup(); env.f1 = test_create_faction(0); env.f2 = test_create_faction(0); @@ -81,7 +87,7 @@ static void test_give_unit(CuTest * tc) { } static void test_give_unit_in_ocean(CuTest * tc) { - struct give env; + struct give env = { 0 }; test_setup(); env.f1 = test_create_faction(0); env.f2 = 0; @@ -93,7 +99,7 @@ static void test_give_unit_in_ocean(CuTest * tc) { } static void test_give_men(CuTest * tc) { - struct give env; + struct give env = { 0 }; test_setup(); env.f2 = env.f1 = test_create_faction(0); setup_give(&env); @@ -104,7 +110,7 @@ static void test_give_men(CuTest * tc) { } static void test_give_men_magicians(CuTest * tc) { - struct give env; + struct give env = { 0 }; int p; message * msg; @@ -135,7 +141,7 @@ static void test_give_men_magicians(CuTest * tc) { } static void test_give_men_limit(CuTest * tc) { - struct give env; + struct give env = { 0 }; message *msg; test_setup(); env.f2 = test_create_faction(0); @@ -165,7 +171,7 @@ static void test_give_men_limit(CuTest * tc) { } static void test_give_men_in_ocean(CuTest * tc) { - struct give env; + struct give env = { 0 }; message * msg; test_setup(); @@ -181,7 +187,7 @@ static void test_give_men_in_ocean(CuTest * tc) { } static void test_give_men_too_many(CuTest * tc) { - struct give env; + struct give env = { 0 }; test_setup(); env.f2 = env.f1 = test_create_faction(0); setup_give(&env); @@ -192,7 +198,7 @@ static void test_give_men_too_many(CuTest * tc) { } static void test_give_men_none(CuTest * tc) { - struct give env; + struct give env = { 0 }; message * msg; test_setup(); @@ -207,7 +213,7 @@ static void test_give_men_none(CuTest * tc) { } static void test_give_men_other_faction(CuTest * tc) { - struct give env; + struct give env = { 0 }; message * msg; test_setup(); @@ -224,7 +230,7 @@ static void test_give_men_other_faction(CuTest * tc) { } static void test_give_men_requires_contact(CuTest * tc) { - struct give env; + struct give env = { 0 }; message * msg; order *ord; char cmd[32]; @@ -251,7 +257,7 @@ static void test_give_men_requires_contact(CuTest * tc) { } static void test_give_men_not_to_self(CuTest * tc) { - struct give env; + struct give env = { 0 }; message * msg; test_setup(); env.f2 = env.f1 = test_create_faction(0); @@ -264,7 +270,7 @@ static void test_give_men_not_to_self(CuTest * tc) { } static void test_give_peasants(CuTest * tc) { - struct give env; + struct give env = { 0 }; message * msg; test_setup(); @@ -281,7 +287,7 @@ static void test_give_peasants(CuTest * tc) { } static void test_give(CuTest * tc) { - struct give env; + struct give env = { 0 }; test_setup(); env.f2 = env.f1 = test_create_faction(0); @@ -298,8 +304,31 @@ static void test_give(CuTest * tc) { test_cleanup(); } +static void test_give_cmd(CuTest * tc) { + struct give env = { 0 }; + struct order *ord; + char cmd[32]; + + test_setup(); + env.lang = test_create_locale(); + env.f2 = env.f1 = test_create_faction(0); + setup_give(&env); + + i_change(&env.src->items, env.itype, 10); + + _snprintf(cmd, sizeof(cmd), "%s 5 %s", itoa36(env.dst->no), LOC(env.f1->locale, env.itype->rtype->_name)); + ord = create_order(K_GIVE, env.f1->locale, cmd); + assert(ord); + give_cmd(env.src, ord); + CuAssertIntEquals(tc, 5, i_get(env.src->items, env.itype)); + CuAssertIntEquals(tc, 5, i_get(env.dst->items, env.itype)); + + free_order(ord); + test_cleanup(); +} + static void test_give_herbs(CuTest * tc) { - struct give env; + struct give env = { 0 }; struct order *ord; char cmd[32]; @@ -321,7 +350,7 @@ static void test_give_herbs(CuTest * tc) { } static void test_give_okay(CuTest * tc) { - struct give env; + struct give env = { 0 }; test_setup(); env.f2 = env.f1 = test_create_faction(0); @@ -333,7 +362,7 @@ static void test_give_okay(CuTest * tc) { } static void test_give_denied_by_rules(CuTest * tc) { - struct give env; + struct give env = { 0 }; struct message *msg; test_setup(); @@ -348,7 +377,7 @@ static void test_give_denied_by_rules(CuTest * tc) { } static void test_give_dead_unit(CuTest * tc) { - struct give env; + struct give env = { 0 }; struct message *msg; test_setup(); @@ -363,7 +392,7 @@ static void test_give_dead_unit(CuTest * tc) { } static void test_give_new_unit(CuTest * tc) { - struct give env; + struct give env = { 0 }; test_setup(); env.f1 = test_create_faction(0); @@ -377,7 +406,7 @@ static void test_give_new_unit(CuTest * tc) { static void test_give_invalid_target(CuTest *tc) { // bug https://bugs.eressea.de/view.php?id=1685 - struct give env; + struct give env = { 0 }; order *ord; test_setup(); @@ -400,6 +429,7 @@ CuSuite *get_give_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_give); + SUITE_ADD_TEST(suite, test_give_cmd); SUITE_ADD_TEST(suite, test_give_men); SUITE_ADD_TEST(suite, test_give_men_magicians); SUITE_ADD_TEST(suite, test_give_men_limit);