[glib: 1/4] gtestutils: Add g_assert_cmpvariant()



commit ee364db967014656a83f54a1882c552fe36c0fc3
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Dec 12 11:24:27 2018 +0000

    gtestutils: Add g_assert_cmpvariant()
    
    This is along the same lines as g_assert_cmpstr(), but for variants.
    
    Based on a patch by Guillaume Desmottes.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    https://gitlab.gnome.org/GNOME/glib/issues/1191

 docs/reference/glib/glib-sections.txt |  1 +
 glib/gtestutils.c                     | 20 ++++++++++++-
 glib/gtestutils.h                     | 17 +++++++++++
 glib/tests/testing.c                  | 53 +++++++++++++++++++++++++++++++++++
 4 files changed, 90 insertions(+), 1 deletion(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 1369012ce..6f8f906ac 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -3176,6 +3176,7 @@ g_assert_cmphex
 g_assert_cmpfloat
 g_assert_cmpfloat_with_epsilon
 g_assert_cmpmem
+g_assert_cmpvariant
 g_assert_no_error
 g_assert_error
 g_assert_true
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index 874d5dc06..fedee7d29 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -90,7 +90,8 @@
  * In addition to the traditional g_assert_true(), the test framework provides
  * an extended set of assertions for comparisons: g_assert_cmpfloat(),
  * g_assert_cmpfloat_with_epsilon(), g_assert_cmpint(), g_assert_cmpuint(),
- * g_assert_cmphex(), g_assert_cmpstr(), and g_assert_cmpmem(). The
+ * g_assert_cmphex(), g_assert_cmpstr(), g_assert_cmpmem() and
+ * g_assert_cmpvariant(). The
  * advantage of these variants over plain g_assert_true() is that the assertion
  * messages can be more elaborate, and include the values of the compared
  * entities.
@@ -701,6 +702,23 @@
  * Since: 2.46
  */
 
+/**
+ * g_assert_cmpvariant:
+ * @v1: pointer to a #GVariant
+ * @v2: pointer to another #GVariant
+ *
+ * Debugging macro to compare two #GVariants. If the comparison fails,
+ * an error message is logged and the application is either terminated
+ * or the testcase marked as failed. The variants are compared using
+ * g_variant_equal().
+ *
+ * The effect of `g_assert_cmpvariant (v1, v2)` is the same as
+ * `g_assert_true (g_variant_equal (v1, v2))`. The advantage of this macro is
+ * that it can produce a message that includes the actual values of @v1 and @v2.
+ *
+ * Since: 2.60
+ */
+
 /**
  * g_assert_no_error:
  * @err: a #GError, possibly %NULL
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 550e2414e..4e1293bc8 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -87,6 +87,23 @@ typedef void (*GTestFixtureFunc) (gpointer      fixture,
                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
                                                                     "assertion failed (" #m1 " == " #m2 
")"); \
                                         } G_STMT_END
+#define g_assert_cmpvariant(v1, v2) \
+  G_STMT_START \
+  { \
+    GVariant *__v1 = (v1), *__v2 = (v2); \
+    if (!g_variant_equal (__v1, __v2)) \
+      { \
+        gchar *__s1, *__s2, *__msg; \
+        __s1 = g_variant_print (__v1, TRUE); \
+        __s2 = g_variant_print (__v2, TRUE); \
+        __msg = g_strdup_printf ("assertion failed (" #v1 " == " #v2 "): %s does not equal %s", __s1, __s2); 
\
+        g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
+        g_free (__s1); \
+        g_free (__s2); \
+        g_free (__msg); \
+      } \
+  } \
+  G_STMT_END
 #define g_assert_no_error(err)          G_STMT_START { \
                                              if (err) \
                                                g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
diff --git a/glib/tests/testing.c b/glib/tests/testing.c
index 716516e2c..03b9ca2c4 100644
--- a/glib/tests/testing.c
+++ b/glib/tests/testing.c
@@ -34,6 +34,38 @@
 #include <string.h>
 
 /* test assertion variants */
+static void
+test_assertions_bad_cmpvariant_types (void)
+{
+  GVariant *v1, *v2;
+
+  v1 = g_variant_new_boolean (TRUE);
+  v2 = g_variant_new_string ("hello");
+
+  g_assert_cmpvariant (v1, v2);
+
+  g_variant_unref (v2);
+  g_variant_unref (v1);
+
+  exit (0);
+}
+
+static void
+test_assertions_bad_cmpvariant_values (void)
+{
+  GVariant *v1, *v2;
+
+  v1 = g_variant_new_string ("goodbye");
+  v2 = g_variant_new_string ("hello");
+
+  g_assert_cmpvariant (v1, v2);
+
+  g_variant_unref (v2);
+  g_variant_unref (v1);
+
+  exit (0);
+}
+
 static void
 test_assertions_bad_cmpstr (void)
 {
@@ -72,7 +104,9 @@ test_assertions_bad_cmpfloat_epsilon (void)
 static void
 test_assertions (void)
 {
+  GVariant *v1, *v2;
   gchar *fuu;
+
   g_assert_cmpint (1, >, 0);
   g_assert_cmphex (2, ==, 2);
   g_assert_cmpfloat (3.3, !=, 7);
@@ -94,6 +128,23 @@ test_assertions (void)
   g_assert_cmpstr ("fzz", ==, "fzz");
   g_assert_cmpmem ("foo", 3, "foot", 3);
 
+  v1 = g_variant_new_parsed ("['hello', 'there']");
+  v2 = g_variant_new_parsed ("['hello', 'there']");
+
+  g_assert_cmpvariant (v1, v1);
+  g_assert_cmpvariant (v1, v2);
+
+  g_variant_unref (v2);
+  g_variant_unref (v1);
+
+  g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpvariant_types", 0, 0);
+  g_test_trap_assert_failed ();
+  g_test_trap_assert_stderr ("*assertion failed*");
+
+  g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpvariant_values", 0, 0);
+  g_test_trap_assert_failed ();
+  g_test_trap_assert_stderr ("*assertion failed*");
+
   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstr", 0, 0);
   g_test_trap_assert_failed ();
   g_test_trap_assert_stderr ("*assertion failed*");
@@ -1026,6 +1077,8 @@ main (int   argc,
   g_test_add_func ("/random-generator/rand-2", test_rand2);
   g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
   g_test_add_func ("/misc/assertions", test_assertions);
+  g_test_add_func ("/misc/assertions/subprocess/bad_cmpvariant_types", test_assertions_bad_cmpvariant_types);
+  g_test_add_func ("/misc/assertions/subprocess/bad_cmpvariant_values", 
test_assertions_bad_cmpvariant_values);
   g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr);
   g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint);
   g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_len", test_assertions_bad_cmpmem_len);


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