From a9f3592228ae47900eea2770d3da5afddf9245e5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 2 Dec 2018 15:51:35 +0100 Subject: [PATCH] Bug 2525: failing test for default orders. --- src/orderfile.c | 12 ++++------ src/orderfile.test.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/util/keyword.c | 5 ++++ src/util/keyword.h | 1 + 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/orderfile.c b/src/orderfile.c index 90b44477b..26ae34ae7 100644 --- a/src/orderfile.c +++ b/src/orderfile.c @@ -47,13 +47,12 @@ static void begin_orders(unit *u) { u->orders = NULL; } -static unit *unitorders(input *in, faction *f) +static void unitorders(input *in, faction *f) { int i; unit *u; - if (!f) - return NULL; + assert(f); i = getid(); u = findunit(i); @@ -120,10 +119,6 @@ static unit *unitorders(input *in, faction *f) } } - else { - return NULL; - } - return u; } static faction *factionorders(void) @@ -189,7 +184,8 @@ int read_orders(input *in) * vermerkt. */ case P_UNIT: - if (!f || !unitorders(in, f)) { + if (f) { + unitorders(in, f); do { b = in->getbuf(in->data); if (!b) { diff --git a/src/orderfile.test.c b/src/orderfile.test.c index ca98fdeb0..9b43795ff 100644 --- a/src/orderfile.test.c +++ b/src/orderfile.test.c @@ -3,9 +3,20 @@ #include "orderfile.h" +#include "direction.h" + #include #include +#include +#include + +#include +#include +#include +#include #include +#include +#include #include #include @@ -24,6 +35,49 @@ static void test_read_orders(CuTest *tc) { test_teardown(); } +static const char *getbuf_strings(void *data) +{ + strlist **listp = (strlist **)data; + if (listp && *listp) { + strlist *list = *listp; + *listp = list->next; + return list->s; + } + return NULL; +} + +static void test_unit_orders(CuTest *tc) { + input in; + unit *u; + faction *f; + strlist *orders = NULL; + char buf[64]; + + test_setup(); + u = test_create_unit(f = test_create_faction(NULL), test_create_plain(0, 0)); + f->locale = test_create_locale(); + u->orders = create_order(K_ENTERTAIN, f->locale, NULL); + faction_setpassword(f, password_hash("password", PASSWORD_DEFAULT)); + snprintf(buf, sizeof(buf), "%s %s %s", + LOC(f->locale, parameters[P_FACTION]), itoa36(f->no), "password"); + addstrlist(&orders, buf); + snprintf(buf, sizeof(buf), "%s %s", + LOC(f->locale, parameters[P_UNIT]), itoa36(u->no)); + addstrlist(&orders, buf); + snprintf(buf, sizeof(buf), "%s %s", keyword_name(K_MOVE, f->locale), + LOC(f->locale, shortdirections[D_WEST])); + addstrlist(&orders, buf); + in.data = &orders; + in.getbuf = getbuf_strings; + CuAssertIntEquals(tc, 0, read_orders(&in)); + CuAssertPtrNotNull(tc, u->old_orders); + CuAssertPtrNotNull(tc, u->orders); + CuAssertIntEquals(tc, K_MOVE, getkeyword(u->orders)); + CuAssertIntEquals(tc, K_ENTERTAIN, getkeyword(u->old_orders)); + CuAssertIntEquals(tc, UFL_ORDERS, u->flags & UFL_ORDERS); + test_teardown(); +} + typedef struct order_list { const char **orders; int next; @@ -85,6 +139,7 @@ CuSuite *get_orderfile_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_read_orders); + SUITE_ADD_TEST(suite, test_unit_orders); SUITE_ADD_TEST(suite, test_faction_password_okay); SUITE_ADD_TEST(suite, test_faction_password_bad); diff --git a/src/util/keyword.c b/src/util/keyword.c index e2b5b4775..0d9a11dc4 100644 --- a/src/util/keyword.c +++ b/src/util/keyword.c @@ -88,6 +88,11 @@ bool keyword_disabled(keyword_t kwd) { return disabled_kwd[kwd]; } +const char *keyword_name(keyword_t kwd, const struct locale *lang) +{ + return LOC(lang, mkname("keyword", keywords[kwd])); +} + const char *keywords[MAXKEYWORDS] = { "//", "banner", diff --git a/src/util/keyword.h b/src/util/keyword.h index 7bb11c485..cc78fd6f7 100644 --- a/src/util/keyword.h +++ b/src/util/keyword.h @@ -83,6 +83,7 @@ extern "C" void init_keywords(const struct locale *lang); void init_keyword(const struct locale *lang, keyword_t kwd, const char *str); bool keyword_disabled(keyword_t kwd); + const char *keyword_name(keyword_t kwd, const struct locale *lang); void enable_keyword(keyword_t kwd, bool enabled); const char *keyword(keyword_t kwd);