From 1c5fcded9f9262042ce258ff4908f21d6c9818b7 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 29 Aug 2016 14:09:47 +0100 Subject: [PATCH] allow control over stderr logging from tests. separate test_cleanup from test_setup. --- src/kernel/spell.test.c | 9 ++++++--- src/test_eressea.c | 1 - src/tests.c | 31 ++++++++++++++++++++++++++++--- src/tests.h | 2 ++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/kernel/spell.test.c b/src/kernel/spell.test.c index 0c0beb6ce..60665a591 100644 --- a/src/kernel/spell.test.c +++ b/src/kernel/spell.test.c @@ -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) diff --git a/src/test_eressea.c b/src/test_eressea.c index 8236465cf..3001ee2b8 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -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) { diff --git a/src/tests.c b/src/tests.c index 53ebe8a28..5d359813d 100644 --- a/src/tests.c +++ b/src/tests.c @@ -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) { diff --git a/src/tests.h b/src/tests.h index e31578beb..a025c92c1 100644 --- a/src/tests.h +++ b/src/tests.h @@ -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);