refactor: extract cycle_route and test it (with PAUSE).

This commit is contained in:
Enno Rehling 2018-04-22 11:00:58 +01:00
parent 915706d6f8
commit fc4b7ba73d
6 changed files with 46 additions and 16 deletions

View File

@ -43,7 +43,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
typedef enum { typedef enum dict_type {
TNONE = 0, TINTEGER = 1, TREAL = 2 TNONE = 0, TINTEGER = 1, TREAL = 2
} dict_type; } dict_type;

View File

@ -8,7 +8,7 @@ extern "C"
struct locale; struct locale;
typedef enum { typedef enum direction_t {
D_NORTHWEST, D_NORTHWEST,
D_NORTHEAST, D_NORTHEAST,
D_EAST, D_EAST,

View File

@ -3548,7 +3548,7 @@ enum {
PROC_LONGORDER = 1 << 1 PROC_LONGORDER = 1 << 1
}; };
typedef enum { PR_GLOBAL, PR_REGION_PRE, PR_UNIT, PR_ORDER, PR_REGION_POST } processor_t; typedef enum processor_t { PR_GLOBAL, PR_REGION_PRE, PR_UNIT, PR_ORDER, PR_REGION_POST } processor_t;
typedef struct processor { typedef struct processor {
struct processor *next; struct processor *next;

View File

@ -1052,7 +1052,7 @@ int movewhere(const unit * u, const char *token, region * r, region ** resultp)
return E_MOVE_OK; return E_MOVE_OK;
} }
static void cycle_route(order * ord, unit * u, int gereist) order * cycle_route(order * ord, const struct locale *lang, int gereist)
{ {
int cm = 0; int cm = 0;
char tail[1024], *bufp = tail; char tail[1024], *bufp = tail;
@ -1067,11 +1067,10 @@ static void cycle_route(order * ord, unit * u, int gereist)
assert(getkeyword(ord) == K_ROUTE); assert(getkeyword(ord) == K_ROUTE);
tail[0] = '\0'; tail[0] = '\0';
neworder[0] = '\0'; neworder[0] = '\0';
init_order(ord, u->faction->locale); init_order(ord, lang);
for (cm = 0;; ++cm) { for (cm = 0;; ++cm) {
const char *s; const char *s;
const struct locale *lang = u->faction->locale;
pause = false; pause = false;
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
if (s && *s) { if (s && *s) {
@ -1087,7 +1086,7 @@ static void cycle_route(order * ord, unit * u, int gereist)
break; break;
} }
if (cm < gereist) { if (cm < gereist) {
/* hier sollte keine PAUSE auftreten */ /* TODO: hier sollte keine PAUSE auftreten */
assert(!pause); assert(!pause);
if (!pause) { if (!pause) {
const char *loc = LOC(lang, shortdirections[d]); const char *loc = LOC(lang, shortdirections[d]);
@ -1124,13 +1123,12 @@ static void cycle_route(order * ord, unit * u, int gereist)
} }
if (neworder[0]) { if (neworder[0]) {
norder = create_order(K_ROUTE, u->faction->locale, "%s %s", neworder, tail); norder = create_order(K_ROUTE, lang, "%s %s", neworder, tail);
} }
else { else {
norder = create_order(K_ROUTE, u->faction->locale, "%s", tail); norder = create_order(K_ROUTE, lang, "%s", tail);
} }
replace_order(&u->orders, ord, norder); return norder;
free_order(norder);
} }
static bool transport(unit * ut, unit * u) static bool transport(unit * ut, unit * u)
@ -1605,7 +1603,9 @@ static const region_list *travel_route(unit * u,
setguard(u, false); setguard(u, false);
if (getkeyword(ord) == K_ROUTE) { if (getkeyword(ord) == K_ROUTE) {
cycle_route(ord, u, steps); order * norder = cycle_route(ord, u->faction->locale, steps);
replace_order(&u->orders, ord, norder);
free_order(norder);
} }
if (mode == TRAVEL_RUNNING) { if (mode == TRAVEL_RUNNING) {
@ -1937,7 +1937,9 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting)
/* nachdem alle Richtungen abgearbeitet wurden, und alle Einheiten /* nachdem alle Richtungen abgearbeitet wurden, und alle Einheiten
* transferiert wurden, kann der aktuelle Befehl gelöscht werden. */ * transferiert wurden, kann der aktuelle Befehl gelöscht werden. */
if (getkeyword(ord) == K_ROUTE) { if (getkeyword(ord) == K_ROUTE) {
cycle_route(ord, u, step); order * norder = cycle_route(ord, u->faction->locale, step);
replace_order(&u->orders, ord, norder);
free_order(norder);
} }
set_order(&u->thisorder, NULL); set_order(&u->thisorder, NULL);
set_coast(sh, last_point, current_point); set_coast(sh, last_point, current_point);

View File

@ -26,12 +26,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern "C" { extern "C" {
#endif #endif
struct unit; struct attrib_type;
struct building_type;
struct locale;
struct order;
struct region; struct region;
struct region_list; struct region_list;
struct ship; struct ship;
struct building_type; struct unit;
struct order;
extern struct attrib_type at_shiptrail; extern struct attrib_type at_shiptrail;
extern int *storms; extern int *storms;
@ -95,6 +97,7 @@ extern "C" {
int check_ship_allowed(struct ship *sh, const struct region * r); int check_ship_allowed(struct ship *sh, const struct region * r);
direction_t drift_target(struct ship *sh); direction_t drift_target(struct ship *sh);
struct order * cycle_route(struct order * ord, const struct locale *lang, int gereist);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -593,6 +593,30 @@ static void test_route_cycle(CuTest *tc) {
test_teardown(); test_teardown();
} }
static void test_cycle_route(CuTest *tc) {
struct locale *lang;
char buffer[32];
order *ord, *onew;
test_setup();
lang = test_create_locale();
CuAssertPtrNotNull(tc, LOC(lang, shortdirections[D_WEST]));
ord = create_order(K_ROUTE, lang, "WEST EAST PAUSE NW");
CuAssertStrEquals(tc, "route WEST EAST PAUSE NW", get_command(ord, lang, buffer, sizeof(buffer)));
onew = cycle_route(ord, lang, 1);
CuAssertStrEquals(tc, "route east PAUSE nw west", get_command(onew, lang, buffer, sizeof(buffer)));
free_order(onew);
onew = cycle_route(ord, lang, 2);
CuAssertStrEquals(tc, "route nw west east PAUSE", get_command(onew, lang, buffer, sizeof(buffer)));
free_order(onew);
free_order(ord);
test_teardown();
}
static void test_route_pause(CuTest *tc) { static void test_route_pause(CuTest *tc) {
unit *u; unit *u;
region *r; region *r;
@ -658,6 +682,7 @@ CuSuite *get_move_suite(void)
SUITE_ADD_TEST(suite, test_follow_ship_msg); SUITE_ADD_TEST(suite, test_follow_ship_msg);
SUITE_ADD_TEST(suite, test_drifting_ships); SUITE_ADD_TEST(suite, test_drifting_ships);
SUITE_ADD_TEST(suite, test_route_cycle); SUITE_ADD_TEST(suite, test_route_cycle);
SUITE_ADD_TEST(suite, test_cycle_route);
SUITE_ADD_TEST(suite, test_route_pause); SUITE_ADD_TEST(suite, test_route_pause);
return suite; return suite;
} }