mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-23 09:28:54 +02:00
gtestutils: Define a custom g_print handler for TAP
When using TAP output in gtest, all the printed strings should be commented not to break the spec, so enforce this at GTest level, by ensuring that all the lines written via g_print() will be in the commented form and will respect the subtest indentation. As per this we can remove some custom code in g_test_log() as now there are very few cases in which we need to use the default print handler which is now private when testing.
This commit is contained in:
@@ -374,18 +374,20 @@ test_print_handler (void)
|
||||
g_print ("bu ba");
|
||||
g_assert_cmpint (my_print_count, ==, 1);
|
||||
|
||||
g_set_print_handler (NULL);
|
||||
|
||||
if (g_test_subprocess ())
|
||||
{
|
||||
g_set_print_handler (NULL);
|
||||
old_print_handler ("default handler\n");
|
||||
g_print ("bu ba\n");
|
||||
return;
|
||||
}
|
||||
|
||||
g_set_print_handler (old_print_handler);
|
||||
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
|
||||
g_test_trap_assert_stdout ("*default handler" LINE_END "*");
|
||||
g_test_trap_assert_stdout ("*bu ba" LINE_END "*");
|
||||
g_test_trap_assert_stdout_unmatched ("*# default handler" LINE_END "*");
|
||||
g_test_trap_assert_stdout_unmatched ("*# bu ba" LINE_END "*");
|
||||
g_test_trap_has_passed ();
|
||||
}
|
||||
|
||||
@@ -401,15 +403,15 @@ test_printerr_handler (void)
|
||||
g_printerr ("bu ba");
|
||||
g_assert_cmpint (my_print_count, ==, 1);
|
||||
|
||||
g_set_printerr_handler (NULL);
|
||||
|
||||
if (g_test_subprocess ())
|
||||
{
|
||||
g_set_printerr_handler (NULL);
|
||||
old_printerr_handler ("default handler\n");
|
||||
g_printerr ("bu ba\n");
|
||||
return;
|
||||
}
|
||||
|
||||
g_set_printerr_handler (old_printerr_handler);
|
||||
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
|
||||
g_test_trap_assert_stderr ("*default handler" LINE_END "*");
|
||||
g_test_trap_assert_stderr ("*bu ba" LINE_END "*");
|
||||
|
@@ -93,6 +93,16 @@ test_message (void)
|
||||
g_test_message ("\nTests that multi\nline\nmessage\nworks with leading and trailing too\n");
|
||||
}
|
||||
|
||||
static void
|
||||
test_print (void)
|
||||
{
|
||||
g_print ("Tests that single line message works\n");
|
||||
g_print ("test that multiple\nlines ");
|
||||
g_print ("can be ");
|
||||
g_print ("written ");
|
||||
g_print ("separately\n");
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
@@ -212,6 +222,10 @@ main (int argc,
|
||||
{
|
||||
g_test_add_func ("/message", test_message);
|
||||
}
|
||||
else if (g_strcmp0 (argv1, "print") == 0)
|
||||
{
|
||||
g_test_add_func ("/print", test_print);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_assert_not_reached ();
|
||||
|
@@ -2428,6 +2428,107 @@ test_tap_subtest_message (void)
|
||||
g_ptr_array_unref (argv);
|
||||
}
|
||||
|
||||
static void
|
||||
test_tap_print (void)
|
||||
{
|
||||
const char *testing_helper;
|
||||
GPtrArray *argv;
|
||||
GError *error = NULL;
|
||||
int status;
|
||||
gchar *output;
|
||||
char **output_lines;
|
||||
char **envp;
|
||||
|
||||
g_test_summary ("Test the output of g_print() 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, "print");
|
||||
g_ptr_array_add (argv, "--tap");
|
||||
g_ptr_array_add (argv, NULL);
|
||||
|
||||
/* Remove the G_TEST_ROOT_PROCESS env so it will be considered a standalone test */
|
||||
envp = g_get_environ ();
|
||||
g_assert_nonnull (g_environ_getenv (envp, "G_TEST_ROOT_PROCESS"));
|
||||
envp = g_environ_unsetenv (g_steal_pointer (&envp), "G_TEST_ROOT_PROCESS");
|
||||
|
||||
g_spawn_sync (NULL, (char **) argv->pdata, envp,
|
||||
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), >=, 3);
|
||||
|
||||
guint i = 0;
|
||||
g_assert_cmpstr (output_lines[i++], ==, "# Tests that single line message works");
|
||||
g_assert_cmpstr (output_lines[i++], ==, "# test that multiple");
|
||||
g_assert_cmpstr (output_lines[i++], ==, "# lines can be written separately");
|
||||
|
||||
g_free (output);
|
||||
g_strfreev (envp);
|
||||
g_strfreev (output_lines);
|
||||
g_ptr_array_unref (argv);
|
||||
}
|
||||
|
||||
static void
|
||||
test_tap_subtest_print (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_print() from the TAP output of a sub-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, "print");
|
||||
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 = "\n" TAP_SUBTEST_PREFIX "1..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), >=, 3);
|
||||
|
||||
guint i = 0;
|
||||
g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# Tests that single line message works");
|
||||
g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# test that multiple");
|
||||
g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# lines can be written separately");
|
||||
|
||||
g_free (output);
|
||||
g_strfreev (output_lines);
|
||||
g_ptr_array_unref (argv);
|
||||
}
|
||||
|
||||
static void
|
||||
test_tap_error (void)
|
||||
{
|
||||
@@ -2753,6 +2854,8 @@ main (int argc,
|
||||
g_test_add_func ("/tap/subtest/summary", test_tap_subtest_summary);
|
||||
g_test_add_func ("/tap/message", test_tap_message);
|
||||
g_test_add_func ("/tap/subtest/message", test_tap_subtest_message);
|
||||
g_test_add_func ("/tap/print", test_tap_print);
|
||||
g_test_add_func ("/tap/subtest/print", test_tap_subtest_print);
|
||||
g_test_add_func ("/tap/error", test_tap_error);
|
||||
g_test_add_func ("/tap/subtest/error", test_tap_subtest_error);
|
||||
g_test_add_func ("/tap/error-and-pass", test_tap_error_and_pass);
|
||||
|
Reference in New Issue
Block a user