[glib: 1/2] gtestutils: Add g_test_get_path() API




commit 657d18fdbba4de3778d5a85fbe80852bcd60e719
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Mon Jan 25 14:43:00 2021 +0100

    gtestutils: Add g_test_get_path() API
    
    I found myself wanting to know the test that is currently being run,
    where e.g. __func__ would be inconvenient to use, because e.g. the place
    the string was needed was not in the test case function. Using __func__
    also relies on the test function itself containing the whole path, while
    loosing the "/" information that is part of the test path.

 docs/reference/glib/glib-sections.txt |  1 +
 glib/gtestutils.c                     | 20 ++++++++++++++++++++
 glib/gtestutils.h                     |  4 ++++
 glib/tests/testing.c                  | 15 +++++++++++++++
 4 files changed, 40 insertions(+)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index d20f419af..35ba9af4d 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -3508,6 +3508,7 @@ GTestDataFunc
 g_test_add_data_func
 g_test_add_data_func_full
 g_test_add
+g_test_get_path
 
 GTestFileType
 g_test_build_filename
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index be6b68e56..473e1b677 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -4230,3 +4230,23 @@ g_test_get_filename (GTestFileType  file_type,
 
   return result;
 }
+
+/**
+ * g_test_get_path:
+ *
+ * Gets the test path for the test currently being run.
+ *
+ * In essence, it will be the same string passed as the first argument to
+ * e.g. g_test_add() when the test was added.
+ *
+ * This function returns a valid string only within a test function.
+ *
+ * Returns: the test path for the test currently being run
+ *
+ * Since: 2.68
+ **/
+const char *
+g_test_get_path (void)
+{
+  return test_run_name;
+}
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 780b163b9..041143936 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -338,6 +338,10 @@ void    g_test_add_data_func_full       (const char     *testpath,
                                          GTestDataFunc   test_func,
                                          GDestroyNotify  data_free_func);
 
+/* tell about currently run test */
+GLIB_AVAILABLE_IN_2_68
+const char * g_test_get_path            (void);
+
 /* tell about failure */
 GLIB_AVAILABLE_IN_2_30
 void    g_test_fail                     (void);
diff --git a/glib/tests/testing.c b/glib/tests/testing.c
index 29551b9d2..7faa7b5ec 100644
--- a/glib/tests/testing.c
+++ b/glib/tests/testing.c
@@ -843,6 +843,18 @@ test_subprocess_timed_out (void)
   g_assert_true (g_test_trap_reached_timeout ());
 }
 
+static void
+test_path_first (void)
+{
+  g_assert_cmpstr (g_test_get_path (), ==, "/misc/path/first");
+}
+
+static void
+test_path_second (void)
+{
+  g_assert_cmpstr (g_test_get_path (), ==, "/misc/path/second");
+}
+
 static const char *argv0;
 
 static void
@@ -1595,6 +1607,9 @@ main (int   argc,
   g_test_add_func ("/misc/incomplete", test_incomplete);
   g_test_add_func ("/misc/timeout", test_subprocess_timed_out);
 
+  g_test_add_func ("/misc/path/first", test_path_first);
+  g_test_add_func ("/misc/path/second", test_path_second);
+
   g_test_add_func ("/tap", test_tap);
   g_test_add_func ("/tap/summary", test_tap_summary);
 


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