[CalendarServer-changes] [11417] CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/ python

source_changes at macosforge.org source_changes at macosforge.org
Wed Jun 26 01:15:07 PDT 2013


Revision: 11417
          http://trac.calendarserver.org//changeset/11417
Author:   glyph at apple.com
Date:     2013-06-26 01:15:07 -0700 (Wed, 26 Jun 2013)
Log Message:
-----------
values, items, list iteration, tests.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/launchd.py
    CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/test/test_launchd.py

Modified: CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/launchd.py
===================================================================
--- CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/launchd.py	2013-06-26 08:14:59 UTC (rev 11416)
+++ CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/launchd.py	2013-06-26 08:15:07 UTC (rev 11417)
@@ -50,11 +50,15 @@
 
 launch_data_t launch_data_alloc(launch_data_type_t);
 launch_data_t launch_data_new_string(const char *);
+launch_data_t launch_data_new_integer(long long);
+
 launch_data_t launch_msg(const launch_data_t);
+
 launch_data_type_t launch_data_get_type(const launch_data_t);
+
 launch_data_t launch_data_dict_lookup(const launch_data_t, const char *);
 size_t launch_data_dict_get_count(const launch_data_t);
-
+long long launch_data_get_integer(const launch_data_t);
 void launch_data_dict_iterate(
     const launch_data_t,
     void (*)(const launch_data_t, const char *, void *),
@@ -64,6 +68,7 @@
 
 size_t launch_data_array_get_count(const launch_data_t);
 launch_data_t launch_data_array_get_index(const launch_data_t, size_t);
+bool launch_data_array_set_index(launch_data_t, const launch_data_t, size_t);
 
 void launch_data_free(launch_data_t);
 """)
@@ -88,7 +93,11 @@
 
 
     def __getitem__(self, index):
-        return lib.launch_data_array_get_index(self.launchdata, index)
+        if index >= len(self):
+            raise IndexError(index)
+        return _launchify(
+            lib.launch_data_array_get_index(self.launchdata, index)
+        )
 
 
 
@@ -109,6 +118,30 @@
         return keys
 
 
+    def values(self):
+        """
+        Return values in the dictionary.
+        """
+        values = []
+        @ffi.callback("void (*)(const launch_data_t, const char *, void *)")
+        def icb(v, k, n):
+            values.append(_launchify(v))
+        lib.launch_data_dict_iterate(self.launchdata, icb, ffi.NULL)
+        return values
+
+
+    def items(self):
+        """
+        Return items in the dictionary.
+        """
+        values = []
+        @ffi.callback("void (*)(const launch_data_t, const char *, void *)")
+        def icb(v, k, n):
+            values.append((ffi.string(k), _launchify(v)))
+        lib.launch_data_dict_iterate(self.launchdata, icb, ffi.NULL)
+        return values
+
+
     def __getitem__(self, key):
         launchvalue = lib.launch_data_dict_lookup(self.launchdata, key)
         try:

Modified: CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/test/test_launchd.py
===================================================================
--- CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/test/test_launchd.py	2013-06-26 08:14:59 UTC (rev 11416)
+++ CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/test/test_launchd.py	2013-06-26 08:15:07 UTC (rev 11417)
@@ -18,22 +18,18 @@
 Tests for L{twext.python.launchd}.
 """
 
-from twext.python.launchd import lib, ffi, LaunchDictionary
+from twext.python.launchd import lib, ffi, LaunchDictionary, LaunchArray
 
 from twisted.trial.unittest import TestCase
 
-alloc = lib.launch_data_alloc
-
-
-
-class WrapperTests(TestCase):
+class DictionaryTests(TestCase):
     """
-    Tests for all wrapper objects.
+    Tests for L{LaunchDictionary}
     """
 
     def setUp(self):
         """
-        Assemble some test data structures.
+        Assemble a test dictionary.
         """
         self.testDict = ffi.gc(
             lib.launch_data_alloc(lib.LAUNCH_DATA_DICTIONARY),
@@ -43,17 +39,111 @@
         val1 = lib.launch_data_new_string("alpha-value")
         key2 = ffi.new("char[]", "beta")
         val2 = lib.launch_data_new_string("beta-value")
+        key3 = ffi.new("char[]", "gamma")
+        val3 = lib.launch_data_new_integer(3)
         lib.launch_data_dict_insert(self.testDict, val1, key1)
         lib.launch_data_dict_insert(self.testDict, val2, key2)
-        self.assertEquals(lib.launch_data_dict_get_count(self.testDict), 2) 
+        lib.launch_data_dict_insert(self.testDict, val3, key3)
+        self.assertEquals(lib.launch_data_dict_get_count(self.testDict), 3)
 
 
+    def test_launchDictionaryLength(self):
+        """
+        C{len(LaunchDictionary())} returns the number of keys in the
+        dictionary.
+        """
+        self.assertEquals(len(LaunchDictionary(self.testDict)), 3)
+
+
     def test_launchDictionaryKeys(self):
         """
-        L{LaunchDictionary.keys} returns a key.
+        L{LaunchDictionary.keys} returns keys present in a C{launch_data_dict}.
         """
         dictionary = LaunchDictionary(self.testDict)
-        self.assertEquals(dictionary.keys(), [u"alpha", u"beta"])
+        self.assertEquals(set(dictionary.keys()),
+                          set([u"alpha", u"beta", u"gamma"]))
 
 
+    def test_launchDictionaryValues(self):
+        """
+        L{LaunchDictionary.values} returns keys present in a
+        C{launch_data_dict}.
+        """
+        dictionary = LaunchDictionary(self.testDict)
+        self.assertEquals(set(dictionary.values()),
+                          set([u"alpha-value", u"beta-value", 3]))
 
+
+    def test_launchDictionaryItems(self):
+        """
+        L{LaunchDictionary.items} returns all (key, value) tuples present in a
+        C{launch_data_dict}.
+        """
+        dictionary = LaunchDictionary(self.testDict)
+        self.assertEquals(set(dictionary.items()),
+                          set([(u"alpha", u"alpha-value"),
+                               (u"beta", u"beta-value"), (u"gamma", 3)]))
+
+
+class ArrayTests(TestCase):
+    """
+    Tests for L{LaunchArray}
+    """
+
+    def setUp(self):
+        """
+        Assemble a test array.
+        """
+        self.testArray = ffi.gc(
+            lib.launch_data_alloc(lib.LAUNCH_DATA_ARRAY),
+            lib.launch_data_free
+        )
+
+        lib.launch_data_array_set_index(
+            self.testArray, lib.launch_data_new_string("test-string-1"), 0
+        )
+        lib.launch_data_array_set_index(
+            self.testArray, lib.launch_data_new_string("another string."), 1
+        )
+        lib.launch_data_array_set_index(
+            self.testArray, lib.launch_data_new_integer(4321), 2
+        )
+
+
+    def test_length(self):
+        """
+        C{len(LaunchArray(...))} returns the number of elements in the array.
+        """
+        self.assertEquals(len(LaunchArray(self.testArray)), 3)
+
+
+    def test_indexing(self):
+        """
+        C{LaunchArray(...)[n]} returns the n'th element in the array.
+        """
+        array = LaunchArray(self.testArray)
+        self.assertEquals(array[0], u"test-string-1")
+        self.assertEquals(array[1], u"another string.")
+        self.assertEquals(array[2], 4321)
+
+
+    def test_indexTooBig(self):
+        """
+        C{LaunchArray(...)[n]}, where C{n} is greater than the length of the
+        array, raises an L{IndexError}.
+        """
+        array = LaunchArray(self.testArray)
+        self.assertRaises(IndexError, lambda: array[3])
+
+
+    def test_iterating(self):
+        """
+        Iterating over a C{LaunchArray} returns each item in sequence.
+        """
+        array = LaunchArray(self.testArray)
+        i = iter(array)
+        self.assertEquals(i.next(), u"test-string-1")
+        self.assertEquals(i.next(), u"another string.")
+        self.assertEquals(i.next(), 4321)
+        self.assertRaises(StopIteration, i.next)
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130626/79986b8e/attachment-0001.html>


More information about the calendarserver-changes mailing list