Commits

Ronald Oussoren  committed 6bd41f8

more test updates

  • Participants
  • Parent commits 7c9d629

Comments (0)

Files changed (5)

File pyobjc-core/Lib/objc/_convenience.py

 
 if sys.version_info[0] == 2:
     def nsset__cmp__(self, other):
-        try:
-            if self < other:
-                return -1
-            elif self == other:
-                return 0
-            else:
-                return 1
-        except TypeError:
-            return cmp(id(self), id(other))
+        raise TypeError("Cannot compare sets using cmp")
 
 def nsset__length_hint__(self):
     return len(self)
 
 if sys.version_info[0] == 2:
     CLASS_METHODS['NSSet'] += (
-        ('__cmp__', 'nsset__cmp__'),
+        ('__cmp__', nsset__cmp__),
     )
 
 CLASS_METHODS['NSMutableSet'] = (

File pyobjc-core/Lib/objc/_lazyimport.py

 
 from objc import lookUpClass, getClassList, nosuchclass_error, loadBundle
 import objc
-module = type(sys)
+ModuleType = type(sys)
 
 def _loadBundle(frameworkName, frameworkIdentifier, frameworkPath):
     if frameworkIdentifier is None:
         except AttributeError:
             raise KeyError(key)
 
-class ObjCLazyModule (module):
+class ObjCLazyModule (ModuleType):
 
     # Define slots for all attributes, that way they don't end up it __dict__.
     __slots__ = (
         self.__load_cftypes(metadict.get('cftypes'))
 
         if metadict.get('protocols') is not None:
-            self.__dict__['protocols'] = module('%s.protocols'%(name,))
+            self.__dict__['protocols'] = ModuleType('%s.protocols'%(name,))
             self.__dict__['protocols'].__dict__.update(
                     metadict['protocols'])
 

File pyobjc-core/NEWS.txt

 - Using NSArchiver or NSKeyedArchiver to encode and decode (pure) python objects didn't always
   work correctly. Found by improved unittests.
 
+- The implementation of cmp() for NSSet instances now matches the behavior of regular python
+  sets, that is calling ``cmp(anNSSet, aValue)`` will raise a TypeError exception unless
+  both arguments are the same object (``anNSSet is aValue``).
+
 
 Version 2.4.1
 -------------

File pyobjc-core/PyObjCTest/test_convenience.py

                 del convenience.CLASS_METHODS["MyObject"]
 
 
+
+class TestBasicConveniences (TestCase):
+    def testBundleForClass(self):
+        orig = convenience.currentBundle
+        try:
+            the_bundle = object()
+            def currentBundle():
+                return the_bundle
+            convenience.currentBundle = currentBundle
+
+            class OC_Test_Basic_Convenience_1 (objc.lookUpClass("NSObject")):
+                pass
+
+            self.assertIs(OC_Test_Basic_Convenience_1.bundleForClass(), the_bundle)
+        finally:
+            convenience.currentBundle = orig
+
+    def test_kvc_helper(self):
+        o = objc.lookUpClass('NSURL').URLWithString_('http://www.python.org/')
+        self.assertEqual(o.host(), 'www.python.org')
+
+        self.assertEqual(o._.host, 'www.python.org')
+        self.assertEqual(o._['host'], 'www.python.org')
+        self.assertRaises(TypeError, lambda: o._[42])
+        self.assertEqual(repr(o._), '<KVC accessor for %r>'%(o,))
+        self.assertRaises(AttributeError, o._.nosuchattr)
+        self.assertRaises(TypeError, o._.__setitem__, 42) 
+
+        o = objc.lookUpClass('NSMutableDictionary').dictionary()
+        o._.key1 = 1
+        o._['key2'] = 2
+
+        self.assertEqual(o, {'key1': 1, 'key2': 2 })
+        self.assertRaises(AttributeError, o._.nosuchattr)
+        self.assertRaises(TypeError, o._.__setitem__, 42) 
+
+
+
 # TODO: Explicit tests for add_convenience_methods.
 
 if __name__ == "__main__":

File pyobjc-core/PyObjCTest/test_set_interface.py

         else:
             self.assertNotEqual(id(s), id(z))
 
+    def test_creation_from_sets(self):
+        s1 = {1, 2, 3}
+        s2 = frozenset({3, 4, 5})
 
+        s = self.thetype(s1)
+        self.assertIsInstance(s, self.thetype)
+        self.assertEqual(s, s1)
+
+        s = self.thetype(s2)
+        self.assertIsInstance(s, self.thetype)
+        self.assertEqual(s, s2)
+
+        n  = self.thetype(s)
+        self.assertIsInstance(n, self.thetype)
+        self.assertEqual(n, s)
+        
     def test_copy(self):
         dup = self.s.copy()
         self.assertEqual(id(self.s), id(dup))
 
+    def test_as_list(self):
+        l = list(self.s)
+        self.assertIsInstance(l, list)
+        self.assertEqual(len(l), len(self.s))
+        for item in l:
+            self.assertIn(item, self.s)
+
+    @onlyPython2
+    def test_cmp_function(self):
+        self.assertRaises(TypeError, cmp, self.s, 'hello')
+        self.assertRaises(TypeError, self.s.__cmp__, self.s)
+
+
+
 class TestMutableSet (TestSet, test.test_set.TestSet):
     thetype = NSMutableSet
     basetype = NSMutableSet