allow control over stderr logging from tests.

separate test_cleanup from test_setup.
This commit is contained in:
Enno Rehling 2016-08-29 14:09:47 +01:00
parent 2eb9b5c5ea
commit 1c5fcded9f
4 changed files with 36 additions and 7 deletions

View File

@ -13,13 +13,14 @@ static void test_create_a_spell(CuTest * tc)
{
spell * sp;
test_cleanup();
test_setup();
CuAssertPtrEquals(tc, 0, spells);
CuAssertPtrEquals(tc, 0, find_spell("testspell"));
sp = create_spell("testspell", 0);
CuAssertPtrEquals(tc, sp, find_spell("testspell"));
CuAssertPtrNotNull(tc, spells);
test_cleanup();
}
static void test_create_duplicate_spell(CuTest * tc)
@ -27,12 +28,13 @@ static void test_create_duplicate_spell(CuTest * tc)
spell *sp;
/* FIXME: this test emits ERROR messages (duplicate spells), inject a logger to verify that */
test_cleanup();
test_setup();
CuAssertPtrEquals(tc, 0, find_spell("testspell"));
sp = create_spell("testspell", 0);
CuAssertPtrEquals(tc, 0, create_spell("testspell", 0));
CuAssertPtrEquals(tc, sp, find_spell("testspell"));
test_cleanup();
}
static void test_create_spell_with_id(CuTest * tc)
@ -40,12 +42,13 @@ static void test_create_spell_with_id(CuTest * tc)
spell *sp;
/* FIXME: this test emits ERROR messages (duplicate spells), inject a logger to verify that */
test_cleanup();
test_setup();
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));
test_cleanup();
}
CuSuite *get_spell_suite(void)

View File

@ -158,7 +158,6 @@ int RunAllTests(int argc, char *argv[])
}
int main(int argc, char ** argv) {
log_to_file(LOG_CPERROR, stderr);
++argv;
--argc;
if (argc > 0 && strcmp("--list", argv[0]) == 0) {

View File

@ -123,10 +123,24 @@ 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);
}
void test_cleanup(void)
{
int i;
void test_log_stderr(int flags) {
static struct log_t *stderrlog;
if (flags) {
if (stderrlog) {
log_error("stderr logging is still active. did you call test_cleanup?");
log_destroy(stderrlog);
}
stderrlog = log_to_file(flags, stderr);
}
else {
log_destroy(stderrlog);
stderrlog = 0;
}
}
static void test_reset(void) {
int i;
default_locale = 0;
free_gamedata();
free_terrains();
@ -158,6 +172,17 @@ void test_cleanup(void)
random_source_reset();
}
void test_setup(void) {
test_log_stderr(LOG_CPERROR);
test_reset();
}
void test_cleanup(void)
{
test_reset();
test_log_stderr(0);
}
terrain_type *
test_create_terrain(const char * name, unsigned int flags)
{

View File

@ -29,7 +29,9 @@ extern "C" {
struct CuTest;
void test_setup(void);
void test_cleanup(void);
void test_log_stderr(int on);
struct locale * test_create_locale(void);
struct terrain_type * test_create_terrain(const char * name, unsigned int flags);