[libgdata/libgdata-0-14: 6/18] gd: Include URI in comparisons between GDataGDPhoneNumbers



commit 50e6da8f2725a3d74ed49af342feb28c9305cabe
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sat Aug 31 22:42:33 2013 -0600

    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.

 gdata/gd/gdata-gd-phone-number.c |   18 ++++++++++++++++-
 gdata/tests/general.c            |   39 ++++++++++++++++++++++++++++++++++++++
 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 d160b97..5a8d3e4 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 7c23fc9..51dd12d 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);


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