Add AbstractFieldDetails.value_type.
authorTravis Reitter <travis.reitter@collabora.co.uk>
Thu, 3 Nov 2011 21:05:45 +0000 (14:05 -0700)
committerTravis Reitter <travis.reitter@collabora.co.uk>
Thu, 10 Nov 2011 22:05:37 +0000 (14:05 -0800)
This allows generic handling of collections of derived objects of
mixed concrete classes.

Helps: bgo#663798 - Add AbstractFieldDetails.value_type

NEWS
folks/abstract-field-details.vala
tests/folks/abstract-field-details.vala

diff --git a/NEWS b/NEWS
index 1c51c3a..b14fe55 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,7 @@ Bugs fixed:
   store.
 * Bug 662314 — Gnome-shell restarts if I change my user status to
   disconnected/unavailable
+* Bug 663798 — Add AbstractFieldDetails.value_type
 
 API changes:
 * Add AbstractFieldDetails.id to identify instances of details
@@ -19,6 +20,7 @@ API changes:
 * Deprecate Role.uid in favor of AbstractFieldDetails.id
 * Add AbstractFieldDetails.values_equal() to compare values (but not parameters)
 * Implement FavouriteDetails on Edsf.Persona
+* Add AbstractFieldDetails.value_type
 
 Behavior changes:
 * PostalAddress.equal() now ignores PostalAddress.uid
index 3f5b657..5520706 100644 (file)
@@ -101,6 +101,20 @@ public abstract class Folks.AbstractFieldDetails<T> : Object
       set { this._value = value; }
     }
 
+  /**
+   * The {@link GLib.Type of the {@link AbstractFieldDetails.value}.
+   *
+   * This is particularly useful for treating collections of different types of
+   * {@link AbstractFieldDetails} in a uniform way without needing to name them
+   * explicitly.
+   *
+   * @since UNRELEASED
+   */
+  public Type value_type
+    {
+      get { return typeof (T); }
+    }
+
   private string _id;
   /**
    * A unique ID (if any) for this specific detail.
index 6d4ef72..63e6a6a 100644 (file)
@@ -32,6 +32,8 @@ public class EmailFieldDetailsTests : Folks.TestCase
           this.test_im_field_details_equality);
       this.add_test ("RoleFieldDetails equality",
           this.test_role_field_details_equality);
+      this.add_test ("Generic AFD handling",
+          this.test_generic_afd_handling);
     }
 
   public override void set_up ()
@@ -181,6 +183,48 @@ public class EmailFieldDetailsTests : Folks.TestCase
       assert (details_a_1.equal (details_a_2));
       assert (!details_a_1.equal (details_b_1));
     }
+
+  public void test_generic_afd_handling ()
+    {
+      AbstractFieldDetails afd;
+
+      afd = new EmailFieldDetails ("foo@example.org");
+      assert (afd.get_type ().is_a (typeof (AbstractFieldDetails)));
+      assert (afd.value_type == typeof (string));
+
+      afd = new ImFieldDetails ("bar@example.org");
+      assert (afd.get_type ().is_a (typeof (AbstractFieldDetails)));
+      assert (afd.value_type == typeof (string));
+
+      afd = new NoteFieldDetails ("Are you writing this down?");
+      assert (afd.get_type ().is_a (typeof (AbstractFieldDetails)));
+      assert (afd.value_type == typeof (string));
+
+      afd = new PhoneFieldDetails ("+19255551234");
+      assert (afd.get_type ().is_a (typeof (AbstractFieldDetails)));
+      assert (afd.value_type == typeof (string));
+
+      afd = new PostalAddressFieldDetails (
+          new PostalAddress (null, null, "123 Streetly Way", "Cityville",
+            "Cascadia", null, "USA", null, null));
+      assert (afd.get_type ().is_a (typeof (AbstractFieldDetails)));
+      assert (afd.value_type == typeof (PostalAddress));
+      assert (afd.value_type != typeof (string));
+
+      afd = new RoleFieldDetails (
+          new Role ("Captain", "Obvious Corp.", null));
+      assert (afd.get_type ().is_a (typeof (AbstractFieldDetails)));
+      assert (afd.value_type == typeof (Role));
+      assert (afd.value_type != typeof (string));
+
+      afd = new UrlFieldDetails ("http://other.side.of.nowhere");
+      assert (afd.get_type ().is_a (typeof (AbstractFieldDetails)));
+      assert (afd.value_type == typeof (string));
+
+      afd = new WebServiceFieldDetails ("bluebomber");
+      assert (afd.get_type ().is_a (typeof (AbstractFieldDetails)));
+      assert (afd.value_type == typeof (string));
+    }
 }
 
 public int main (string[] args)