From fb2b6495e59e8c055893d93643769861e31f8b24 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 29 Aug 2016 15:19:17 +0100 Subject: [PATCH] promote spell.test error logging to a tests.c API, use it for item.test --- src/kernel/item.c | 2 +- src/kernel/item.test.c | 24 ++++++++++++++++++------ src/kernel/spell.test.c | 17 +++++------------ src/tests.c | 15 +++++++++++++++ src/tests.h | 4 ++++ 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/kernel/item.c b/src/kernel/item.c index 182e71fbe..9816258c2 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -491,7 +491,7 @@ item *i_change(item ** pi, const item_type * itype, int delta) item *i = *pi; i->number += delta; if (i->number < 0) { - log_error("serious accounting error. number of items is %d.\n", i->number); + log_error("serious accounting error. number of items is %d.", i->number); i->number = 0; } if (i->number == 0) { diff --git a/src/kernel/item.test.c b/src/kernel/item.test.c index 1c133b74f..47b32b537 100644 --- a/src/kernel/item.test.c +++ b/src/kernel/item.test.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include @@ -45,8 +47,11 @@ static void test_resourcename_with_appearance(CuTest *tc) { static void test_uchange(CuTest * tc, unit * u, const resource_type * rtype) { int n; + struct log_t *log; + strlist *sl = 0; assert(rtype); + log = test_log_start(LOG_CPERROR, &sl); change_resource(u, rtype, 4); n = get_resource(u, rtype); CuAssertPtrNotNull(tc, rtype->uchange); @@ -54,25 +59,31 @@ static void test_uchange(CuTest * tc, unit * u, const resource_type * rtype) { CuAssertIntEquals(tc, n - 3, rtype->uchange(u, rtype, -3)); CuAssertIntEquals(tc, n - 3, get_resource(u, rtype)); CuAssertIntEquals(tc, 0, rtype->uchange(u, rtype, -n)); + CuAssertPtrNotNull(tc, sl); + CuAssertStrEquals(tc, "serious accounting error. number of items is %d.", sl->s); + CuAssertPtrEquals(tc, 0, sl->next); + test_log_stop(log, sl); } void test_change_item(CuTest * tc) { unit * u; - test_cleanup(); + test_setup(); + test_log_stderr(0); test_create_itemtype("iron"); init_resources(); u = test_create_unit(test_create_faction(0), 0); test_uchange(tc, u, get_resourcetype(R_IRON)); + test_cleanup(); } void test_resource_type(CuTest * tc) { struct item_type *itype; - test_cleanup(); + test_setup(); CuAssertPtrEquals(tc, 0, rt_find("herpderp")); @@ -89,7 +100,7 @@ void test_finditemtype(CuTest * tc) const item_type *itype; struct locale * lang; - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); locale_setstring(lang, "horse", "Pferd"); @@ -103,7 +114,7 @@ void test_findresourcetype(CuTest * tc) const item_type *itype; struct locale * lang; - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); locale_setstring(lang, "horse", "Pferd"); @@ -124,7 +135,7 @@ static void test_fix_demand(CuTest *tc) { terrain_type *tplain; item_type *ltype; - test_cleanup(); + test_setup(); ltype = test_create_itemtype("balm"); ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED); new_luxurytype(ltype, 0); @@ -145,7 +156,8 @@ static void test_fix_demand(CuTest *tc) { static void test_core_resources(CuTest *tc) { resource_type * rtype; - test_cleanup(); + + test_setup(); init_resources(); CuAssertPtrNotNull(tc, rtype = rt_find("money")); CuAssertPtrNotNull(tc, rtype->itype); diff --git a/src/kernel/spell.test.c b/src/kernel/spell.test.c index 64ab5c0cb..fa5e9527c 100644 --- a/src/kernel/spell.test.c +++ b/src/kernel/spell.test.c @@ -25,11 +25,6 @@ static void test_create_a_spell(CuTest * tc) test_cleanup(); } -static void log_list(void *udata, int flags, const char *module, const char *format, va_list args) { - strlist **slp = (strlist **)udata; - addstrlist(slp, format); -} - static void test_create_duplicate_spell(CuTest * tc) { spell *sp; @@ -38,7 +33,7 @@ static void test_create_duplicate_spell(CuTest * tc) test_setup(); test_log_stderr(0); - log = log_create(LOG_CPERROR, &sl, log_list); + log = test_log_start(LOG_CPERROR, &sl); CuAssertPtrEquals(tc, 0, find_spell("testspell")); @@ -47,9 +42,8 @@ static void test_create_duplicate_spell(CuTest * tc) CuAssertPtrNotNull(tc, sl); CuAssertStrEquals(tc, "create_spell: duplicate name '%s'", sl->s); CuAssertPtrEquals(tc, 0, sl->next); - freestrlist(sl); - log_destroy(log); CuAssertPtrEquals(tc, sp, find_spell("testspell")); + test_log_stop(log, sl); test_cleanup(); } @@ -61,18 +55,17 @@ static void test_create_spell_with_id(CuTest * tc) test_setup(); test_log_stderr(0); - log = log_create(LOG_CPERROR, &sl, log_list); + log = test_log_start(LOG_CPERROR, &sl); CuAssertPtrEquals(tc, 0, find_spellbyid(42)); sp = create_spell("testspell", 42); CuAssertPtrEquals(tc, sp, find_spellbyid(42)); CuAssertPtrEquals(tc, 0, create_spell("testspell", 47)); + CuAssertPtrEquals(tc, 0, find_spellbyid(47)); CuAssertPtrNotNull(tc, sl); CuAssertStrEquals(tc, "create_spell: duplicate name '%s'", sl->s); CuAssertPtrEquals(tc, 0, sl->next); - freestrlist(sl); - log_destroy(log); - CuAssertPtrEquals(tc, 0, find_spellbyid(47)); + test_log_stop(log, sl); test_cleanup(); } diff --git a/src/tests.c b/src/tests.c index 54779c450..13400ebd4 100644 --- a/src/tests.c +++ b/src/tests.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -123,6 +124,20 @@ struct unit *test_create_unit(struct faction *f, struct region *r) return create_unit(r, f, 1, rc ? rc : rc_get_or_create("human"), 0, 0, 0); } +static void log_list(void *udata, int flags, const char *module, const char *format, va_list args) { + strlist **slp = (strlist **)udata; + addstrlist(slp, format); +} + +struct log_t * test_log_start(int flags, strlist **slist) { + return log_create(flags, slist, log_list); +} + +void test_log_stop(struct log_t *log, struct strlist *slist) { + freestrlist(slist); + log_destroy(log); +} + void test_log_stderr(int flags) { static struct log_t *stderrlog; if (flags) { diff --git a/src/tests.h b/src/tests.h index a025c92c1..10934c3d7 100644 --- a/src/tests.h +++ b/src/tests.h @@ -26,12 +26,16 @@ extern "C" { struct spellparameter; struct spell; struct locale; + struct strlist; + struct log_t; struct CuTest; void test_setup(void); void test_cleanup(void); void test_log_stderr(int on); + struct log_t * test_log_start(int flags, struct strlist **slist); + void test_log_stop(struct log_t *log, struct strlist *slist); struct locale * test_create_locale(void); struct terrain_type * test_create_terrain(const char * name, unsigned int flags);