gmessages: fix g_test_expect_message() with NULL domains

Allow passing a NULL domain to g_test_expect_message(), and more
importantly, don't crash if a message with a NULL domain gets logged
while there is an expected message.
This commit is contained in:
Dan Winship 2013-08-31 11:51:07 -04:00
parent cde9f4598b
commit f8bb08305c
2 changed files with 15 additions and 3 deletions

View File

@ -894,7 +894,7 @@ g_logv (const gchar *log_domain,
expected_messages = g_slist_delete_link (expected_messages,
expected_messages);
if (strcmp (expected->log_domain, log_domain) == 0 &&
if (g_strcmp0 (expected->log_domain, log_domain) == 0 &&
((log_level & expected->log_level) == expected->log_level) &&
g_pattern_match_simple (expected->pattern, msg))
{
@ -1080,7 +1080,7 @@ g_assert_warning (const char *log_domain,
/**
* g_test_expect_message:
* @log_domain: the log domain of the message
* @log_domain: (allow-none): the log domain of the message
* @log_level: the log level of the message
* @pattern: a glob-style
* <link linkend="glib-Glob-style-pattern-matching">pattern</link>
@ -1123,7 +1123,6 @@ g_test_expect_message (const gchar *log_domain,
{
GTestExpectedMessage *expected;
g_return_if_fail (log_domain != NULL);
g_return_if_fail (log_level != 0);
g_return_if_fail (pattern != NULL);

View File

@ -372,6 +372,14 @@ test_expected_messages_expected (void)
exit (0);
}
static void
test_expected_messages_null_domain (void)
{
g_test_expect_message (NULL, G_LOG_LEVEL_WARNING, "no domain");
g_log (NULL, G_LOG_LEVEL_WARNING, "no domain");
g_test_assert_expected_messages ();
}
static void
test_expected_messages_extra_warning (void)
{
@ -426,6 +434,10 @@ test_expected_messages (void)
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("");
g_test_trap_subprocess ("/misc/expected-messages/subprocess/null-domain", 0, 0);
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("");
g_test_trap_subprocess ("/misc/expected-messages/subprocess/extra-warning", 0, 0);
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("");
@ -575,6 +587,7 @@ main (int argc,
g_test_add_func ("/misc/expected-messages/subprocess/expect-warning", test_expected_messages_expect_warning);
g_test_add_func ("/misc/expected-messages/subprocess/wrong-warning", test_expected_messages_wrong_warning);
g_test_add_func ("/misc/expected-messages/subprocess/expected", test_expected_messages_expected);
g_test_add_func ("/misc/expected-messages/subprocess/null-domain", test_expected_messages_null_domain);
g_test_add_func ("/misc/expected-messages/subprocess/extra-warning", test_expected_messages_extra_warning);
g_test_add_func ("/misc/expected-messages/subprocess/unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);