diff options
author | Simon McVittie <smcv@collabora.com> | 2021-08-05 11:51:18 +0100 |
---|---|---|
committer | Simon McVittie <smcv@collabora.com> | 2021-08-05 15:59:22 +0100 |
commit | a33d8ffd5095861eddce3ccebe0ed8ee0bff844e (patch) | |
tree | 86718687b64d68d20f28d2a9273b65202cd8a9e1 | |
parent | e43c83bc412edec5f61ff51f382c80d9fef36432 (diff) | |
download | glib-a33d8ffd5095861eddce3ccebe0ed8ee0bff844e.tar.gz |
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 <smcv@collabora.com>
-rw-r--r-- | docs/reference/glib/glib-sections.txt | 2 | ||||
-rw-r--r-- | glib/gtestutils.c | 46 | ||||
-rw-r--r-- | glib/gtestutils.h | 6 | ||||
-rw-r--r-- | glib/tests/testing-helper.c | 24 | ||||
-rw-r--r-- | glib/tests/testing.c | 38 |
5 files changed, 116 insertions, 0 deletions
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 37cc7771f..d0a87be4f 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -3571,7 +3571,9 @@ g_test_get_dir g_test_fail g_test_fail_message 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 c56ae8ddd..f4366c903 100644 --- a/glib/gtestutils.c +++ b/glib/gtestutils.c @@ -2460,6 +2460,29 @@ g_test_incomplete (const gchar *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 * @@ -2483,6 +2506,29 @@ g_test_skip (const gchar *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: * * Returns whether a test has already failed. This will diff --git a/glib/gtestutils.h b/glib/gtestutils.h index 5acdf3f2c..2998b9bf3 100644 --- a/glib/gtestutils.h +++ b/glib/gtestutils.h @@ -350,8 +350,14 @@ void g_test_fail_message (const char *format, ...) G_GNUC_PRINTF (1, 2); 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 a3a2089c7..5321ab1cf 100644 --- a/glib/tests/testing-helper.c +++ b/glib/tests/testing-helper.c @@ -36,6 +36,14 @@ test_skip (void) } static void +test_skip_printf (void) +{ + const char *beverage = "coffee"; + + g_test_skip_printf ("not enough %s", beverage); +} + +static void test_fail (void) { g_test_fail (); @@ -54,6 +62,14 @@ test_incomplete (void) } static void +test_incomplete_printf (void) +{ + const char *operation = "telekinesis"; + + g_test_incomplete_printf ("%s not implemented yet", operation); +} + +static void test_summary (void) { g_test_summary ("Tests that g_test_summary() works with TAP, by outputting a " @@ -97,10 +113,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 7554fc5aa..67be00445 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); |