split off report.test from reports.test

move some tests around
This commit is contained in:
Enno Rehling 2016-09-13 20:13:26 +02:00
parent 428f640ba6
commit 4e4b4e482e
6 changed files with 373 additions and 338 deletions

View File

@ -114,7 +114,7 @@ set (ERESSEA_SRC
randenc.c randenc.c
volcano.c volcano.c
chaos.c chaos.c
# report.c report.c
spy.c spy.c
study.c study.c
summary.c summary.c
@ -194,7 +194,8 @@ set(TESTS_SRC
vortex.test.c vortex.test.c
tests.test.c tests.test.c
volcano.test.c volcano.test.c
# reports.test.c reports.test.c
report.test.c
summary.test.c summary.test.c
travelthru.test.c travelthru.test.c
callback.test.c callback.test.c

View File

@ -11,6 +11,9 @@
*/ */
#ifndef H_GC_CREPORT #ifndef H_GC_CREPORT
#define H_GC_CREPORT #define H_GC_CREPORT
#include <kernel/types.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -1,6 +1,7 @@
#include <platform.h> #include <platform.h>
#include <kernel/spell.h> #include <kernel/spell.h>
#include <kernel/spellbook.h>
#include <util/log.h> #include <util/log.h>
#include <util/lists.h> #include <util/lists.h>

View File

@ -144,7 +144,6 @@ void write_spaces(stream *out, size_t num) {
} }
} }
static void centre(stream *out, const char *s, bool breaking) static void centre(stream *out, const char *s, bool breaking)
{ {
/* Bei Namen die genau 80 Zeichen lang sind, kann es hier Probleme /* Bei Namen die genau 80 Zeichen lang sind, kann es hier Probleme
@ -247,125 +246,6 @@ static size_t write_spell_modifier(spell * sp, int flag, const char * str, bool
return 0; return 0;
} }
void nr_spell_syntax(struct stream *out, struct spellbook_entry * sbe, const struct locale *lang);
void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang)
{
int bytes, k, itemanz, costtyp;
char buf[4096];
char *startp, *bufp = buf;
size_t size = sizeof(buf) - 1;
spell * sp = sbe->sp;
newline(out);
centre(out, spell_name(sp, lang), true);
newline(out);
paragraph(out, LOC(lang, "nr_spell_description"), 0, 0, 0);
paragraph(out, spell_info(sp, lang), 2, 0, 0);
bytes = (int)strlcpy(bufp, LOC(lang, "nr_spell_type"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (size) {
*bufp++ = ' ';
--size;
}
if (sp->sptyp & PRECOMBATSPELL) {
bytes = (int)strlcpy(bufp, LOC(lang, "sptype_precombat"), size);
}
else if (sp->sptyp & COMBATSPELL) {
bytes = (int)strlcpy(bufp, LOC(lang, "sptype_combat"), size);
}
else if (sp->sptyp & POSTCOMBATSPELL) {
bytes = (int)strlcpy(bufp, LOC(lang, "sptype_postcombat"), size);
}
else {
bytes = (int)strlcpy(bufp, LOC(lang, "sptype_normal"), size);
}
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
*bufp = 0;
paragraph(out, buf, 0, 0, 0);
sprintf(buf, "%s %d", LOC(lang, "nr_spell_level"), sbe->level);
paragraph(out, buf, 0, 0, 0);
sprintf(buf, "%s %d", LOC(lang, "nr_spell_rank"), sp->rank);
paragraph(out, buf, 0, 0, 0);
paragraph(out, LOC(lang, "nr_spell_components"), 0, 0, 0);
for (k = 0; sp->components[k].type; ++k) {
const resource_type *rtype = sp->components[k].type;
itemanz = sp->components[k].amount;
costtyp = sp->components[k].cost;
if (itemanz > 0) {
size = sizeof(buf) - 1;
bufp = buf;
if (sp->sptyp & SPELLLEVEL) {
bytes =
_snprintf(bufp, size, " %d %s", itemanz, LOC(lang, resourcename(rtype,
itemanz != 1)));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (costtyp == SPC_LEVEL || costtyp == SPC_LINEAR) {
bytes = _snprintf(bufp, size, " * %s", LOC(lang, "nr_level"));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
}
}
else {
bytes = _snprintf(bufp, size, "%d %s", itemanz, LOC(lang, resourcename(rtype, itemanz != 1)));
if (wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
}
*bufp = 0;
paragraph(out, buf, 2, 2, '-');
}
}
size = sizeof(buf) - 1;
bufp = buf;
bytes = (int)strlcpy(buf, LOC(lang, "nr_spell_modifiers"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
startp = bufp;
bytes = (int)write_spell_modifier(sp, FARCASTING, LOC(lang, "smod_far"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
bytes = (int)write_spell_modifier(sp, OCEANCASTABLE, LOC(lang, "smod_sea"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
bytes = (int)write_spell_modifier(sp, ONSHIPCAST, LOC(lang, "smod_ship"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
bytes = (int)write_spell_modifier(sp, NOTFAMILIARCAST, LOC(lang, "smod_nofamiliar"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
if (startp == bufp) {
bytes = (int)write_spell_modifier(sp, NOTFAMILIARCAST, LOC(lang, "smod_none"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
}
*bufp = 0;
paragraph(out, buf, 0, 0, 0);
paragraph(out, LOC(lang, "nr_spell_syntax"), 0, 0, 0);
bufp = buf;
size = sizeof(buf) - 1;
nr_spell_syntax(out, sbe, lang);
newline(out);
}
void nr_spell_syntax(stream *out, spellbook_entry * sbe, const struct locale *lang) void nr_spell_syntax(stream *out, spellbook_entry * sbe, const struct locale *lang)
{ {
int bytes; int bytes;
@ -544,8 +424,9 @@ void nr_spell_syntax(stream *out, spellbook_entry * sbe, const struct locale *la
} }
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} else { }
log_error("unknown spell parameter %c for spell %s", cp, sp->sname); else {
log_error("unknown spell parameter %c for spell %s", cp, sp->sname);
} }
} }
*bufp = 0; *bufp = 0;
@ -553,6 +434,123 @@ void nr_spell_syntax(stream *out, spellbook_entry * sbe, const struct locale *la
} }
void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang)
{
int bytes, k, itemanz, costtyp;
char buf[4096];
char *startp, *bufp = buf;
size_t size = sizeof(buf) - 1;
spell * sp = sbe->sp;
newline(out);
centre(out, spell_name(sp, lang), true);
newline(out);
paragraph(out, LOC(lang, "nr_spell_description"), 0, 0, 0);
paragraph(out, spell_info(sp, lang), 2, 0, 0);
bytes = (int)strlcpy(bufp, LOC(lang, "nr_spell_type"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (size) {
*bufp++ = ' ';
--size;
}
if (sp->sptyp & PRECOMBATSPELL) {
bytes = (int)strlcpy(bufp, LOC(lang, "sptype_precombat"), size);
}
else if (sp->sptyp & COMBATSPELL) {
bytes = (int)strlcpy(bufp, LOC(lang, "sptype_combat"), size);
}
else if (sp->sptyp & POSTCOMBATSPELL) {
bytes = (int)strlcpy(bufp, LOC(lang, "sptype_postcombat"), size);
}
else {
bytes = (int)strlcpy(bufp, LOC(lang, "sptype_normal"), size);
}
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
*bufp = 0;
paragraph(out, buf, 0, 0, 0);
sprintf(buf, "%s %d", LOC(lang, "nr_spell_level"), sbe->level);
paragraph(out, buf, 0, 0, 0);
sprintf(buf, "%s %d", LOC(lang, "nr_spell_rank"), sp->rank);
paragraph(out, buf, 0, 0, 0);
paragraph(out, LOC(lang, "nr_spell_components"), 0, 0, 0);
for (k = 0; sp->components[k].type; ++k) {
const resource_type *rtype = sp->components[k].type;
itemanz = sp->components[k].amount;
costtyp = sp->components[k].cost;
if (itemanz > 0) {
size = sizeof(buf) - 1;
bufp = buf;
if (sp->sptyp & SPELLLEVEL) {
bytes =
_snprintf(bufp, size, " %d %s", itemanz, LOC(lang, resourcename(rtype,
itemanz != 1)));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (costtyp == SPC_LEVEL || costtyp == SPC_LINEAR) {
bytes = _snprintf(bufp, size, " * %s", LOC(lang, "nr_level"));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
}
}
else {
bytes = _snprintf(bufp, size, "%d %s", itemanz, LOC(lang, resourcename(rtype, itemanz != 1)));
if (wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
}
*bufp = 0;
paragraph(out, buf, 2, 2, '-');
}
}
size = sizeof(buf) - 1;
bufp = buf;
bytes = (int)strlcpy(buf, LOC(lang, "nr_spell_modifiers"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
startp = bufp;
bytes = (int)write_spell_modifier(sp, FARCASTING, LOC(lang, "smod_far"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
bytes = (int)write_spell_modifier(sp, OCEANCASTABLE, LOC(lang, "smod_sea"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
bytes = (int)write_spell_modifier(sp, ONSHIPCAST, LOC(lang, "smod_ship"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
bytes = (int)write_spell_modifier(sp, NOTFAMILIARCAST, LOC(lang, "smod_nofamiliar"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
if (startp == bufp) {
bytes = (int)write_spell_modifier(sp, NOTFAMILIARCAST, LOC(lang, "smod_none"), startp != bufp, bufp, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
}
*bufp = 0;
paragraph(out, buf, 0, 0, 0);
paragraph(out, LOC(lang, "nr_spell_syntax"), 0, 0, 0);
bufp = buf;
size = sizeof(buf) - 1;
nr_spell_syntax(out, sbe, lang);
newline(out);
}
static void static void
nr_curses_i(stream *out, int indent, const faction *viewer, objtype_t typ, const void *obj, attrib *a, int self) nr_curses_i(stream *out, int indent, const faction *viewer, objtype_t typ, const void *obj, attrib *a, int self)
{ {
@ -675,12 +673,12 @@ static void rps_nowrap(stream *out, const char *s)
} }
static void static void
nr_unit(stream *out, const faction * f, const unit * u, int indent, int mode) nr_unit(stream *out, const faction * f, const unit * u, int indent, seen_mode mode)
{ {
attrib *a_otherfaction; attrib *a_otherfaction;
char marker; char marker;
int dh; int dh;
bool isbattle = (bool)(mode == see_battle); bool isbattle = (bool)(mode == seen_battle);
char buf[8192]; char buf[8192];
if (fval(u_race(u), RCF_INVISIBLE)) if (fval(u_race(u), RCF_INVISIBLE))
@ -875,9 +873,8 @@ bool see_border(const connection * b, const faction * f, const region * r)
return cs; return cs;
} }
static void describe(stream *out, const seen_region * sr, faction * f) static void describe(stream *out, const region * r, faction * f)
{ {
const region *r;
int n; int n;
bool dh; bool dh;
direction_t d; direction_t d;
@ -901,9 +898,8 @@ static void describe(stream *out, const seen_region * sr, faction * f)
assert(out); assert(out);
assert(f); assert(f);
assert(sr); assert(r);
r = sr->r;
for (d = 0; d != MAXDIRECTIONS; d++) { for (d = 0; d != MAXDIRECTIONS; d++) {
/* Nachbarregionen, die gesehen werden, ermitteln */ /* Nachbarregionen, die gesehen werden, ermitteln */
region *r2 = rconnect(r, d); region *r2 = rconnect(r, d);

242
src/report.test.c Normal file
View File

@ -0,0 +1,242 @@
#include <platform.h>
#include <config.h>
#include "report.h"
#include "move.h"
#include "travelthru.h"
#include "keyword.h"
#include <kernel/building.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/ship.h>
#include <kernel/unit.h>
#include <kernel/spell.h>
#include <kernel/spellbook.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/message.h>
#include <quicklist.h>
#include <stream.h>
#include <memstream.h>
#include <CuTest.h>
#include <tests.h>
#include <string.h>
static void test_write_spaces(CuTest *tc) {
stream out = { 0 };
char buf[1024];
size_t len;
mstream_init(&out);
write_spaces(&out, 4);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = '\0';
CuAssertStrEquals(tc, " ", buf);
CuAssertIntEquals(tc, ' ', buf[3]);
mstream_done(&out);
}
static void test_write_many_spaces(CuTest *tc) {
stream out = { 0 };
char buf[1024];
size_t len;
mstream_init(&out);
write_spaces(&out, 100);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = '\0';
CuAssertIntEquals(tc, 100, (int)len);
CuAssertIntEquals(tc, ' ', buf[99]);
mstream_done(&out);
}
static void test_write_travelthru(CuTest *tc) {
stream out = { 0 };
char buf[1024];
size_t len;
region *r;
faction *f;
unit *u;
struct locale *lang;
test_cleanup();
lang = get_or_create_locale("de");
locale_setstring(lang, "travelthru_header", "Durchreise: ");
mstream_init(&out);
r = test_create_region(0, 0, 0);
r->flags |= RF_TRAVELUNIT;
f = test_create_faction(0);
f->locale = lang;
u = test_create_unit(f, test_create_region(0, 1, 0));
unit_setname(u, "Hodor");
unit_setid(u, 1);
write_travelthru(&out, r, f);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
CuAssertIntEquals_Msg(tc, "no travelers, no report", 0, (int)len);
mstream_done(&out);
mstream_init(&out);
travelthru_add(r, u);
write_travelthru(&out, r, f);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = '\0';
CuAssertStrEquals_Msg(tc, "list one unit", "Durchreise: Hodor (1).\n", buf);
mstream_done(&out);
mstream_init(&out);
move_unit(u, r, 0);
write_travelthru(&out, r, f);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
CuAssertIntEquals_Msg(tc, "do not list units that stopped in the region", 0, (int)len);
mstream_done(&out);
test_cleanup();
}
typedef struct {
struct locale *lang;
spell *sp;
spellbook *spb;
spellbook_entry * sbe;
} spell_fixture;
static void setup_spell_fixture(spell_fixture * spf) {
spf->lang = test_create_locale();
locale_setstring(spf->lang, mkname("spell", "testspell"), "Testzauber");
locale_setstring(spf->lang, "nr_spell_type", "Typ:");
locale_setstring(spf->lang, "sptype_normal", "Normal");
locale_setstring(spf->lang, "nr_spell_modifiers", "Modifier:");
locale_setstring(spf->lang, "smod_none", "Keine");
locale_setstring(spf->lang, keyword(K_CAST), "ZAUBERE");
locale_setstring(spf->lang, parameters[P_REGION], "REGION");
locale_setstring(spf->lang, parameters[P_LEVEL], "STUFE");
locale_setstring(spf->lang, "par_unit", "enr");
locale_setstring(spf->lang, "par_ship", "snr");
locale_setstring(spf->lang, "par_building", "bnr");
locale_setstring(spf->lang, "spellpar::hodor", "Hodor");
spf->spb = create_spellbook("testbook");
spf->sp = test_create_spell();
spellbook_add(spf->spb, spf->sp, 1);
spf->sbe = spellbook_get(spf->spb, spf->sp);
}
static void cleanup_spell_fixture(spell_fixture *spf) {
spellbook_clear(spf->spb);
free(spf->spb);
test_cleanup();
}
static void set_parameter(spell_fixture spell, char *value) {
free(spell.sp->parameter);
spell.sp->parameter = _strdup(value);
}
static void check_spell_syntax(CuTest *tc, char *msg, spell_fixture *spell, char *syntax) {
stream strm;
char buf[1024];
char *linestart, *newline;
size_t len;
mstream_init(&strm);
nr_spell_syntax(&strm, spell->sbe, spell->lang);
strm.api->rewind(strm.handle);
len = strm.api->read(strm.handle, buf, sizeof(buf));
buf[len] = '\0';
linestart = strtok(buf, "\n");
while (linestart && !strstr(linestart, "ZAUBERE"))
linestart = strtok(NULL, "\n");
CuAssertPtrNotNull(tc, linestart);
newline = strtok(NULL, "\n");
while (newline) {
*(newline - 1) = '\n';
newline = strtok(NULL, "\n");
}
CuAssertStrEquals_Msg(tc, msg, syntax, linestart);
mstream_done(&strm);
}
static void test_write_spell_syntax(CuTest *tc) {
spell_fixture spell;
test_cleanup();
setup_spell_fixture(&spell);
check_spell_syntax(tc, "vanilla", &spell, " ZAUBERE \"Testzauber\"");
spell.sp->sptyp |= FARCASTING;
check_spell_syntax(tc, "far", &spell, " ZAUBERE [REGION x y] \"Testzauber\"");
spell.sp->sptyp |= SPELLLEVEL;
check_spell_syntax(tc, "farlevel", &spell, " ZAUBERE [REGION x y] [STUFE n] \"Testzauber\"");
spell.sp->sptyp = 0;
set_parameter(spell, "kc");
check_spell_syntax(tc, "kc", &spell, " ZAUBERE \"Testzauber\" ( REGION | EINHEIT <enr> | SCHIFF <snr> | BURG <bnr> )");
spell.sp->sptyp |= BUILDINGSPELL;
check_spell_syntax(tc, "kc typed", &spell, " ZAUBERE \"Testzauber\" BURG <bnr>");
spell.sp->sptyp = 0;
set_parameter(spell, "b");
check_spell_syntax(tc, "b", &spell, " ZAUBERE \"Testzauber\" <bnr>");
set_parameter(spell, "s");
check_spell_syntax(tc, "s", &spell, " ZAUBERE \"Testzauber\" <snr>");
set_parameter(spell, "s+");
check_spell_syntax(tc, "s+", &spell, " ZAUBERE \"Testzauber\" <snr> [<snr> ...]");
set_parameter(spell, "u");
check_spell_syntax(tc, "u", &spell, " ZAUBERE \"Testzauber\" <enr>");
set_parameter(spell, "r");
check_spell_syntax(tc, "r", &spell, " ZAUBERE \"Testzauber\" <x> <y>");
set_parameter(spell, "bc");
free(spell.sp->syntax);
spell.sp->syntax = _strdup("hodor");
check_spell_syntax(tc, "bc hodor", &spell, " ZAUBERE \"Testzauber\" <bnr> <Hodor>");
free(spell.sp->syntax);
spell.sp->syntax = 0;
set_parameter(spell, "c?");
free(spell.sp->syntax);
spell.sp->syntax = _strdup("hodor");
check_spell_syntax(tc, "c?", &spell, " ZAUBERE \"Testzauber\" [<Hodor>]");
free(spell.sp->syntax);
spell.sp->syntax = 0;
set_parameter(spell, "kc+");
check_spell_syntax(tc, "kc+", &spell,
" ZAUBERE \"Testzauber\" ( REGION | EINHEIT <enr> [<enr> ...] | SCHIFF <snr>\n [<snr> ...] | BURG <bnr> [<bnr> ...] )");
cleanup_spell_fixture(&spell);
}
CuSuite *get_report_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_write_spaces);
SUITE_ADD_TEST(suite, test_write_many_spaces);
SUITE_ADD_TEST(suite, test_write_travelthru);
SUITE_ADD_TEST(suite, test_write_spell_syntax);
return suite;
}

