Move memory allocation into test methods

This implies, that memory will only get freed, if the whole test method
is gone through. In case of failure, memory leaks aren't important. But
it's crucial not to fail with a segfault just for an assertion.

Fixes #580
This commit is contained in:
Benedikt Heine 2019-01-03 12:21:05 +01:00
parent 84e5a0bf26
commit c37326c9a0

View File

@ -19,16 +19,28 @@ TEST test_notification_is_duplicate_field(char **field,
PASS(); PASS();
} }
TEST test_notification_is_duplicate(struct notification *a, TEST test_notification_is_duplicate(void)
struct notification *b)
{ {
ASSERT(notification_is_duplicate(a, b)); struct notification *a = notification_create();
a->appname = g_strdup("Test");
a->summary = g_strdup("Summary");
a->body = g_strdup("Body");
a->icon = g_strdup("Icon");
a->urgency = URG_NORM;
struct notification *b = notification_create();
b->appname = g_strdup("Test");
b->summary = g_strdup("Summary");
b->body = g_strdup("Body");
b->icon = g_strdup("Icon");
b->urgency = URG_NORM;
CHECK_CALL(test_notification_is_duplicate_field(&(b->appname), a, b)); CHECK_CALL(test_notification_is_duplicate_field(&(b->appname), a, b));
CHECK_CALL(test_notification_is_duplicate_field(&(b->summary), a, b)); CHECK_CALL(test_notification_is_duplicate_field(&(b->summary), a, b));
CHECK_CALL(test_notification_is_duplicate_field(&(b->body), a, b)); CHECK_CALL(test_notification_is_duplicate_field(&(b->body), a, b));
ASSERT(notification_is_duplicate(a, b)); ASSERTm("One of the notifications got corrupted during test",
notification_is_duplicate(a, b));
enum icon_position icon_setting_tmp = settings.icon_position; enum icon_position icon_setting_tmp = settings.icon_position;
@ -62,6 +74,8 @@ TEST test_notification_is_duplicate(struct notification *a,
b->urgency = URG_CRIT; b->urgency = URG_CRIT;
ASSERT_FALSE(notification_is_duplicate(a, b)); ASSERT_FALSE(notification_is_duplicate(a, b));
notification_unref(a);
notification_unref(b);
PASS(); PASS();
} }
@ -150,30 +164,12 @@ SUITE(suite_notification)
char *config_path = g_strconcat(base, "/data/dunstrc.default", NULL); char *config_path = g_strconcat(base, "/data/dunstrc.default", NULL);
load_settings(config_path); load_settings(config_path);
struct notification *a = notification_create(); RUN_TEST(test_notification_is_duplicate);
a->appname = g_strdup("Test");
a->summary = g_strdup("Summary");
a->body = g_strdup("Body");
a->icon = g_strdup("Icon");
a->urgency = URG_NORM;
struct notification *b = notification_create();
b->appname = g_strdup("Test");
b->summary = g_strdup("Summary");
b->body = g_strdup("Body");
b->icon = g_strdup("Icon");
b->urgency = URG_NORM;
//2 equal notifications to be passed for duplicate checking,
RUN_TESTp(test_notification_is_duplicate, a, b);
g_clear_pointer(&a, notification_unref);
g_clear_pointer(&b, notification_unref);
RUN_TEST(test_notification_replace_single_field); RUN_TEST(test_notification_replace_single_field);
RUN_TEST(test_notification_referencing); RUN_TEST(test_notification_referencing);
// TEST notification_format_message // TEST notification_format_message
a = notification_create(); struct notification *a = notification_create();
a->appname = g_strdup("MyApp"); a->appname = g_strdup("MyApp");
a->summary = g_strdup("I've got a summary!"); a->summary = g_strdup("I've got a summary!");
a->body = g_strdup("Look at my shiny <notification>"); a->body = g_strdup("Look at my shiny <notification>");