From 78a206f467dc3387a3116df587a1084a9d052bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 13 Jan 2023 23:16:59 +0100 Subject: [PATCH] tests/testing, testing-helper: Add test to check g_test_message on TAP Ensure that g_test_message() output is conformant to TAP specifications. --- glib/tests/testing-helper.c | 12 ++++++++ glib/tests/testing.c | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/glib/tests/testing-helper.c b/glib/tests/testing-helper.c index 3d435e588..647aabf4e 100644 --- a/glib/tests/testing-helper.c +++ b/glib/tests/testing-helper.c @@ -79,6 +79,14 @@ test_summary (void) "it in the TAP output later."); } +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"); +} + int main (int argc, char *argv[]) @@ -185,6 +193,10 @@ main (int argc, { g_test_add_func ("/summary", test_summary); } + else if (g_strcmp0 (argv1, "message") == 0) + { + g_test_add_func ("/message", test_message); + } else { g_assert_not_reached (); diff --git a/glib/tests/testing.c b/glib/tests/testing.c index d9e8522b2..28f92cec1 100644 --- a/glib/tests/testing.c +++ b/glib/tests/testing.c @@ -1641,6 +1641,60 @@ test_tap_summary (void) g_ptr_array_unref (argv); } +static void +test_tap_message (void) +{ + const char *testing_helper; + GPtrArray *argv; + GError *error = NULL; + int status; + gchar *output; + char **output_lines; + + g_test_summary ("Test the output of g_test_message() from the TAP output of a test."); + + testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL); + + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "message"); + g_ptr_array_add (argv, "--tap"); + g_ptr_array_add (argv, NULL); + + g_spawn_sync (NULL, (char **) argv->pdata, NULL, + G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, &output, NULL, &status, + &error); + g_assert_no_error (error); + + g_spawn_check_wait_status (status, &error); + g_assert_no_error (error); + + const char *expected_tap_header = "\n1..1\n"; + const char *interesting_lines = strstr (output, expected_tap_header); + g_assert_nonnull (interesting_lines); + interesting_lines += strlen (expected_tap_header); + + output_lines = g_strsplit (interesting_lines, "\n", -1); + g_assert_cmpuint (g_strv_length (output_lines), >=, 10); + + 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++], ==, "# line"); + g_assert_cmpstr (output_lines[i++], ==, "# message"); + g_assert_cmpstr (output_lines[i++], ==, "# works"); + 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++], ==, "# "); + + g_free (output); + g_strfreev (output_lines); + g_ptr_array_unref (argv); +} + static void test_init_no_argv0 (void) { @@ -1771,6 +1825,7 @@ main (int argc, g_test_add_func ("/tap", test_tap); g_test_add_func ("/tap/summary", test_tap_summary); + g_test_add_func ("/tap/message", test_tap_message); g_test_add_func ("/init/no_argv0", test_init_no_argv0);