promote spell.test error logging to a tests.c API, use it for item.test

This commit is contained in:
Enno Rehling 2016-08-29 15:19:17 +01:00
parent 2c5063095d
commit fb2b6495e5
5 changed files with 43 additions and 19 deletions

View File

@ -491,7 +491,7 @@ item *i_change(item ** pi, const item_type * itype, int delta)
item *i = *pi; item *i = *pi;
i->number += delta; i->number += delta;
if (i->number < 0) { 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; i->number = 0;
} }
if (i->number == 0) { if (i->number == 0) {

View File

@ -6,6 +6,8 @@
#include <kernel/terrain.h> #include <kernel/terrain.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <util/language.h> #include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/functions.h> #include <util/functions.h>
#include <CuTest.h> #include <CuTest.h>
@ -45,8 +47,11 @@ static void test_resourcename_with_appearance(CuTest *tc) {
static void test_uchange(CuTest * tc, unit * u, const resource_type * rtype) { static void test_uchange(CuTest * tc, unit * u, const resource_type * rtype) {
int n; int n;
struct log_t *log;
strlist *sl = 0;
assert(rtype); assert(rtype);
log = test_log_start(LOG_CPERROR, &sl);
change_resource(u, rtype, 4); change_resource(u, rtype, 4);
n = get_resource(u, rtype); n = get_resource(u, rtype);
CuAssertPtrNotNull(tc, rtype->uchange); 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, rtype->uchange(u, rtype, -3));
CuAssertIntEquals(tc, n - 3, get_resource(u, rtype)); CuAssertIntEquals(tc, n - 3, get_resource(u, rtype));
CuAssertIntEquals(tc, 0, rtype->uchange(u, rtype, -n)); 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) void test_change_item(CuTest * tc)
{ {
unit * u; unit * u;
test_cleanup(); test_setup();
test_log_stderr(0);
test_create_itemtype("iron"); test_create_itemtype("iron");
init_resources(); init_resources();
u = test_create_unit(test_create_faction(0), 0); u = test_create_unit(test_create_faction(0), 0);
test_uchange(tc, u, get_resourcetype(R_IRON)); test_uchange(tc, u, get_resourcetype(R_IRON));
test_cleanup();
} }
void test_resource_type(CuTest * tc) void test_resource_type(CuTest * tc)
{ {
struct item_type *itype; struct item_type *itype;
test_cleanup(); test_setup();
CuAssertPtrEquals(tc, 0, rt_find("herpderp")); CuAssertPtrEquals(tc, 0, rt_find("herpderp"));
@ -89,7 +100,7 @@ void test_finditemtype(CuTest * tc)
const item_type *itype; const item_type *itype;
struct locale * lang; struct locale * lang;
test_cleanup(); test_setup();
lang = get_or_create_locale("de"); lang = get_or_create_locale("de");
locale_setstring(lang, "horse", "Pferd"); locale_setstring(lang, "horse", "Pferd");
@ -103,7 +114,7 @@ void test_findresourcetype(CuTest * tc)
const item_type *itype; const item_type *itype;
struct locale * lang; struct locale * lang;
test_cleanup(); test_setup();
lang = get_or_create_locale("de"); lang = get_or_create_locale("de");
locale_setstring(lang, "horse", "Pferd"); locale_setstring(lang, "horse", "Pferd");
@ -124,7 +135,7 @@ static void test_fix_demand(CuTest *tc) {
terrain_type *tplain; terrain_type *tplain;
item_type *ltype; item_type *ltype;
test_cleanup(); test_setup();
ltype = test_create_itemtype("balm"); ltype = test_create_itemtype("balm");
ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED); ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED);
new_luxurytype(ltype, 0); new_luxurytype(ltype, 0);
@ -145,7 +156,8 @@ static void test_fix_demand(CuTest *tc) {
static void test_core_resources(CuTest *tc) { static void test_core_resources(CuTest *tc) {
resource_type * rtype; resource_type * rtype;
test_cleanup();
test_setup();
init_resources(); init_resources();
CuAssertPtrNotNull(tc, rtype = rt_find("money")); CuAssertPtrNotNull(tc, rtype = rt_find("money"));
CuAssertPtrNotNull(tc, rtype->itype); CuAssertPtrNotNull(tc, rtype->itype);

View File

@ -25,11 +25,6 @@ static void test_create_a_spell(CuTest * tc)
test_cleanup(); 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) static void test_create_duplicate_spell(CuTest * tc)
{ {
spell *sp; spell *sp;
@ -38,7 +33,7 @@ static void test_create_duplicate_spell(CuTest * tc)
test_setup(); test_setup();
test_log_stderr(0); 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")); CuAssertPtrEquals(tc, 0, find_spell("testspell"));
@ -47,9 +42,8 @@ static void test_create_duplicate_spell(CuTest * tc)
CuAssertPtrNotNull(tc, sl); CuAssertPtrNotNull(tc, sl);
CuAssertStrEquals(tc, "create_spell: duplicate name '%s'", sl->s); CuAssertStrEquals(tc, "create_spell: duplicate name '%s'", sl->s);
CuAssertPtrEquals(tc, 0, sl->next); CuAssertPtrEquals(tc, 0, sl->next);
freestrlist(sl);
log_destroy(log);
CuAssertPtrEquals(tc, sp, find_spell("testspell")); CuAssertPtrEquals(tc, sp, find_spell("testspell"));
test_log_stop(log, sl);
test_cleanup(); test_cleanup();
} }
@ -61,18 +55,17 @@ static void test_create_spell_with_id(CuTest * tc)
test_setup(); test_setup();
test_log_stderr(0); 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)); CuAssertPtrEquals(tc, 0, find_spellbyid(42));
sp = create_spell("testspell", 42); sp = create_spell("testspell", 42);
CuAssertPtrEquals(tc, sp, find_spellbyid(42)); CuAssertPtrEquals(tc, sp, find_spellbyid(42));
CuAssertPtrEquals(tc, 0, create_spell("testspell", 47)); CuAssertPtrEquals(tc, 0, create_spell("testspell", 47));
CuAssertPtrEquals(tc, 0, find_spellbyid(47));
CuAssertPtrNotNull(tc, sl); CuAssertPtrNotNull(tc, sl);
CuAssertStrEquals(tc, "create_spell: duplicate name '%s'", sl->s); CuAssertStrEquals(tc, "create_spell: duplicate name '%s'", sl->s);
CuAssertPtrEquals(tc, 0, sl->next); CuAssertPtrEquals(tc, 0, sl->next);
freestrlist(sl); test_log_stop(log, sl);
log_destroy(log);
CuAssertPtrEquals(tc, 0, find_spellbyid(47));
test_cleanup(); test_cleanup();
} }

View File

@ -24,6 +24,7 @@
#include <util/bsdstring.h> #include <util/bsdstring.h>
#include <util/functions.h> #include <util/functions.h>
#include <util/language.h> #include <util/language.h>
#include <util/lists.h>
#include <util/message.h> #include <util/message.h>
#include <util/log.h> #include <util/log.h>
#include <util/rand.h> #include <util/rand.h>
@ -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); 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) { void test_log_stderr(int flags) {
static struct log_t *stderrlog; static struct log_t *stderrlog;
if (flags) { if (flags) {

View File

@ -26,12 +26,16 @@ extern "C" {
struct spellparameter; struct spellparameter;
struct spell; struct spell;
struct locale; struct locale;
struct strlist;
struct log_t;
struct CuTest; struct CuTest;
void test_setup(void); void test_setup(void);
void test_cleanup(void); void test_cleanup(void);
void test_log_stderr(int on); 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 locale * test_create_locale(void);
struct terrain_type * test_create_terrain(const char * name, unsigned int flags); struct terrain_type * test_create_terrain(const char * name, unsigned int flags);