added tests for all kinds of syntax parameters

This commit is contained in:
Steffen Mecke 2015-11-12 22:29:56 +01:00
parent 4ab92e3caf
commit 477d69152f
3 changed files with 129 additions and 29 deletions

View File

@ -248,6 +248,8 @@ static size_t write_spell_modifier(spell * sp, int flag, const char * str, bool
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;
@ -255,7 +257,6 @@ void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang)
char *startp, *bufp = buf;
size_t size = sizeof(buf) - 1;
spell * sp = sbe->sp;
const char *params = sp->parameter;
newline(out);
centre(out, spell_name(sp, lang), true);
@ -361,6 +362,20 @@ void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang)
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)
{
int bytes;
char buf[4096];
char *bufp = buf;
size_t size = sizeof(buf) - 1;
spell * sp = sbe->sp;
const char *params = sp->parameter;
if (sp->sptyp & ISCOMBATSPELL) {
bytes = (int)strlcpy(bufp, LOC(lang, keyword(K_COMBATSPELL)), size);
}
@ -456,10 +471,15 @@ void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang)
WARN_STATIC_BUFFER();
}
else if (cp == 'k') {
if (*params == 'c') {
bool multi = false;
if (params && *params == 'c') {
/* skip over a potential id */
++params;
}
if (params && *params == '+') {
++params;
multi = true;
}
for (targetp = targets; targetp->flag; ++targetp) {
if (sp->sptyp & targetp->flag)
++maxparam;
@ -482,8 +502,7 @@ void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang)
bytes =
(int)_snprintf(bufp, size, " %s <%s>", parameters[targetp->param],
locp);
if (*params == '+') {
++params;
if (multi) {
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
bytes = (int)_snprintf(bufp, size, " [<%s> ...]", locp);
@ -520,11 +539,13 @@ void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *lang)
bytes = (int)_snprintf(bufp, size, " <%s>", locp);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} else {
log_error("unknown spell parameter %c for spell", cp, sp->sname);
}
}
*bufp = 0;
paragraph(out, buf, 2, 0, 0);
newline(out);
}
static void

View File

@ -28,6 +28,7 @@ extern "C" {
void write_spaces(struct stream *out, size_t num);
void write_travelthru(struct stream *out, const struct region * r, const struct faction * f);
void nr_spell_syntax(struct stream *out, struct spellbook_entry * sbe, const struct locale *lang);
void nr_spell(struct stream *out, struct spellbook_entry * sbe, const struct locale *lang);
#ifdef __cplusplus

View File

@ -287,47 +287,125 @@ static void test_write_unit(CuTest *tc) {
test_cleanup();
}
static void test_write_spell_syntax(CuTest *tc) {
stream strm;
char buf[1024];
char *line;
size_t len;
typedef struct {
struct locale *lang;
spell *sp;
spellbook *spb;
spellbook_entry * sbe;
struct locale *lang;
} 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");
test_cleanup();
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);
lang = get_or_create_locale("de");
locale_setstring(lang, "spell::testspell", "Testzauber");
locale_setstring(lang, "nr_spell_type", "Typ:");
locale_setstring(lang, "sptype_normal", "Normal");
locale_setstring(lang, "nr_spell_modifiers", "Modifier:");
locale_setstring(lang, "smod_none", "Keine");
locale_setstring(lang, keyword(K_CAST), "ZAUBERE");
}
static void test_spell_syntax(CuTest *tc, char *msg, spell_fixture *spell, char *syntax) {
stream strm;
char buf[1024];
char *linestart, *newline;
size_t len;
spb = create_spellbook("testbook");
sp = test_create_spell();
spellbook_add(spb, sp, 1);
sbe = spellbook_get(spb, sp);
mstream_init(&strm);
nr_spell(&strm, sbe, lang);
stream_printf(&strm, "\n");
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';
line = strtok(buf, "\n");
while (line && !strstr(line, "ZAUBERE")) line = strtok(NULL, "\n") ;
linestart = strtok(buf, "\n");
while (linestart && !strstr(linestart, "ZAUBERE"))
linestart = strtok(NULL, "\n") ;
CuAssertTrue(tc, (bool) linestart);
while ((newline = strtok(NULL, "\n")))
*(newline-1) = '\n';
CuAssertStrEquals_Msg(tc, msg, syntax, linestart);
CuAssertTrue(tc, (bool) line);
CuAssertStrEquals(tc, " ZAUBERE \"Testzauber\"", line);
mstream_done(&strm);
}
static void set_parameter(spell_fixture spell, char *value) {
if (spell.sp->parameter)
strcpy(spell.sp->parameter, value);
else
spell.sp->parameter = _strdup(value);
}
static void test_write_spell_syntax(CuTest *tc) {
spell_fixture spell;
test_cleanup();
setup_spell_fixture(&spell);
test_spell_syntax(tc, "vanilla", &spell, " ZAUBERE \"Testzauber\"");
spell.sp->sptyp |= FARCASTING;
test_spell_syntax(tc, "far", &spell, " ZAUBERE [REGION x y] \"Testzauber\"");
spell.sp->sptyp |= SPELLLEVEL;
test_spell_syntax(tc, "farlevel", &spell, " ZAUBERE [REGION x y] [STUFE n] \"Testzauber\"");
spell.sp->sptyp = 0;
set_parameter(spell, "kc");
test_spell_syntax(tc, "kc", &spell, " ZAUBERE \"Testzauber\" ( REGION | EINHEIT <enr> | SCHIFF <snr> | BURG <bnr> )");
spell.sp->sptyp |= BUILDINGSPELL;
test_spell_syntax(tc, "kc typed", &spell, " ZAUBERE \"Testzauber\" BURG <bnr>");
spell.sp->sptyp = 0;
set_parameter(spell, "b");
test_spell_syntax(tc, "b", &spell, " ZAUBERE \"Testzauber\" <bnr>");
set_parameter(spell, "s");
test_spell_syntax(tc, "s", &spell, " ZAUBERE \"Testzauber\" <snr>");
set_parameter(spell, "s+");
test_spell_syntax(tc, "s+", &spell, " ZAUBERE \"Testzauber\" <snr> [<snr> ...]");
set_parameter(spell, "u");
test_spell_syntax(tc, "u", &spell, " ZAUBERE \"Testzauber\" <enr>");
set_parameter(spell, "r");
test_spell_syntax(tc, "r", &spell, " ZAUBERE \"Testzauber\" <x> <y>");
set_parameter(spell, "bc");
spell.sp->syntax = _strdup("hodor");
test_spell_syntax(tc, "bc hodor", &spell, " ZAUBERE \"Testzauber\" <bnr> <Hodor>");
free(spell.sp->syntax);
/* no idea what ? is supposed to mean, optional parameter maybe?
set_parameter(spell, "kcc?");
spell.sp->syntax = _strdup("hodor");
test_spell_syntax(tc, "kcc?", &spell, " ZAUBERE \"Testzauber\" <bnr>");
free(spell.sp->syntax);
*/
set_parameter(spell, "kc+");
test_spell_syntax(tc, "kc+", &spell,
" ZAUBERE \"Testzauber\" ( REGION | EINHEIT <enr> [<enr> ...] | SCHIFF <snr>\n [<snr> ...] | BURG <bnr> [<bnr> ...] )");
test_cleanup();
}