use init_order, it is better than the init_tokens+skip_token pattern.

test for new_units.
additional testing for init_order (renamed from init_command)and init_tokens.
fixed a memory access error when kwd==NOKEYWORD.
This commit is contained in:
Enno Rehling 2014-08-23 06:45:20 +02:00
parent 6b14eae118
commit ee2363a4d9
5 changed files with 60 additions and 5 deletions

View File

@ -256,7 +256,7 @@ static order *create_order_i(keyword_t kwd, const char *sptr, int persistent,
order *ord = NULL; order *ord = NULL;
int lindex; int lindex;
if (keyword_disabled(kwd)) { if ((int)kwd>0 && keyword_disabled(kwd)) {
log_error("trying to create an order for disabled keyword %s.", keyword(kwd)); log_error("trying to create an order for disabled keyword %s.", keyword(kwd));
return NULL; return NULL;
} }
@ -295,6 +295,7 @@ order *create_order(keyword_t kwd, const struct locale * lang,
const char *params, ...) const char *params, ...)
{ {
char zBuffer[DISPLAYSIZE]; char zBuffer[DISPLAYSIZE];
assert(lang);
if (params) { if (params) {
char *bufp = zBuffer; char *bufp = zBuffer;
int bytes; int bytes;
@ -568,3 +569,10 @@ void init_tokens(const struct order *ord)
char *cmd = getcommand(ord); char *cmd = getcommand(ord);
init_tokens_str(cmd, cmd); init_tokens_str(cmd, cmd);
} }
keyword_t init_order(const struct order *ord)
{
char *cmd = _strdup(ord->data->_str);
init_tokens_str(cmd, cmd);
return ord->data->_keyword;
}

View File

@ -58,8 +58,9 @@ extern "C" {
bool is_repeated(const order * ord); bool is_repeated(const order * ord);
bool is_long(const order * ord); bool is_long(const order * ord);
extern char *write_order(const order * ord, char *buffer, size_t size); char *write_order(const order * ord, char *buffer, size_t size);
extern void init_tokens(const struct order *ord); /* initialize token parsing */ void init_tokens(const struct order *ord); /* initialize token parsing */
keyword_t init_order(const struct order *ord);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -98,6 +98,27 @@ static void test_parse_maketemp(CuTest *tc) {
free_order(ord); free_order(ord);
} }
static void test_init_tokens(CuTest *tc) {
order *ord;
struct locale * lang = get_or_create_locale("en");
ord = create_order(K_MAKETEMP, lang, "hurr durr");
init_tokens(ord);
skip_token();
CuAssertStrEquals(tc, "hurr", getstrtoken());
CuAssertStrEquals(tc, "durr", getstrtoken());
}
static void test_init_order(CuTest *tc) {
order *ord;
struct locale * lang = get_or_create_locale("en");
ord = create_order(K_MAKETEMP, lang, "hurr durr");
CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord));
CuAssertStrEquals(tc, "hurr", getstrtoken());
CuAssertStrEquals(tc, "durr", getstrtoken());
}
CuSuite *get_order_suite(void) CuSuite *get_order_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -106,5 +127,7 @@ CuSuite *get_order_suite(void)
SUITE_ADD_TEST(suite, test_parse_make); SUITE_ADD_TEST(suite, test_parse_make);
SUITE_ADD_TEST(suite, test_parse_make_temp); SUITE_ADD_TEST(suite, test_parse_make_temp);
SUITE_ADD_TEST(suite, test_parse_maketemp); SUITE_ADD_TEST(suite, test_parse_maketemp);
SUITE_ADD_TEST(suite, test_init_tokens);
SUITE_ADD_TEST(suite, test_init_order);
return suite; return suite;
} }

View File

@ -3627,8 +3627,7 @@ void new_units(void)
} }
continue; continue;
} }
init_tokens(makeord); init_order(makeord);
skip_token();
alias = getid(); alias = getid();
token = getstrtoken(); token = getstrtoken();

View File

@ -243,6 +243,29 @@ static void test_reserve_cmd(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_new_units(CuTest *tc) {
unit *u;
faction *f;
region *r;
order *ord;
const struct locale *loc;
test_cleanup();
test_create_world();
f = test_create_faction(rc_find("human"));
r = findregion(0, 0);
assert(r && f);
u = test_create_unit(f, r);
assert(u && !u->next);
loc = get_locale("de");
assert(loc);
ord = create_order(K_MAKETEMP, loc, "hurr");
assert(ord);
u->orders = ord;
new_units();
CuAssertPtrNotNull(tc, u->next);
test_cleanup();
}
static void test_reserve_self(CuTest *tc) { static void test_reserve_self(CuTest *tc) {
unit *u1, *u2; unit *u1, *u2;
faction *f; faction *f;
@ -286,6 +309,7 @@ CuSuite *get_laws_suite(void)
SUITE_ADD_TEST(suite, test_unit_limit); SUITE_ADD_TEST(suite, test_unit_limit);
SUITE_ADD_TEST(suite, test_reserve_self); SUITE_ADD_TEST(suite, test_reserve_self);
SUITE_ADD_TEST(suite, test_reserve_cmd); SUITE_ADD_TEST(suite, test_reserve_cmd);
SUITE_ADD_TEST(suite, test_new_units);
SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit); SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit);
return suite; return suite;
} }