[glib/wip/test-fail-convenience: 8/9] gtestutils: Allow skipping tests with a printf-style message




commit a33d8ffd5095861eddce3ccebe0ed8ee0bff844e
Author: Simon McVittie <smcv collabora com>
Date:   Thu Aug 5 11:51:18 2021 +0100

    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>

 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 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
@@ -2459,6 +2459,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
@@ -2482,6 +2505,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 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
@@ -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)
 {
@@ -53,6 +61,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)
 {
@@ -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);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]