refactor cr_output_resources for easy testing.

Write a (failing) test that checks first resource is silver.
This commit is contained in:
Enno Rehling 2017-03-05 15:26:04 +01:00
parent aa662e65d2
commit 6a87625409
5 changed files with 61 additions and 20 deletions

View File

@ -1157,11 +1157,9 @@ cr_borders(const region * r, const faction * f, seen_mode mode, FILE * F)
}
}
static void
cr_output_resources(FILE * F, report_context * ctx, region *r, bool see_unit)
void cr_output_resources(stream *out, const faction * f, const region *r, bool see_unit)
{
char cbuf[BUFFERSIZE], *pos = cbuf;
faction *f = ctx->f;
resource_report result[MAX_RAWMATERIALS];
int n, size = report_resources(r, result, MAX_RAWMATERIALS, f, see_unit);
@ -1169,15 +1167,18 @@ cr_output_resources(FILE * F, report_context * ctx, region *r, bool see_unit)
int trees = rtrees(r, 2);
int saplings = rtrees(r, 1);
if (trees > 0)
fprintf(F, "%d;Baeume\n", trees);
if (saplings > 0)
fprintf(F, "%d;Schoesslinge\n", saplings);
if (fval(r, RF_MALLORN) && (trees > 0 || saplings > 0))
fprintf(F, "1;Mallorn\n");
if (trees > 0) {
stream_printf(out, "%d;Baeume\n", trees);
}
if (saplings > 0) {
stream_printf(out, "%d;Schoesslinge\n", trees);
}
if (fval(r, RF_MALLORN) && (trees > 0 || saplings > 0)) {
sputs("1;Mallorn\n", out);
}
for (n = 0; n < size; ++n) {
if (result[n].level >= 0 && result[n].number >= 0) {
fprintf(F, "%d;%s\n", result[n].number, crtag(result[n].name));
stream_printf(out, "%d;%s\n", result[n].number, crtag(result[n].name));
}
}
#endif
@ -1189,10 +1190,21 @@ cr_output_resources(FILE * F, report_context * ctx, region *r, bool see_unit)
result[n].level);
}
}
if (pos != cbuf)
fputs(cbuf, F);
if (pos != cbuf) {
sputs(cbuf, out);
}
}
static void cr_output_resources_compat(FILE *F, report_context * ctx,
region *r, bool see_unit)
{
/* TODO: eliminate this function */
stream strm;
fstream_init(&strm, F);
cr_output_resources(&strm, ctx->f, r, see_unit);
}
static void
cr_region_header(FILE * F, int plid, int nx, int ny, int uid)
{
@ -1357,7 +1369,7 @@ static void cr_output_region(FILE * F, report_context * ctx, region * r)
/* this writes both some tags (RESOURCECOMPAT) and a block.
* must not write any blocks before it */
cr_output_resources(F, ctx, r, r->seen.mode >= seen_unit);
cr_output_resources_compat(F, ctx, r, r->seen.mode >= seen_unit);
if (r->seen.mode >= seen_unit) {
/* trade */

View File

@ -29,7 +29,8 @@ extern "C" {
int crwritemap(const char *filename);
void cr_output_unit(struct stream *out, const struct region * r,
const struct faction * f, const struct unit * u, seen_mode mode);
void cr_output_resources(struct stream *out, const struct faction * f,
const struct region *r, bool see_unit);
#ifdef __cplusplus
}
#endif

View File

@ -50,6 +50,32 @@ static void test_cr_unit(CuTest *tc) {
test_cleanup();
}
static void test_cr_resources(CuTest *tc) {
stream strm;
char line[1024];
faction *f;
region *r;
test_cleanup();
f = test_create_faction(0);
r = test_create_region(0, 0, 0);
r->land->horses = 100;
r->land->peasants = 200;
r->land->money = 300;
mstream_init(&strm);
cr_output_resources(&strm, f, r, false);
strm.api->rewind(strm.handle);
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9));
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
CuAssertStrEquals(tc, "\"Silber\";type", line);
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
CuAssertStrEquals(tc, "300;number", line);
mstream_done(&strm);
test_cleanup();
}
static int cr_get_int(stream *strm, const char *match, int def)
{
char line[1024];
@ -111,6 +137,7 @@ CuSuite *get_creport_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_cr_unit);
SUITE_ADD_TEST(suite, test_cr_resources);
SUITE_ADD_TEST(suite, test_cr_factionstealth);
return suite;
}

View File

@ -982,23 +982,23 @@ void init_resources(void)
* which can be used in a construction recipe or as a spell ingredient.
*/
rtype = rt_get_or_create(resourcenames[R_PEASANT]);
rtype->uchange = res_changepeasants;
rtype = rt_get_or_create(resourcenames[R_SILVER]);
rtype->flags |= RTF_ITEM | RTF_POOLED;
rtype->uchange = res_changeitem;
rtype->itype = it_get_or_create(rtype);
rtype->itype->give = give_money;
rtype = rt_get_or_create(resourcenames[R_AURA]);
rtype->uchange = res_changeaura;
rtype = rt_get_or_create(resourcenames[R_PERMAURA]);
rtype->uchange = res_changepermaura;
rtype = rt_get_or_create(resourcenames[R_LIFE]);
rtype->uchange = res_changehp;
rtype = rt_get_or_create(resourcenames[R_AURA]);
rtype->uchange = res_changeaura;
rtype = rt_get_or_create(resourcenames[R_PEASANT]);
rtype->uchange = res_changepeasants;
/* alte typen registrieren: */
init_oldpotions();

View File

@ -2049,7 +2049,8 @@ static void log_orders(const struct message *msg)
}
}
int stream_printf(struct stream * out, const char *format, ...) {
int stream_printf(struct stream * out, const char *format, ...)
{
va_list args;
int result;
char buffer[4096];