Commit 903ce7dc authored by Philip Withnall's avatar Philip Withnall

Merge branch '1191-g_assert_cmpvariant' into 'master'

gtestutils: Add g_assert_cmpvariant()

Closes #1191

See merge request !529
parents 1fa24a68 d289ef44
Pipeline #46426 failed with stages
in 13 minutes and 12 seconds
......@@ -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
......
......@@ -515,7 +515,7 @@ test_parse_detailed (void)
expected = g_variant_parse (NULL, testcases[i].expected_target, NULL, NULL, NULL);
g_assert (expected);
g_assert (g_variant_equal (expected, target));
g_assert_cmpvariant (expected, target);
g_variant_unref (expected);
g_variant_unref (target);
}
......
......@@ -128,7 +128,7 @@ message_copy (void)
copy_val = g_dbus_message_get_header (m, m_headers[n]);
g_assert (m_val != NULL);
g_assert (copy_val != NULL);
g_assert (g_variant_equal (m_val, copy_val));
g_assert_cmpvariant (m_val, copy_val);
}
g_assert_cmpint (n, >, 0); /* make sure we actually compared headers etc. */
g_assert_cmpint (copy_headers[n], ==, 0);
......
......@@ -526,7 +526,6 @@ check_serialization (GVariant *value,
GError *error;
DBusError dbus_error;
gchar *s;
gchar *s1;
guint n;
message = g_dbus_message_new ();
......@@ -618,17 +617,7 @@ check_serialization (GVariant *value,
else
{
g_assert (g_dbus_message_get_body (recovered_message) != NULL);
if (!g_variant_equal (g_dbus_message_get_body (recovered_message), value))
{
s = g_variant_print (g_dbus_message_get_body (recovered_message), TRUE);
s1 = g_variant_print (value, TRUE);
g_printerr ("Recovered value:\n%s\ndoes not match given value\n%s\n",
s,
s1);
g_free (s);
g_free (s1);
g_assert_not_reached ();
}
g_assert_cmpvariant (g_dbus_message_get_body (recovered_message), value);
}
g_object_unref (recovered_message);
g_free (blob);
......
......@@ -933,7 +933,7 @@ check_bar_proxy (FooiGenBar *proxy,
g_assert_cmpuint (g_strv_length ((gchar **) ret_array_of_objpaths), ==,
g_strv_length ((gchar **) array_of_objpaths));
g_assert_nonnull (ret_array_of_signatures);
g_assert_true (g_variant_equal (ret_array_of_signatures, array_of_signatures));
g_assert_cmpvariant (ret_array_of_signatures, array_of_signatures);
g_assert_nonnull (ret_array_of_bytestrings);
g_assert_cmpuint (g_strv_length ((gchar **) ret_array_of_bytestrings), ==,
g_strv_length ((gchar **) array_of_bytestrings));
......
......@@ -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.
......@@ -575,7 +576,7 @@
* g_assert_cmpstr:
* @s1: a string (may be %NULL)
* @cmp: The comparison operator to use.
* One of ==, !=, <, >, <=, >=.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @s2: another string (may be %NULL)
*
* Debugging macro to compare two strings. If the comparison fails,
......@@ -599,7 +600,7 @@
* g_assert_cmpint:
* @n1: an integer
* @cmp: The comparison operator to use.
* One of ==, !=, <, >, <=, >=.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @n2: another integer
*
* Debugging macro to compare two integers.
......@@ -616,7 +617,7 @@
* g_assert_cmpuint:
* @n1: an unsigned integer
* @cmp: The comparison operator to use.
* One of ==, !=, <, >, <=, >=.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @n2: another unsigned integer
*
* Debugging macro to compare two unsigned integers.
......@@ -633,7 +634,7 @@
* g_assert_cmphex:
* @n1: an unsigned integer
* @cmp: The comparison operator to use.
* One of ==, !=, <, >, <=, >=.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @n2: another unsigned integer
*
* Debugging macro to compare to unsigned integers.
......@@ -648,7 +649,7 @@
* g_assert_cmpfloat:
* @n1: an floating point number
* @cmp: The comparison operator to use.
* One of ==, !=, <, >, <=, >=.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @n2: another floating point number
*
* Debugging macro to compare two floating point numbers.
......@@ -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
......
......@@ -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, \
......
......@@ -4406,67 +4406,67 @@ test_equal (void)
a = untrusted (g_variant_new_byte (5));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_int16 (G_MININT16));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_uint16 (0));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_int32 (G_MININT32));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_uint32 (0));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_int64 (G_MININT64));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_uint64 (0));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_double (G_MINDOUBLE));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_string ("abc"));
g_assert_true (g_variant_equal (a, a));
g_assert_cmpvariant (a, a);
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_object_path ("/abc"));
g_assert_true (g_variant_equal (a, a));
g_assert_cmpvariant (a, a);
b = g_variant_get_normal_form (a);
a = untrusted (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_signature ("g"));
g_assert_true (g_variant_equal (a, a));
g_assert_cmpvariant (a, a);
b = g_variant_get_normal_form (a);
a = untrusted (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
a = untrusted (g_variant_new_boolean (FALSE));
b = g_variant_get_normal_form (a);
g_assert_true (g_variant_equal (a, b));
g_assert_cmpvariant (a, b);
g_variant_unref (a);
g_variant_unref (b);
}
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment