~divmod-dev/divmod.org/dangling-1091

« back to all changes in this revision

Viewing changes to Axiom/axiom/test/test_slotmachine.py

  • Committer: glyph
  • Date: 2005-07-28 22:09:16 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:2
move this repository to a more official-looking URL

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from twisted.trial import unittest
 
3
from axiom.slotmachine import SetOnce, Attribute, SlotMachine, SchemaMachine
 
4
 
 
5
class A(SlotMachine):
 
6
    slots = ['a', 'initialized']
 
7
 
 
8
class B(SchemaMachine):
 
9
    test = Attribute()
 
10
    initialized = SetOnce()
 
11
    other = SetOnce(default=None)
 
12
 
 
13
class Bsub(B):
 
14
    pass
 
15
 
 
16
class C(object):
 
17
    __slots__ = ['a', 'b',
 
18
                 'c', 'initialized']
 
19
 
 
20
class D:
 
21
    def activate(self):
 
22
        self.initialized = 1
 
23
        self.test = 2
 
24
        self.a = 3
 
25
        self.b = 4
 
26
        self.c = 5
 
27
 
 
28
class E(object):
 
29
    pass
 
30
 
 
31
class X(B, A, C, D, E):
 
32
    pass
 
33
 
 
34
class Y(Bsub):
 
35
    blah = SetOnce()
 
36
 
 
37
class ClassWithDefault:
 
38
    x = 1
 
39
 
 
40
class DefaultTest(SchemaMachine, ClassWithDefault):
 
41
    x = Attribute()
 
42
 
 
43
 
 
44
class SlotMachineTest(unittest.TestCase):
 
45
 
 
46
    def btest(self, b):
 
47
        b.test = 1
 
48
        b.test = 2
 
49
 
 
50
        self.assertEquals(b.test, 2)
 
51
        self.assertRaises(AttributeError, setattr, b, 'nottest', 'anything')
 
52
        self.assertRaises(AttributeError, getattr, b, 'nottest')
 
53
        self.assertEquals(b.other, None)
 
54
        b.other = 7
 
55
        self.assertEquals(b.other, 7)
 
56
        self.assertRaises(AttributeError, setattr, b, 'other', 'anything')
 
57
 
 
58
    def testAttributesNotAllowed(self):
 
59
        b = B()
 
60
        self.btest(b)
 
61
 
 
62
    def testTrivialSubclass(self):
 
63
        b = Bsub()
 
64
        self.btest(b)
 
65
 
 
66
    def testSetOnce(self):
 
67
        b = B()
 
68
        b.initialized = 1
 
69
        self.assertRaises(AttributeError, setattr, b, 'initialized', 2)
 
70
        self.assertEquals(b.initialized, 1)
 
71
 
 
72
 
 
73
    def testClassicMixin(self):
 
74
        x = X()
 
75
        x.activate()
 
76
 
 
77
        self.assertRaises(AttributeError, setattr, x, 'initialized', 2)
 
78
        self.assertRaises(AttributeError, setattr, x, 'nottest', 'anything')
 
79
        self.assertRaises(AttributeError, getattr, x, 'nottest')
 
80
 
 
81
    def testAttributesTraverseDeepHierarchy(self):
 
82
        y = Y()
 
83
        self.btest(y)
 
84
 
 
85
    def testBaseDefault(self):
 
86
        dt = DefaultTest()
 
87
        # self.failUnless('x' in dt.__slots__, 'x not in '+repr(dt.__slots__) )
 
88
        dt.x = 2
 
89
 
 
90
 
 
91
 
 
92