Пример #1
0
    def testReading(self):
        obj = ClassWithVariables.alloc().init()

        getter = objc.getInstanceVariable

        cls = getter(obj, 'isa')
        self.assertIs(cls, type(obj))

        self.assertEqual(getter(obj, 'intValue'), 42)
        self.assertIsInstance(getter(obj, 'intValue'), int)

        self.assertEqual(getter(obj, 'floatValue'), -10.055)
        self.assertIsInstance(getter(obj, 'floatValue'), float)

        self.assertEqual(getter(obj, 'charValue'), ord('a'))
        self.assertIsInstance(getter(obj, 'charValue'), int)

        self.assertEqual(getter(obj, 'strValue'), b"hello world")
        self.assertIsInstance(getter(obj, 'strValue'), bytes)

        self.assertIsInstance(getter(obj, 'objValue'), NSObject)

        self.assertIsNone(getter(obj, 'nilValue'))

        self.assertEqual(getter(obj, 'pyValue'), slice(1, 10, 4))
        self.assertIsInstance(getter(obj, 'pyValue'), slice)

        self.assertEqual(getter(obj, 'rectValue'), ((1, 2), (3, 4)))

        self.assertRaises(AttributeError, getter, obj, "noSuchMember")
Пример #2
0
    def testDir(self):
        obj = ClassWithVariables.alloc().init()

        # Note: cannot check the exact contents of dir(), who knows
        # what NSObject defines...
        v = objc.listInstanceVariables(obj)
        self.assertIn(('charValue', objc._C_CHR), v)
        self.assertIn(('intValue', objc._C_INT), v)
        self.assertIn(('isa', objc._C_CLASS), v)
Пример #3
0
    def testWriting(self):
        obj = ClassWithVariables.alloc().init()

        getter = objc.getInstanceVariable
        setter = objc.setInstanceVariable

        self.assertEqual(getter(obj, 'intValue'), 42)
        setter(obj, 'intValue', 99)
        self.assertEqual(getter(obj, 'intValue'), 99)

        self.assertEqual(getter(obj, 'floatValue'), -10.055)
        setter(obj, 'floatValue', 0.5)
        self.assertEqual(getter(obj, 'floatValue'), 0.5)

        self.assertEqual(getter(obj, 'charValue'), ord('a'))
        setter(obj, 'charValue', b'b')
        self.assertEqual(getter(obj, 'charValue'), ord('b'))
        setter(obj, 'charValue', 10)
        self.assertEqual(getter(obj, 'charValue'), 10)

        self.assertEqual(getter(obj, 'strValue'), b"hello world")
        setter(obj, 'strValue', b"foo bar")
        self.assertEqual(getter(obj, 'strValue'), b"foo bar")
        setter(obj, 'strValue', None)
        self.assertEqual(getter(obj, 'strValue'), None)

        o = NSObject.new()
        self.assertIsNot(getter(obj, 'objValue'), o)
        self.assertRaises(TypeError, setter, 'objValue', o)
        self.assertIsNot(getter(obj, 'objValue'), o)
        setter(obj, 'objValue', o, True)
        self.assertIs(getter(obj, 'objValue'), o)

        o2 = NSObject.new()
        o2.retain()
        self.assertIsNot(getter(obj, 'objValue'), o2)
        setter(obj, 'objValue', o2, False)
        self.assertIs(getter(obj, 'objValue'), o2)

        self.assertEqual(getter(obj, 'pyValue'), slice(1, 10, 4))
        setter(obj, 'pyValue', [1,2,3])
        self.assertEqual(getter(obj, 'pyValue'), [1,2,3])

        self.assertEqual(getter(obj, 'rectValue'), ((1, 2), (3, 4)))
        setter(obj, 'rectValue', ((-4, -8), (2, 7)))
        self.assertEqual(getter(obj, 'rectValue'), ((-4, -8), (2, 7)))

        self.assertRaises(AttributeError, setter, obj, "noSuchMember", 'foo')