View File

@ -1,7 +1,6 @@
#include <platform.h> #include <platform.h>
#include <config.h> #include <config.h>
#include "reports.h" #include "reports.h"
//#include "report.h"
#include "creport.h" #include "creport.h"
#include "move.h" #include "move.h"
#include "travelthru.h" #include "travelthru.h"
@ -118,36 +117,6 @@ static void test_seen_faction(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_write_spaces(CuTest *tc) {
stream out = { 0 };
char buf[1024];
size_t len;
mstream_init(&out);
write_spaces(&out, 4);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = '\0';
CuAssertStrEquals(tc, " ", buf);
CuAssertIntEquals(tc, ' ', buf[3]);
mstream_done(&out);
}
static void test_write_many_spaces(CuTest *tc) {
stream out = { 0 };
char buf[1024];
size_t len;
mstream_init(&out);
write_spaces(&out, 100);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = '\0';
CuAssertIntEquals(tc, 100, (int)len);
CuAssertIntEquals(tc, ' ', buf[99]);
mstream_done(&out);
}
static void test_sparagraph(CuTest *tc) { static void test_sparagraph(CuTest *tc) {
strlist *sp = 0; strlist *sp = 0;
@ -202,53 +171,6 @@ static void test_cr_unit(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_write_travelthru(CuTest *tc) {
stream out = { 0 };
char buf[1024];
size_t len;
region *r;
faction *f;
unit *u;
struct locale *lang;
test_cleanup();
lang = get_or_create_locale("de");
locale_setstring(lang, "travelthru_header", "Durchreise: ");
mstream_init(&out);
r = test_create_region(0, 0, 0);
r->flags |= RF_TRAVELUNIT;
f = test_create_faction(0);
f->locale = lang;
u = test_create_unit(f, test_create_region(0, 1, 0));
unit_setname(u, "Hodor");
unit_setid(u, 1);
write_travelthru(&out, r, f);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
CuAssertIntEquals_Msg(tc, "no travelers, no report", 0, (int)len);
mstream_done(&out);
mstream_init(&out);
travelthru_add(r, u);
write_travelthru(&out, r, f);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = '\0';
CuAssertStrEquals_Msg(tc, "list one unit", "Durchreise: Hodor (1).\n", buf);
mstream_done(&out);
mstream_init(&out);
move_unit(u, r, 0);
write_travelthru(&out, r, f);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
CuAssertIntEquals_Msg(tc, "do not list units that stopped in the region", 0, (int)len);
mstream_done(&out);
test_cleanup();
}
static void test_write_unit(CuTest *tc) { static void test_write_unit(CuTest *tc) {
unit *u; unit *u;
faction *f; faction *f;
@ -290,132 +212,6 @@ static void test_write_unit(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
typedef struct {
struct locale *lang;
spell *sp;
spellbook *spb;
spellbook_entry * sbe;
} spell_fixture;
static void setup_spell_fixture(spell_fixture * spf) {
spf->lang = get_or_create_locale("de");
locale_setstring(spf->lang, mkname("spell", "testspell"), "Testzauber");
locale_setstring(spf->lang, "nr_spell_type", "Typ:");
locale_setstring(spf->lang, "sptype_normal", "Normal");
locale_setstring(spf->lang, "nr_spell_modifiers", "Modifier:");
locale_setstring(spf->lang, "smod_none", "Keine");
locale_setstring(spf->lang, keyword(K_CAST), "ZAUBERE");
locale_setstring(spf->lang, parameters[P_REGION], "REGION");
locale_setstring(spf->lang, parameters[P_LEVEL], "STUFE");
locale_setstring(spf->lang, "par_unit", "enr");
locale_setstring(spf->lang, "par_ship", "snr");
locale_setstring(spf->lang, "par_building", "bnr");
locale_setstring(spf->lang, "spellpar::hodor", "Hodor");
spf->spb = create_spellbook("testbook");
spf->sp = test_create_spell();
spellbook_add(spf->spb, spf->sp, 1);
spf->sbe = spellbook_get(spf->spb, spf->sp);
}
static void cleanup_spell_fixture(spell_fixture *spf) {
spellbook_clear(spf->spb);
free(spf->spb);
test_cleanup();
}
static void check_spell_syntax(CuTest *tc, char *msg, spell_fixture *spell, char *syntax) {
stream strm;
char buf[1024];
char *linestart, *newline;
size_t len;
mstream_init(&strm);
nr_spell_syntax(&strm, spell->sbe, spell->lang);
strm.api->rewind(strm.handle);
len = strm.api->read(strm.handle, buf, sizeof(buf));
buf[len] = '\0';
linestart = strtok(buf, "\n");
while (linestart && !strstr(linestart, "ZAUBERE"))
linestart = strtok(NULL, "\n") ;
CuAssertPtrNotNull(tc, linestart);
newline = strtok(NULL, "\n");
while (newline) {
*(newline - 1) = '\n';
newline = strtok(NULL, "\n");
}
CuAssertStrEquals_Msg(tc, msg, syntax, linestart);
mstream_done(&strm);
}
static void set_parameter(spell_fixture spell, char *value) {
free(spell.sp->parameter);
spell.sp->parameter = _strdup(value);
}
static void test_write_spell_syntax(CuTest *tc) {
spell_fixture spell;
test_cleanup();
setup_spell_fixture(&spell);
check_spell_syntax(tc, "vanilla", &spell, " ZAUBERE \"Testzauber\"");
spell.sp->sptyp |= FARCASTING;
check_spell_syntax(tc, "far", &spell, " ZAUBERE [REGION x y] \"Testzauber\"");
spell.sp->sptyp |= SPELLLEVEL;
check_spell_syntax(tc, "farlevel", &spell, " ZAUBERE [REGION x y] [STUFE n] \"Testzauber\"");
spell.sp->sptyp = 0;
set_parameter(spell, "kc");
check_spell_syntax(tc, "kc", &spell, " ZAUBERE \"Testzauber\" ( REGION | EINHEIT <enr> | SCHIFF <snr> | BURG <bnr> )");
spell.sp->sptyp |= BUILDINGSPELL;
check_spell_syntax(tc, "kc typed", &spell, " ZAUBERE \"Testzauber\" BURG <bnr>");
spell.sp->sptyp = 0;
set_parameter(spell, "b");
check_spell_syntax(tc, "b", &spell, " ZAUBERE \"Testzauber\" <bnr>");
set_parameter(spell, "s");
check_spell_syntax(tc, "s", &spell, " ZAUBERE \"Testzauber\" <snr>");
set_parameter(spell, "s+");
check_spell_syntax(tc, "s+", &spell, " ZAUBERE \"Testzauber\" <snr> [<snr> ...]");
set_parameter(spell, "u");
check_spell_syntax(tc, "u", &spell, " ZAUBERE \"Testzauber\" <enr>");
set_parameter(spell, "r");
check_spell_syntax(tc, "r", &spell, " ZAUBERE \"Testzauber\" <x> <y>");
set_parameter(spell, "bc");
free(spell.sp->syntax);
spell.sp->syntax = _strdup("hodor");
check_spell_syntax(tc, "bc hodor", &spell, " ZAUBERE \"Testzauber\" <bnr> <Hodor>");
free(spell.sp->syntax);
spell.sp->syntax = 0;
set_parameter(spell, "c?");
free(spell.sp->syntax);
spell.sp->syntax = _strdup("hodor");
check_spell_syntax(tc, "c?", &spell, " ZAUBERE \"Testzauber\" [<Hodor>]");
free(spell.sp->syntax);
spell.sp->syntax = 0;
set_parameter(spell, "kc+");
check_spell_syntax(tc, "kc+", &spell,
" ZAUBERE \"Testzauber\" ( REGION | EINHEIT <enr> [<enr> ...] | SCHIFF <snr>\n [<snr> ...] | BURG <bnr> [<bnr> ...] )");
cleanup_spell_fixture(&spell);
}
static void test_arg_resources(CuTest *tc) { static void test_arg_resources(CuTest *tc) {
variant v1, v2; variant v1, v2;
arg_type *atype; arg_type *atype;
@ -456,12 +252,8 @@ CuSuite *get_reports_suite(void)
SUITE_ADD_TEST(suite, test_reorder_units); SUITE_ADD_TEST(suite, test_reorder_units);
SUITE_ADD_TEST(suite, test_seen_faction); SUITE_ADD_TEST(suite, test_seen_faction);
SUITE_ADD_TEST(suite, test_regionid); SUITE_ADD_TEST(suite, test_regionid);
SUITE_ADD_TEST(suite, test_write_spaces);
SUITE_ADD_TEST(suite, test_write_many_spaces);
SUITE_ADD_TEST(suite, test_sparagraph); SUITE_ADD_TEST(suite, test_sparagraph);
SUITE_ADD_TEST(suite, test_write_travelthru);
SUITE_ADD_TEST(suite, test_write_unit); SUITE_ADD_TEST(suite, test_write_unit);
SUITE_ADD_TEST(suite, test_write_spell_syntax);
SUITE_ADD_TEST(suite, test_arg_resources); SUITE_ADD_TEST(suite, test_arg_resources);
return suite; return suite;
} }