[glib/wip/nielsdg/g_assert_cmpstrv: 1/2] gtestutils: Add g_assert_cmpstrv()




commit 741ce1962dfe4e3f879dea9df56494c60584a6fd
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sun Nov 1 12:03:04 2020 +0100

    gtestutils: Add g_assert_cmpstrv()

 docs/reference/glib/glib-sections.txt |  1 +
 glib/gtestutils.c                     | 48 ++++++++++++++++++++++++++++++
 glib/gtestutils.h                     | 55 +++++++++++++++++++++++++++++++++++
 glib/tests/testing.c                  |  4 +++
 4 files changed, 108 insertions(+)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 02bb2ba50..68574040d 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -3547,6 +3547,7 @@ g_assert
 g_assert_not_reached
 
 g_assert_cmpstr
+g_assert_cmpstrv
 g_assert_cmpint
 g_assert_cmpuint
 g_assert_cmphex
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index 3b03e9831..01c5138aa 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -576,6 +576,29 @@
  * Since: 2.16
  */
 
+/**
+ * g_assert_cmpstrv:
+ * @strv1: (nullable): a string array (may be %NULL)
+ * @strv2: (nullable): another string array (may be %NULL)
+ *
+ * Debugging macro to check if two string arrays (i.e. 2 #GStrv) are equal.
+ * If they are not equal, an error message is logged and the application is
+ * either terminated or the testcase marked as failed.
+ * The strings are compared using g_strcmp0().
+ *
+ * The effect of `g_assert_cmpstrv (strv1, strv2)` is the same as
+ * `g_assert_true (g_strv_equal (strv1, strv2))`.
+ * The advantage of this macro is that it can produce a message that
+ * includes how @strv1 and @strv2 are different.
+ *
+ * |[<!-- language="C" -->
+ *   const char *expected[] = { "one", "two", "three", NULL };
+ *   g_assert_cmpstrv (mystrv, expected);
+ * ]|
+ *
+ * Since: 2.66
+ */
+
 /**
  * g_assert_cmpint:
  * @n1: an integer
@@ -3018,6 +3041,31 @@ g_assertion_message_cmpstr (const char     *domain,
   g_free (s);
 }
 
+void
+g_assertion_message_cmpstrv (const char   *domain,
+                             const char   *file,
+                             int           line,
+                             const char   *func,
+                             const char   *expr,
+                             char        **arg1,
+                             char        **arg2,
+                             guint         index)
+{
+  const char *s1 = arg1[index], *s2 = arg2[index];
+  char *a1, *a2, *s, *t1 = NULL, *t2 = NULL;
+
+  a1 = arg1 ? g_strconcat ("\"", t1 = g_strescape (s1, NULL), "\"", NULL) : g_strdup ("NULL");
+  a2 = arg2 ? g_strconcat ("\"", t2 = g_strescape (s2, NULL), "\"", NULL) : g_strdup ("NULL");
+  g_free (t1);
+  g_free (t2);
+  s = g_strdup_printf ("assertion failed (%s): first differing element at index %u: %s does not equal %s",
+                       expr, index, a1, a2);
+  g_free (a1);
+  g_free (a2);
+  g_assertion_message (domain, file, line, func, s);
+  g_free (s);
+}
+
 void
 g_assertion_message_error (const char     *domain,
                           const char     *file,
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 10b65c164..658e45492 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -111,6 +111,51 @@ typedef void (*GTestFixtureFunc) (gpointer      fixture,
       } \
   } \
   G_STMT_END
+#define g_assert_cmpstrv(strv1, strv2) \
+  G_STMT_START \
+  { \
+    GStrv __strv1 = (GStrv) (strv1); \
+    GStrv __strv2 = (GStrv) (strv2); \
+    if ((!__strv1 || !__strv2) && __strv1 != __strv2) \
+      { \
+        if (__strv1) \
+          { \
+            g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+                                 "assertion failed (" #strv1 " == " #strv2 "): " #strv2 " is NULL, but " 
#strv1 " is not"); \
+          } \
+        else \
+          { \
+            g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+                                 "assertion failed (" #strv1 " == " #strv2 "): " #strv1 " is NULL, but " 
#strv2 " is not"); \
+          } \
+      } \
+    else \
+      { \
+        guint __l1, __l2; \
+        __l1 = g_strv_length (__strv1); \
+        __l2 = g_strv_length (__strv2); \
+        if (__l1 != __l2) \
+          { \
+            char *__msg; \
+            __msg = g_strdup_printf ("assertion failed (" #strv1 " == " #strv2 "): length %u does not equal 
length %u", __l1, __l2); \
+            g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
+            g_free (__msg); \
+          } \
+        else \
+          { \
+            for (guint __i = 0; __i < __l1; __i++) \
+              { \
+                if (g_strcmp0 (__strv1[__i], __strv2[__i]) != 0) \
+                  { \
+                    g_assertion_message_cmpstrv (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+                                                 #strv1 " == " #strv2, \
+                                                 __strv1, __strv2, __i); \
+                  } \
+              } \
+          } \
+      } \
+  } \
+  G_STMT_END
 #define g_assert_no_errno(expr)         G_STMT_START { \
                                              int __ret, __errsv; \
                                              errno = 0; \
@@ -483,6 +528,16 @@ void    g_assertion_message_cmpstr      (const char     *domain,
                                          const char     *arg1,
                                          const char     *cmp,
                                          const char     *arg2) G_ANALYZER_NORETURN;
+
+GLIB_AVAILABLE_IN_2_66
+void    g_assertion_message_cmpstrv     (const char     *domain,
+                                         const char     *file,
+                                         int             line,
+                                         const char     *func,
+                                         const char     *expr,
+                                         char          **arg1,
+                                         char          **arg2,
+                                         guint           index) G_ANALYZER_NORETURN;
 GLIB_AVAILABLE_IN_ALL
 void    g_assertion_message_cmpnum      (const char     *domain,
                                          const char     *file,
diff --git a/glib/tests/testing.c b/glib/tests/testing.c
index ed4c8d6f7..763808d14 100644
--- a/glib/tests/testing.c
+++ b/glib/tests/testing.c
@@ -132,6 +132,8 @@ test_assertions_bad_no_errno (void)
 static void
 test_assertions (void)
 {
+  const char *strv1[] = { "one", "two", "three", NULL };
+  const char *strv2[] = { "one", "two", "four", NULL };
   GVariant *v1, *v2;
   gchar *fuu;
 
@@ -160,6 +162,8 @@ test_assertions (void)
   g_assert_cmpmem ("foo", 0, NULL, 0);
   g_assert_no_errno (return_no_errno ());
 
+  g_assert_cmpstrv (strv1, strv2);
+
   v1 = g_variant_new_parsed ("['hello', 'there']");
   v2 = g_variant_new_parsed ("['hello', 'there']");
 


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