kill read_orders, rewrite tests to use parseorders.

This commit is contained in:
Enno Rehling 2020-03-02 21:05:49 +01:00
parent 64ef6f9c32
commit 0bbee5d1ac
3 changed files with 19 additions and 137 deletions

View file

@ -147,83 +147,6 @@ static faction *factionorders(void)
return f;
}
int read_orders(input *in)
{
const char *b;
int nfactions = 0;
struct faction *f = NULL;
const struct locale *lang = default_locale;
/* TODO: recognize UTF8 BOM */
b = in->getbuf(in->data);
/* Auffinden der ersten Partei, und danach abarbeiten bis zur letzten
* Partei */
while (b) {
char token[128];
param_t p;
const char *s;
init_tokens_str(b);
s = gettoken(token, sizeof(token));
p = findparam_block(s, lang, true);
switch (p) {
case P_GAMENAME:
case P_FACTION:
f = factionorders();
if (f) {
lang = f->locale;
++nfactions;
}
b = in->getbuf(in->data);
break;
/* in factionorders wird nur eine zeile gelesen:
* diejenige mit dem passwort. Die befehle der units
* werden geloescht, und die Partei wird als aktiv
* vermerkt. */
case P_UNIT:
if (f) {
unitorders(in, f);
do {
b = in->getbuf(in->data);
if (!b) {
break;
}
init_tokens_str(b);
s = gettoken(token, sizeof(token));
p = (s && s[0] != '@') ? findparam(s, lang) : NOPARAM;
} while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT
&& p != P_GAMENAME);
}
break;
/* Falls in unitorders() abgebrochen wird, steht dort entweder eine neue
* Partei, eine neue Einheit oder das File-Ende. Das switch() wird erneut
* durchlaufen, und die entsprechende Funktion aufgerufen. Man darf buf
* auf alle Faelle nicht ueberschreiben! Bei allen anderen Eintraegen hier
* muss buf erneut gefaellt werden, da die betreffende Information in nur
* einer Zeile steht, und nun die naechste gelesen werden muss.
*/
case P_NEXT:
f = NULL;
lang = default_locale;
b = in->getbuf(in->data);
break;
default:
b = in->getbuf(in->data);
break;
}
}
log_info("done reading orders for %d factions", nfactions);
return 0;
}
typedef struct parser_state {
unit *u;
faction *f;

View file

@ -12,7 +12,6 @@ extern "C" {
void *data;
} input;
int read_orders(struct input *in);
int parseorders(FILE *F);
#ifdef __cplusplus

View file

@ -21,63 +21,30 @@
#include <CuTest.h>
#include <tests.h>
static const char *getbuf_null(void *data)
{
return NULL;
}
static void test_read_orders(CuTest *tc) {
input in;
test_setup();
in.getbuf = getbuf_null;
in.data = NULL;
CuAssertIntEquals(tc, 0, read_orders(&in));
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, *backup;
char buf[64];
FILE *F = tmpfile();
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",
fprintf(F, "%s %s %s\n",
LOC(f->locale, parameters[P_FACTION]), itoa36(f->no), "password");
addstrlist(&orders, buf);
snprintf(buf, sizeof(buf), "%s %s",
fprintf(F, "%s %s\n",
LOC(f->locale, parameters[P_UNIT]), itoa36(u->no));
addstrlist(&orders, buf);
snprintf(buf, sizeof(buf), "%s %s", keyword_name(K_MOVE, f->locale),
fprintf(F, "%s %s\n", keyword_name(K_MOVE, f->locale),
LOC(f->locale, shortdirections[D_WEST]));
backup = orders;
addstrlist(&orders, buf);
in.data = &orders;
in.getbuf = getbuf_strings;
CuAssertIntEquals(tc, 0, read_orders(&in));
rewind(F);
CuAssertIntEquals(tc, 0, parseorders(F));
CuAssertPtrNotNull(tc, u->old_orders);
CuAssertPtrNotNull(tc, u->orders);
CuAssertPtrEquals(tc, NULL, orders);
CuAssertIntEquals(tc, K_MOVE, getkeyword(u->orders));
CuAssertIntEquals(tc, K_ENTERTAIN, getkeyword(u->old_orders));
CuAssertIntEquals(tc, UFL_ORDERS, u->flags & UFL_ORDERS);
freestrlist(backup);
fclose(F);
test_teardown();
}
@ -93,10 +60,8 @@ static const char *getbuf_list(void *data)
}
static void test_faction_password_okay(CuTest *tc) {
input in;
faction *f;
order_list olist;
const char *orders[] = { "ERESSEA 1 password", NULL };
FILE *F;
test_setup();
f = test_create_faction(NULL);
@ -104,21 +69,18 @@ static void test_faction_password_okay(CuTest *tc) {
CuAssertIntEquals(tc, 1, f->no);
faction_setpassword(f, "password");
f->lastorders = turn - 1;
olist.orders = orders;
olist.next = 0;
in.getbuf = getbuf_list;
in.data = &olist;
CuAssertIntEquals(tc, 0, read_orders(&in));
CuAssertIntEquals(tc, 2, olist.next);
F = tmpfile();
fprintf(F, "ERESSEA 1 password\n");
rewind(F);
CuAssertIntEquals(tc, 0, parseorders(F));
CuAssertIntEquals(tc, turn, f->lastorders);
fclose(F);
test_teardown();
}
static void test_faction_password_bad(CuTest *tc) {
input in;
faction *f;
order_list olist;
const char *orders[] = { "ERESSEA 1 password", NULL };
FILE *F;
test_setup();
mt_create_va(mt_new("wrongpasswd", NULL), "password:string", MT_NEW_END);
@ -128,20 +90,18 @@ static void test_faction_password_bad(CuTest *tc) {
CuAssertIntEquals(tc, 1, f->no);
faction_setpassword(f, "patzword");
f->lastorders = turn - 1;
olist.orders = orders;
olist.next = 0;
in.getbuf = getbuf_list;
in.data = &olist;
CuAssertIntEquals(tc, 0, read_orders(&in));
CuAssertIntEquals(tc, 2, olist.next);
F = tmpfile();
fprintf(F, "ERESSEA 1 password\n");
rewind(F);
CuAssertIntEquals(tc, 0, parseorders(F));
CuAssertIntEquals(tc, turn - 1, f->lastorders);
fclose(F);
test_teardown();
}
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);