1from PyObjCTools.TestSupport import * 2from PyObjCTest.testbndl import OC_TestClass2 3import objc 4import collections 5import sys 6 7if sys.version_info[0] == 2: 8 from UserList import UserList 9 from UserDict import IterableUserDict 10 11else: 12 from collections import UserDict as IterableUserDict, UserList 13 14NSMutableArray = objc.lookUpClass("NSMutableArray") 15NSMutableDictionary = objc.lookUpClass("NSMutableDictionary") 16 17def classOfProxy(value): 18 return OC_TestClass2.classOfObject_(value) 19 20 21class TestBridges (TestCase): 22 # NOTE: the two "register" functions from objc._bridges aren't 23 # tested explictly, but the tests in this class do verify that 24 # the default registrations (which are made through those two 25 # functions) work properly. 26 27 def test_xrange(self): 28 range_type = range if sys.version_info[0] == 3 else xrange 29 30 v = range_type(0, 10) 31 self.assertTrue(issubclass(classOfProxy(v), NSMutableArray)) 32 33 def test_user_collectons(self): 34 # Note: Not "UserDict" because UserDict doesn't implement 35 # __iter__ and hence isn't a collections.Mapping, and doesn't 36 # implement enough API to implement the NSDictionary interface. 37 v = IterableUserDict() 38 self.assertTrue(issubclass(classOfProxy(v), NSMutableDictionary)) 39 40 v = UserList() 41 self.assertTrue(issubclass(classOfProxy(v), NSMutableArray)) 42 43 def test_abc(self): 44 class MySequence (collections.Sequence): 45 def __getitem__(self, idx): 46 raise IndexError(idx) 47 48 def __len__(self): 49 return 0 50 51 class MyDictionary (collections.Mapping): 52 def __getitem__(self, key): 53 raise KeyError(key) 54 55 def __len__(self): 56 return 0 57 58 def __iter__(self): 59 return 60 yield 61 62 v = MyDictionary() 63 self.assertTrue(issubclass(classOfProxy(v), NSMutableDictionary)) 64 65 v = MySequence() 66 self.assertTrue(issubclass(classOfProxy(v), NSMutableArray)) 67 68 69if __name__ == "__main__": 70 main() 71