From 070ed1a9416f546cf918edcbfb8f7e74745ece80 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Oct 2015 12:38:05 +0200 Subject: [PATCH 1/3] clean up hastily written tests, eliminate duplicate code --- src/laws.test.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/laws.test.c b/src/laws.test.c index 431c03845..c7f974de9 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -989,9 +989,7 @@ static void test_nmr_warnings(CuTest *tc) { test_cleanup(); } -static void test_mail_cmd(CuTest *tc) { - unit *u; - order *ord; +static unit * setup_mail_cmd(void) { faction *f; struct locale *lang; @@ -999,30 +997,32 @@ static void test_mail_cmd(CuTest *tc) { f = test_create_faction(0); f->locale = lang = get_or_create_locale("de"); locale_setstring(lang, parameters[P_UNIT], "EINHEIT"); + locale_setstring(lang, parameters[P_REGION], "REGION"); + locale_setstring(lang, parameters[P_FACTION], "PARTEI"); init_parameters(lang); - u = test_create_unit(f, test_create_region(0, 0, 0)); + return test_create_unit(f, test_create_region(0, 0, 0)); +} + +static void test_mail_cmd(CuTest *tc) { + order *ord; + unit *u; + + u = setup_mail_cmd(); ord = create_order(K_MAIL, u->faction->locale, "EINHEIT %s 'Hodor!'", itoa36(u->no)); mail_cmd(u, ord); - CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "unitmessage")); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "unitmessage")); test_cleanup(); } static void test_mail_cmd_no_msg(CuTest *tc) { unit *u; order *ord; - faction *f; - struct locale *lang; - test_cleanup(); - f = test_create_faction(0); - f->locale = lang = get_or_create_locale("de"); - locale_setstring(lang, parameters[P_UNIT], "EINHEIT"); - init_parameters(lang); - u = test_create_unit(f, test_create_region(0, 0, 0)); + u = setup_mail_cmd(); ord = create_order(K_MAIL, u->faction->locale, "EINHEIT %s", itoa36(u->no)); mail_cmd(u, ord); - CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "unitmessage")); - CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error30")); + CuAssertPtrEquals(tc, 0, test_find_messagetype(u->faction->msgs, "unitmessage")); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error30")); test_cleanup(); } From 403b8a2e7ea878243a8101b392b5f122c0e067fb Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Oct 2015 12:44:22 +0200 Subject: [PATCH 2/3] add mail_cmd tests for REGION --- src/laws.test.c | 33 +++++++++++++++++++++++++++++---- src/tests.c | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/laws.test.c b/src/laws.test.c index c7f974de9..87b455330 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -1003,7 +1003,7 @@ static unit * setup_mail_cmd(void) { return test_create_unit(f, test_create_region(0, 0, 0)); } -static void test_mail_cmd(CuTest *tc) { +static void test_mail_unit(CuTest *tc) { order *ord; unit *u; @@ -1014,7 +1014,18 @@ static void test_mail_cmd(CuTest *tc) { test_cleanup(); } -static void test_mail_cmd_no_msg(CuTest *tc) { +static void test_mail_region(CuTest *tc) { + order *ord; + unit *u; + + u = setup_mail_cmd(); + ord = create_order(K_MAIL, u->faction->locale, "REGION 'Hodor!'", itoa36(u->no)); + mail_cmd(u, ord); + CuAssertPtrNotNull(tc, test_find_messagetype(u->region->msgs, "mail_result")); + test_cleanup(); +} + +static void test_mail_unit_no_msg(CuTest *tc) { unit *u; order *ord; @@ -1026,6 +1037,18 @@ static void test_mail_cmd_no_msg(CuTest *tc) { test_cleanup(); } +static void test_mail_region_no_msg(CuTest *tc) { + unit *u; + order *ord; + + u = setup_mail_cmd(); + ord = create_order(K_MAIL, u->faction->locale, "REGION"); + mail_cmd(u, ord); + CuAssertPtrEquals(tc, 0, test_find_messagetype(u->region->msgs, "mail_result")); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error30")); + test_cleanup(); +} + CuSuite *get_laws_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -1070,8 +1093,10 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_force_leave_ships); SUITE_ADD_TEST(suite, test_force_leave_ships_on_ocean); SUITE_ADD_TEST(suite, test_peasant_luck_effect); - SUITE_ADD_TEST(suite, test_mail_cmd); - SUITE_ADD_TEST(suite, test_mail_cmd_no_msg); + SUITE_ADD_TEST(suite, test_mail_unit); + SUITE_ADD_TEST(suite, test_mail_region); + SUITE_ADD_TEST(suite, test_mail_unit_no_msg); + SUITE_ADD_TEST(suite, test_mail_region_no_msg); (void)test_luck_message; /* disabled, breaks on travis */ return suite; diff --git a/src/tests.c b/src/tests.c index bcbd18c29..82417e9d2 100644 --- a/src/tests.c +++ b/src/tests.c @@ -257,7 +257,7 @@ const char * test_get_messagetype(const message *msg) { struct message * test_find_messagetype(struct message_list *msgs, const char *name) { struct mlist *ml; - assert(msgs); + if (!msgs) return 0; for (ml = msgs->begin; ml; ml = ml->next) { if (strcmp(name, test_get_messagetype(ml->msg)) == 0) { return ml->msg; From c47c9dbf6442645fdc46a1abaef11e1d3cbf360f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Oct 2015 12:52:13 +0200 Subject: [PATCH 3/3] add mail_cmd tests for FACTION --- src/laws.test.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/laws.test.c b/src/laws.test.c index 87b455330..c7c3bf632 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -1014,6 +1014,17 @@ static void test_mail_unit(CuTest *tc) { test_cleanup(); } +static void test_mail_faction(CuTest *tc) { + order *ord; + unit *u; + + u = setup_mail_cmd(); + ord = create_order(K_MAIL, u->faction->locale, "PARTEI %s 'Hodor!'", itoa36(u->faction->no)); + mail_cmd(u, ord); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "regionmessage")); + test_cleanup(); +} + static void test_mail_region(CuTest *tc) { order *ord; unit *u; @@ -1037,6 +1048,30 @@ static void test_mail_unit_no_msg(CuTest *tc) { test_cleanup(); } +static void test_mail_faction_no_msg(CuTest *tc) { + unit *u; + order *ord; + + u = setup_mail_cmd(); + ord = create_order(K_MAIL, u->faction->locale, "PARTEI %s", itoa36(u->faction->no)); + mail_cmd(u, ord); + CuAssertPtrEquals(tc, 0, test_find_messagetype(u->faction->msgs, "regionmessage")); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error30")); + test_cleanup(); +} + +static void test_mail_faction_no_target(CuTest *tc) { + unit *u; + order *ord; + + u = setup_mail_cmd(); + ord = create_order(K_MAIL, u->faction->locale, "PARTEI %s", itoa36(u->faction->no+1)); + mail_cmd(u, ord); + CuAssertPtrEquals(tc, 0, test_find_messagetype(u->faction->msgs, "regionmessage")); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error66")); + test_cleanup(); +} + static void test_mail_region_no_msg(CuTest *tc) { unit *u; order *ord; @@ -1094,9 +1129,12 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_force_leave_ships_on_ocean); SUITE_ADD_TEST(suite, test_peasant_luck_effect); SUITE_ADD_TEST(suite, test_mail_unit); + SUITE_ADD_TEST(suite, test_mail_faction); SUITE_ADD_TEST(suite, test_mail_region); SUITE_ADD_TEST(suite, test_mail_unit_no_msg); + SUITE_ADD_TEST(suite, test_mail_faction_no_msg); SUITE_ADD_TEST(suite, test_mail_region_no_msg); + SUITE_ADD_TEST(suite, test_mail_faction_no_target); (void)test_luck_message; /* disabled, breaks on travis */ return suite;