mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-26 07:26:15 +01:00
gtestutils: Allow failing a test with a printf-style message
This allows a pattern like g_test_message ("cannot reticulate splines: %s", error->message); g_test_fail (); to be replaced by the simpler g_test_fail_printf ("cannot reticulate splines: %s", error->message); with the secondary benefit of making the message available to TAP consumers as part of the "not ok" message. Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
parent
26fbd14954
commit
a076dbcb68
@ -3569,6 +3569,7 @@ g_test_get_filename
|
||||
g_test_get_dir
|
||||
|
||||
g_test_fail
|
||||
g_test_fail_printf
|
||||
g_test_skip
|
||||
g_test_skip_printf
|
||||
g_test_incomplete
|
||||
|
@ -1039,6 +1039,8 @@ g_test_log (GTestLogType lbit,
|
||||
g_print (" # TODO %s\n", string2 ? string2 : "");
|
||||
else if (result == G_TEST_RUN_SKIPPED)
|
||||
g_print (" # SKIP %s\n", string2 ? string2 : "");
|
||||
else if (result == G_TEST_RUN_FAILURE && string2 != NULL)
|
||||
g_print (" - %s\n", string2);
|
||||
else
|
||||
g_print ("\n");
|
||||
}
|
||||
@ -2401,12 +2403,42 @@ g_test_add_vtable (const char *testpath,
|
||||
*
|
||||
* If not called from inside a test, this function does nothing.
|
||||
*
|
||||
* Note that unlike g_test_skip() and g_test_incomplete(), this
|
||||
* function does not log a message alongside the test failure.
|
||||
* If details of the test failure are available, either log them with
|
||||
* g_test_message() before g_test_fail(), or use g_test_fail_printf()
|
||||
* instead.
|
||||
*
|
||||
* Since: 2.30
|
||||
**/
|
||||
void
|
||||
g_test_fail (void)
|
||||
{
|
||||
test_run_success = G_TEST_RUN_FAILURE;
|
||||
g_clear_pointer (&test_run_msg, g_free);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_test_fail_printf:
|
||||
* @format: the format string
|
||||
* @...: printf-like arguments to @format
|
||||
*
|
||||
* Equivalent to g_test_fail(), but also record a message like
|
||||
* g_test_skip_printf().
|
||||
*
|
||||
* Since: 2.70
|
||||
**/
|
||||
void
|
||||
g_test_fail_printf (const char *format,
|
||||
...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
test_run_success = G_TEST_RUN_FAILURE;
|
||||
va_start (args, format);
|
||||
g_free (test_run_msg);
|
||||
test_run_msg = g_strdup_vprintf (format, args);
|
||||
va_end (args);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -345,6 +345,9 @@ const char * g_test_get_path (void);
|
||||
/* tell about failure */
|
||||
GLIB_AVAILABLE_IN_2_30
|
||||
void g_test_fail (void);
|
||||
GLIB_AVAILABLE_IN_2_70
|
||||
void g_test_fail_printf (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
|
||||
|
@ -49,6 +49,12 @@ test_fail (void)
|
||||
g_test_fail ();
|
||||
}
|
||||
|
||||
static void
|
||||
test_fail_printf (void)
|
||||
{
|
||||
g_test_fail_printf ("this test intentionally left failing");
|
||||
}
|
||||
|
||||
static void
|
||||
test_incomplete (void)
|
||||
{
|
||||
@ -123,6 +129,10 @@ main (int argc,
|
||||
{
|
||||
g_test_add_func ("/fail", test_fail);
|
||||
}
|
||||
else if (g_strcmp0 (argv1, "fail-printf") == 0)
|
||||
{
|
||||
g_test_add_func ("/fail-printf", test_fail_printf);
|
||||
}
|
||||
else if (g_strcmp0 (argv1, "all-non-failures") == 0)
|
||||
{
|
||||
g_test_add_func ("/pass", test_pass);
|
||||
|
@ -1177,6 +1177,26 @@ test_tap (void)
|
||||
g_clear_error (&error);
|
||||
g_ptr_array_unref (argv);
|
||||
|
||||
g_test_message ("fail with message");
|
||||
argv = g_ptr_array_new ();
|
||||
g_ptr_array_add (argv, (char *) testing_helper);
|
||||
g_ptr_array_add (argv, "fail-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_error (error, G_SPAWN_EXIT_ERROR, 1);
|
||||
g_assert_nonnull (strstr (output, "\nnot ok 1 /fail-printf - this test intentionally left failing\n"));
|
||||
g_free (output);
|
||||
g_clear_error (&error);
|
||||
g_ptr_array_unref (argv);
|
||||
|
||||
g_test_message ("all");
|
||||
argv = g_ptr_array_new ();
|
||||
g_ptr_array_add (argv, (char *) testing_helper);
|
||||
|
Loading…
Reference in New Issue
Block a user