From 182d9995cac0553ea1bd50db56885c39c4fcb813 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 5 Aug 2021 11:51:18 +0100 Subject: [PATCH] gtestutils: Allow skipping tests with a printf-style message Forming the g_test_skip() message from printf-style arguments seems common enough to deserve a convenience function. g_test_incomplete() is mechanically almost equivalent to g_test_skip() (the semantics are different but the implementation is very similar), so give it a similar mechanism for symmetry. Signed-off-by: Simon McVittie --- docs/reference/glib/glib-sections.txt | 2 ++ glib/gtestutils.c | 46 +++++++++++++++++++++++++++ glib/gtestutils.h | 6 ++++ glib/tests/testing-helper.c | 24 ++++++++++++++ glib/tests/testing.c | 38 ++++++++++++++++++++++ 5 files changed, 116 insertions(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 8fdf065a1..6e4216809 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -3570,7 +3570,9 @@ g_test_get_dir g_test_fail g_test_skip +g_test_skip_printf g_test_incomplete +g_test_incomplete_printf g_test_failed g_test_message g_test_bug_base diff --git a/glib/gtestutils.c b/glib/gtestutils.c index 8aafc703b..f27ad2636 100644 --- a/glib/gtestutils.c +++ b/glib/gtestutils.c @@ -2434,6 +2434,29 @@ g_test_incomplete (const gchar *msg) test_run_msg = g_strdup (msg); } +/** + * g_test_incomplete_printf: + * @format: the format string + * @...: printf-like arguments to @format + * + * Equivalent to g_test_incomplete(), but the explanation is formatted + * as if by g_strdup_printf(). + * + * Since: 2.70 + */ +void +g_test_incomplete_printf (const char *format, + ...) +{ + va_list args; + + test_run_success = G_TEST_RUN_INCOMPLETE; + va_start (args, format); + g_free (test_run_msg); + test_run_msg = g_strdup_vprintf (format, args); + va_end (args); +} + /** * g_test_skip: * @msg: (nullable): explanation @@ -2457,6 +2480,29 @@ g_test_skip (const gchar *msg) test_run_msg = g_strdup (msg); } +/** + * g_test_skip_printf: + * @format: the format string + * @...: printf-like arguments to @format + * + * Equivalent to g_test_skip(), but the explanation is formatted + * as if by g_strdup_printf(). + * + * Since: 2.70 + */ +void +g_test_skip_printf (const char *format, + ...) +{ + va_list args; + + test_run_success = G_TEST_RUN_SKIPPED; + va_start (args, format); + g_free (test_run_msg); + test_run_msg = g_strdup_vprintf (format, args); + va_end (args); +} + /** * g_test_failed: * diff --git a/glib/gtestutils.h b/glib/gtestutils.h index 5be4ce215..524344b72 100644 --- a/glib/gtestutils.h +++ b/glib/gtestutils.h @@ -347,8 +347,14 @@ GLIB_AVAILABLE_IN_2_30 void g_test_fail (void); GLIB_AVAILABLE_IN_2_38 void g_test_incomplete (const gchar *msg); +GLIB_AVAILABLE_IN_2_70 +void g_test_incomplete_printf (const char *format, + ...) G_GNUC_PRINTF (1, 2); GLIB_AVAILABLE_IN_2_38 void g_test_skip (const gchar *msg); +GLIB_AVAILABLE_IN_2_70 +void g_test_skip_printf (const char *format, + ...) G_GNUC_PRINTF (1, 2); GLIB_AVAILABLE_IN_2_38 gboolean g_test_failed (void); GLIB_AVAILABLE_IN_2_38 diff --git a/glib/tests/testing-helper.c b/glib/tests/testing-helper.c index cc6afe589..9c30b01b8 100644 --- a/glib/tests/testing-helper.c +++ b/glib/tests/testing-helper.c @@ -35,6 +35,14 @@ test_skip (void) g_test_skip ("not enough tea"); } +static void +test_skip_printf (void) +{ + const char *beverage = "coffee"; + + g_test_skip_printf ("not enough %s", beverage); +} + static void test_fail (void) { @@ -47,6 +55,14 @@ test_incomplete (void) g_test_incomplete ("mind reading not implemented yet"); } +static void +test_incomplete_printf (void) +{ + const char *operation = "telekinesis"; + + g_test_incomplete_printf ("%s not implemented yet", operation); +} + static void test_summary (void) { @@ -91,10 +107,18 @@ main (int argc, { g_test_add_func ("/skip", test_skip); } + else if (g_strcmp0 (argv1, "skip-printf") == 0) + { + g_test_add_func ("/skip-printf", test_skip_printf); + } else if (g_strcmp0 (argv1, "incomplete") == 0) { g_test_add_func ("/incomplete", test_incomplete); } + else if (g_strcmp0 (argv1, "incomplete-printf") == 0) + { + g_test_add_func ("/incomplete-printf", test_incomplete_printf); + } else if (g_strcmp0 (argv1, "fail") == 0) { g_test_add_func ("/fail", test_fail); diff --git a/glib/tests/testing.c b/glib/tests/testing.c index fd10f4891..a646f7c71 100644 --- a/glib/tests/testing.c +++ b/glib/tests/testing.c @@ -1100,6 +1100,25 @@ test_tap (void) g_free (output); g_ptr_array_unref (argv); + g_test_message ("skip with printf format"); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "skip-printf"); + 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); + g_assert_nonnull (strstr (output, "\nok 1 /skip-printf # SKIP not enough coffee\n")); + g_free (output); + g_ptr_array_unref (argv); + g_test_message ("incomplete"); argv = g_ptr_array_new (); g_ptr_array_add (argv, (char *) testing_helper); @@ -1119,6 +1138,25 @@ test_tap (void) g_free (output); g_ptr_array_unref (argv); + g_test_message ("incomplete with printf format"); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "incomplete-printf"); + 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); + g_assert_nonnull (strstr (output, "\nnot ok 1 /incomplete-printf # TODO telekinesis not implemented yet\n")); + g_free (output); + g_ptr_array_unref (argv); + g_test_message ("fail"); argv = g_ptr_array_new (); g_ptr_array_add (argv, (char *) testing_helper);