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
This commit is contained in:
Philip Withnall 2018-12-12 11:24:27 +00:00
parent af39a37312
commit ee364db967
4 changed files with 90 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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, \

View File

@ -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);