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>
This commit is contained in:
Simon McVittie 2021-08-05 11:51:18 +01:00
parent ae486f6dc6
commit 182d9995ca
5 changed files with 116 additions and 0 deletions

View File

@ -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

View File

@ -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:
*

View File

@ -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

View File

@ -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);

View File

@ -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);