summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2013-08-31 22:42:33 -0600
committerPhilip Withnall <philip@tecnocode.co.uk>2013-12-18 23:41:31 +0000
commit50e6da8f2725a3d74ed49af342feb28c9305cabe (patch)
tree2f36f8219841e37df55bb282431aada3db7c3c38
parent3c7551b2ccce59412180ac969b8ce027c7c05783 (diff)
downloadlibgdata-50e6da8f2725a3d74ed49af342feb28c9305cabe.tar.gz
gd: Include URI in comparisons between GDataGDPhoneNumbers
This means two numbers will compare equal if their URIs are non-NULL and equal. This is a slight behaviour change, but I don’t consider it to break API, since comparison behaviour isn’t documented anywhere and nobody’s ever asked. This includes a test case.
-rw-r--r--gdata/gd/gdata-gd-phone-number.c18
-rw-r--r--gdata/tests/general.c39
2 files changed, 56 insertions, 1 deletions
diff --git a/gdata/gd/gdata-gd-phone-number.c b/gdata/gd/gdata-gd-phone-number.c
index d160b978..5a8d3e4b 100644
--- a/gdata/gd/gdata-gd-phone-number.c
+++ b/gdata/gd/gdata-gd-phone-number.c
@@ -171,7 +171,23 @@ gdata_gd_phone_number_class_init (GDataGDPhoneNumberClass *klass)
static gint
compare_with (GDataComparable *self, GDataComparable *other)
{
- return g_strcmp0 (((GDataGDPhoneNumber*) self)->priv->number, ((GDataGDPhoneNumber*) other)->priv->number);
+ GDataGDPhoneNumber *a = (GDataGDPhoneNumber*) self, *b = (GDataGDPhoneNumber*) other;
+ gint number_comparison, uri_comparison;
+
+ number_comparison = g_strcmp0 (a->priv->number, b->priv->number);
+ uri_comparison = g_strcmp0 (a->priv->uri, b->priv->uri);
+
+ /* NULL URIs should not compare equal. */
+ if (a->priv->uri == NULL && b->priv->uri == NULL) {
+ uri_comparison = 1;
+ }
+
+ /* If the numbers or the URIs are equal, the objects are equal. */
+ if (number_comparison == 0 || uri_comparison == 0) {
+ return 0;
+ }
+
+ return number_comparison;
}
static void
diff --git a/gdata/tests/general.c b/gdata/tests/general.c
index 7c23fc97..51dd12de 100644
--- a/gdata/tests/general.c
+++ b/gdata/tests/general.c
@@ -2420,6 +2420,7 @@ test_gd_phone_number (void)
/* …and a different one */
gdata_gd_phone_number_set_number (phone2, "+1 206 555 1212 666");
+ gdata_gd_phone_number_set_uri (phone2, NULL);
g_assert_cmpint (gdata_comparable_compare (GDATA_COMPARABLE (phone), GDATA_COMPARABLE (phone2)), !=, 0);
g_object_unref (phone2);
@@ -2475,6 +2476,43 @@ test_gd_phone_number_escaping (void)
}
static void
+test_gd_phone_number_comparison (void)
+{
+ GDataGDPhoneNumber *phone1, *phone2;
+
+ /* Phone numbers are equal if the number or the URI matches (NULL URIs cannot match). */
+ phone1 = gdata_gd_phone_number_new ("123", NULL, NULL, "phone://123", TRUE);
+ phone2 = gdata_gd_phone_number_new ("123", NULL, "label", "phone://123", FALSE);
+ g_assert_cmpint (gdata_comparable_compare (GDATA_COMPARABLE (phone1), GDATA_COMPARABLE (phone2)), ==, 0);
+
+ /* Same numbers, different URIs. */
+ gdata_gd_phone_number_set_uri (phone1, "phone://+44123");
+ g_assert_cmpint (gdata_comparable_compare (GDATA_COMPARABLE (phone1), GDATA_COMPARABLE (phone2)), ==, 0);
+
+ /* Different numbers, same URIs. */
+ gdata_gd_phone_number_set_uri (phone1, "phone://123");
+ gdata_gd_phone_number_set_number (phone1, "+44123");
+ g_assert_cmpint (gdata_comparable_compare (GDATA_COMPARABLE (phone1), GDATA_COMPARABLE (phone2)), ==, 0);
+
+ /* Different numbers and URIs. */
+ gdata_gd_phone_number_set_number (phone1, "456");
+ gdata_gd_phone_number_set_uri (phone1, "phone://456");
+ g_assert_cmpint (gdata_comparable_compare (GDATA_COMPARABLE (phone1), GDATA_COMPARABLE (phone2)), !=, 0);
+
+ /* Different numbers, NULL URIs. */
+ gdata_gd_phone_number_set_uri (phone1, NULL);
+ gdata_gd_phone_number_set_uri (phone2, NULL);
+ g_assert_cmpint (gdata_comparable_compare (GDATA_COMPARABLE (phone1), GDATA_COMPARABLE (phone2)), !=, 0);
+
+ /* Same numbers, NULL URIs. */
+ gdata_gd_phone_number_set_number (phone1, "123");
+ g_assert_cmpint (gdata_comparable_compare (GDATA_COMPARABLE (phone1), GDATA_COMPARABLE (phone2)), ==, 0);
+
+ g_object_unref (phone2);
+ g_object_unref (phone1);
+}
+
+static void
test_gd_postal_address (void)
{
GDataGDPostalAddress *postal, *postal2;
@@ -4047,6 +4085,7 @@ main (int argc, char *argv[])
g_test_add_func ("/gd/organization/escaping", test_gd_organization_escaping);
g_test_add_func ("/gd/phone_number", test_gd_phone_number);
g_test_add_func ("/gd/phone_number/escaping", test_gd_phone_number_escaping);
+ g_test_add_func ("/gd/phone_number/comparison", test_gd_phone_number_comparison);
g_test_add_func ("/gd/postal_address", test_gd_postal_address);
g_test_add_func ("/gd/postal_address/escaping", test_gd_postal_address_escaping);
g_test_add_func ("/gd/reminder", test_gd_reminder);