gtestutils: Write g_test_message() output in a single operation

Do not write it in multiple lines, to ensure it's going to be written
all together, and nothing else could be written in the middle.

Also optimize a bit the code.
This commit is contained in:
Marco Trevisan (Treviño) 2023-01-13 23:20:08 +01:00
parent 78a206f467
commit 250f3f0644
3 changed files with 29 additions and 10 deletions

View File

@ -1094,17 +1094,34 @@ g_test_log (GTestLogType lbit,
case G_TEST_LOG_MESSAGE:
if (test_tap_log)
{
if (strstr (string1, "\n") == NULL)
if (strchr (string1, '\n') == NULL)
g_print ("# %s\n", string1);
else
{
char **lines = g_strsplit (string1, "\n", -1);
gsize i;
GString *output = g_string_new (NULL);
const char *line = string1;
for (i = 0; lines[i] != NULL; i++)
g_print ("# %s\n", lines[i]);
do
{
const char *next = strchr (line, '\n');
g_string_append (output, "# ");
g_strfreev (lines);
if (next)
{
g_string_append_len (output, line, next - line + 1);
line = next + 1;
}
else
{
g_string_append (output, line);
g_string_append_c (output, '\n');
line = next;
}
}
while (line != NULL);
g_print ("%s", output->str);
g_string_free (g_steal_pointer (&output), TRUE);
}
}
else if (g_test_verbose ())

View File

@ -83,8 +83,8 @@ static void
test_message (void)
{
g_test_message ("Tests that single line message works");
g_test_message ("Tests that multi\nline\nmessage\nworks");
g_test_message ("Tests that multi\nline\nmessage\nworks with trailing too\n");
g_test_message ("Tests that multi\n\nline\nmessage\nworks");
g_test_message ("\nTests that multi\nline\nmessage\nworks with leading and trailing too\n");
}
int

View File

@ -1676,18 +1676,20 @@ test_tap_message (void)
interesting_lines += strlen (expected_tap_header);
output_lines = g_strsplit (interesting_lines, "\n", -1);
g_assert_cmpuint (g_strv_length (output_lines), >=, 10);
g_assert_cmpuint (g_strv_length (output_lines), >=, 12);
guint i = 0;
g_assert_cmpstr (output_lines[i++], ==, "# Tests that single line message works");
g_assert_cmpstr (output_lines[i++], ==, "# Tests that multi");
g_assert_cmpstr (output_lines[i++], ==, "# ");
g_assert_cmpstr (output_lines[i++], ==, "# line");
g_assert_cmpstr (output_lines[i++], ==, "# message");
g_assert_cmpstr (output_lines[i++], ==, "# works");
g_assert_cmpstr (output_lines[i++], ==, "# ");
g_assert_cmpstr (output_lines[i++], ==, "# Tests that multi");
g_assert_cmpstr (output_lines[i++], ==, "# line");
g_assert_cmpstr (output_lines[i++], ==, "# message");
g_assert_cmpstr (output_lines[i++], ==, "# works with trailing too");
g_assert_cmpstr (output_lines[i++], ==, "# works with leading and trailing too");
g_assert_cmpstr (output_lines[i++], ==, "# ");
g_free (output);