1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
"""Slot tests
Made for Jython.
"""
from test import test_support
import unittest
# The strict tests fail on PyPy (but work on CPython and Jython).
# They're questionable
strict = True
class SlottedTestCase(unittest.TestCase):
def test_slotted(self):
class Foo(object):
__slots__ = 'bar'
self.assert_('__dict__' not in Foo.__dict__)
foo = Foo()
self.assert_(not hasattr(foo, '__dict__'))
foo.bar = 'hello bar'
self.assertEqual(foo.bar, 'hello bar')
self.assertRaises(AttributeError, setattr, foo, 'foo', 'hello foo')
class Baz(object):
__slots__ = ['python', 'jython']
self.assert_('__dict__' not in Baz.__dict__)
baz = Baz()
self.assert_(not hasattr(baz, '__dict__'))
baz.python = 'hello python'
baz.jython = 'hello jython'
self.assertEqual(baz.python, 'hello python')
self.assertEqual(baz.jython, 'hello jython')
self.assertRaises(AttributeError, setattr, baz, 'foo', 'hello')
class SlottedWithDictTestCase(unittest.TestCase):
def test_subclass(self):
class Base(object):
pass
class Foo(Base):
__slots__ = 'bar'
self.assert_('__dict__' not in Foo.__dict__)
foo = Foo()
self.assert_(hasattr(foo, '__dict__'))
foo.bar = 'hello bar'
foo.foo = 'hello foo'
self.assertEqual(foo.bar, 'hello bar')
self.assertEqual(foo.__dict__, {'foo': 'hello foo'})
def test_subclass_mro(self):
class Base(object):
pass
class Slotted(object):
__slots__ = 'baz'
class Foo(Slotted, Base):
__slots__ = 'bar'
if strict:
self.assert_('__dict__' in Foo.__dict__)
self.assertEqual(Foo.__dict__['__dict__'].__objclass__, Foo)
foo = Foo()
self.assert_(hasattr(foo, '__dict__'))
foo.bar = 'hello bar'
foo.baz = 'hello baz'
foo.foo = 'hello foo'
self.assertEqual(foo.bar, 'hello bar')
self.assertEqual(foo.baz, 'hello baz')
self.assertEqual(foo.__dict__, {'foo': 'hello foo'})
class Bar(Slotted, Base):
pass
if strict:
self.assert_('__dict__' in Bar.__dict__)
self.assertEqual(Bar.__dict__['__dict__'].__objclass__, Bar)
bar = Bar()
self.assert_(hasattr(bar, '__dict__'))
bar.bar = 'hello bar'
bar.baz = 'hello baz'
bar.foo = 'hello foo'
self.assertEqual(bar.bar, 'hello bar')
self.assertEqual(bar.baz, 'hello baz')
self.assertEqual(bar.__dict__, {'foo': 'hello foo', 'bar': 'hello bar'})
def test_subclass_oldstyle(self):
class OldBase:
pass
class Foo(OldBase, object):
__slots__ = 'bar'
if strict:
self.assert_('__dict__' in Foo.__dict__)
self.assertEqual(Foo.__dict__['__dict__'].__objclass__, Foo)
foo = Foo()
self.assert_(hasattr(foo, '__dict__'))
foo.bar = 'hello bar'
foo.foo = 'hello foo'
self.assertEqual(foo.bar, 'hello bar')
self.assertEqual(foo.__dict__, {'foo': 'hello foo'})
class Bar(OldBase, object):
__slots__ = '__dict__'
self.assert_('__dict__' in Bar.__dict__)
self.assertEqual(Bar.__dict__['__dict__'].__objclass__, Bar)
bar = Bar()
self.assert_(hasattr(bar, '__dict__'))
bar.bar = 'hello bar'
bar.foo = 'hello foo'
self.assertEqual(bar.bar, 'hello bar')
self.assertEqual(bar.__dict__, {'foo': 'hello foo', 'bar': 'hello bar'})
def test_mixin_oldstyle(self):
class OldBase:
pass
class NewBase(object):
pass
class Baz(NewBase, OldBase):
__slots__ = 'baz'
self.assert_('__dict__' not in Baz.__dict__)
baz = Baz()
self.assert_(hasattr(baz, '__dict__'))
baz.baz = 'hello baz'
baz.bar = 'hello bar'
self.assertEqual(baz.baz, 'hello baz')
self.assertEqual(baz.bar, 'hello bar')
self.assertEqual(baz.__dict__, {'bar': 'hello bar'})
class SlottedWithWeakrefTestCase(unittest.TestCase):
def test_subclass_oldstyle(self):
class OldBase:
pass
class Foo(OldBase, object):
__slots__ = '__dict__'
self.assert_(hasattr(Foo, '__weakref__'))
def test_main():
test_support.run_unittest(SlottedTestCase,
SlottedWithDictTestCase,
SlottedWithWeakrefTestCase)
if __name__ == '__main__':
test_main()
|