From 6a8762540970158af26dee391df74a1b13328cdd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 5 Mar 2017 15:26:04 +0100 Subject: [PATCH] refactor cr_output_resources for easy testing. Write a (failing) test that checks first resource is silver. --- src/creport.c | 38 +++++++++++++++++++++++++------------- src/creport.h | 3 ++- src/creport.test.c | 27 +++++++++++++++++++++++++++ src/kernel/item.c | 10 +++++----- src/reports.c | 3 ++- 5 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/creport.c b/src/creport.c index 99a9ede97..aa4fd2f98 100644 --- a/src/creport.c +++ b/src/creport.c @@ -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 */ diff --git a/src/creport.h b/src/creport.h index fc2517859..435bb637f 100644 --- a/src/creport.h +++ b/src/creport.h @@ -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 diff --git a/src/creport.test.c b/src/creport.test.c index 14f40268c..254a73cbb 100644 --- a/src/creport.test.c +++ b/src/creport.test.c @@ -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; } diff --git a/src/kernel/item.c b/src/kernel/item.c index 6c96cb0b0..476387075 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -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(); diff --git a/src/reports.c b/src/reports.c index a07b60f4b..74b800a90 100644 --- a/src/reports.c +++ b/src/reports.c @@ -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];