root/gaphor/tags/gaphor-0.2.0/uml2/test-properties.py

Revision 192, 5.7 kB (checked in by arjanmol, 6 years ago)

*** empty log message ***

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/env python
2 # vim:sw=4:et
3
4 from properties import *
5
6 def test_associations():
7     #
8     # 1:-
9     #
10     class A(object): pass
11     class B(object): pass
12     class C(object): pass
13
14     A.one = association('one', B, 0, 1, 'two')
15     B.two = association('two', A, 0, 1)
16
17     a = A()
18     b = B()
19     a.one = b
20     assert a.one is b
21     assert b.two is a
22
23     #
24     # n:-
25     #
26     class A(object): pass
27     class B(object): pass
28     class C(object): pass
29
30     A.one = association('one', B, 0, infinite, 'two')
31     B.two = association('two', A, 0, 1)
32
33     a = A()
34     b = B()
35     a.one = b
36     assert b in a.one
37     assert b.two is a
38
39     #
40     # 1:1
41     #
42     class A(object): pass
43     class B(object): pass
44     class C(object): pass
45
46     A.one = association('one', B, 0, 1, 'two')
47     B.two = association('two', A, 0, 1, 'one')
48
49     a = A()
50     b = B()
51     a.one = b
52
53     assert a.one is b
54     assert b.two is a
55     a.one = B()
56     assert a.one is not b
57     assert b.two is None
58
59     c = C()
60     try:
61         a.one = c
62     except Exception, e:
63         pass #ok print 'exception caught:', e
64     else:
65         assert a.one is not c
66
67     del a.one
68     assert a.one is None
69     assert b.two is None
70
71     #
72     # 1:n
73     #
74     class A(object): pass
75     class B(object): pass
76     class C(object): pass
77
78     A.one = association('one', B, 0, 1, 'two')
79     B.two = association('two', A, 0, infinite, 'one')
80
81     a1 = A()
82     a2 = A()
83     b1 = B()
84     b2 = B()
85
86     b1.two = a1
87     assert a1 in b1.two
88     assert a1.one is b1
89
90     b1.two = a2
91     assert a1 in b1.two
92     assert a2 in b1.two
93     assert a1.one is b1
94     assert a2.one is b1
95
96     try:
97         del b1.two
98     except Exception:
99         pass #ok
100     else:
101         assert b1.two != []
102
103     assert a1 in b1.two
104     assert a2 in b1.two
105     assert a1.one is b1
106     assert a2.one is b1
107
108     b1.two.remove(a1)
109
110     assert len(b1.two) == 1
111     assert a1 not in b1.two
112     assert a2 in b1.two
113     assert a1.one is None
114     assert a2.one is b1
115
116     a2.one = b2
117
118     assert len(b1.two) == 0
119     assert len(b2.two) == 1
120     assert a2 in b2.two
121     assert a1.one is None
122     assert a2.one is b2
123
124     try:
125         b1.two.remove(a1)
126     except AttributeError:
127         pass #ok
128     else:
129         assert 0, 'should not be removed'
130     #
131     # n:n
132     #
133     class A(object): pass
134     class B(object): pass
135     class C(object): pass
136
137     A.one = association('one', B, 0, infinite, 'two')
138     B.two = association('two', A, 0, infinite, 'one')
139
140     a1 = A()
141     a2 = A()
142     b1 = B()
143     b2 = B()
144
145     a1.one = b1
146     assert b1 in a1.one
147     assert a1 in b1.two
148     assert not a2.one
149     assert not b2.two
150
151     a1.one = b2
152     assert b1 in a1.one
153     assert b2 in a1.one
154     assert a1 in b1.two
155     assert a1 in b2.two
156     assert not a2.one
157
158     a2.one = b1
159     assert len(a1.one) == 2
160     assert len(a2.one) == 1
161     assert len(b1.two) == 2
162     assert len(b2.two) == 1
163     assert b1 in a1.one
164     assert b2 in a1.one
165     assert a1 in b1.two
166     assert a1 in b2.two
167     assert b1 in a2.one
168     assert a2 in b1.two
169
170     del a1.one[b1]
171     assert len(a1.one) == 1
172     assert len(a2.one) == 1
173     assert len(b1.two) == 1
174     assert len(b2.two) == 1
175     assert b1 not in a1.one
176     assert b2 in a1.one
177     assert a1 not in b1.two
178     assert a1 in b2.two
179     assert b1 in a2.one
180     assert a2 in b1.two
181
182 def test_attributes():
183     import types
184     class A(object): pass
185
186     A.a = attribute('a', types.StringType, 'default')
187
188     a = A()
189     assert a.a == 'default'
190     a.a = 'bar'
191     assert a.a == 'bar'
192     del a.a
193     assert a.a == 'default'
194     try:
195         a.a = 1
196     except AttributeError:
197         pass #ok
198     else:
199         assert 0, 'should not set integer'
200
201 def test_enumerations():
202     import types
203     class A(object): pass
204
205     A.a = enumeration('a', ('one', 'two', 'three'), 'one')
206     a = A()
207     assert a.a == 'one'
208     a.a = 'two'
209     assert a.a == 'two'
210     a.a = 'three'
211     assert a.a == 'three'
212     try:
213         a.a = 'four'
214     except AttributeError:
215         assert a.a == 'three'
216     else:
217         assert 0, 'a.a could not be four'
218     del a.a
219     assert a.a == 'one'
220
221 def test_notify():
222     import types
223     class A(object):
224         notified=None
225         def notify(self, name):
226             self.notified = name
227
228     A.assoc = association('assoc', A)
229     A.attr = attribute('attr', types.StringType, 'default')
230     A.enum = enumeration('enum', ('one', 'two'), 'one')
231
232     a = A()
233     assert a.notified == None
234     a.assoc = A()
235     assert a.notified == 'assoc'
236     a.attr = 'newval'
237     assert a.notified == 'attr'
238     a.enum = 'two'
239     assert a.notified == 'enum'
240     a.notified = None
241     a.enum = 'two' # should not notify
242     assert a.notified == None
243
244 def test_derivedunion():
245     class A(object): pass
246
247     A.a = association('a', A)
248     A.b = association('b', A, 0, 1)
249     A.u = derivedunion('u', A.a, A.b)
250
251     a = A()
252     assert len(a.a) == 0, 'a.a = %s' % a.a
253     assert len(a.u) == 0, 'a.u = %s' % a.u
254     a.a = b = A()
255     a.a = c = A()
256     assert len(a.a) == 2, 'a.a = %s' % a.a
257     assert b in a.a
258     assert c in a.a
259     assert len(a.u) == 2, 'a.u = %s' % a.u
260     assert b in a.u
261     assert c in a.u
262
263     a.b = d = A()
264     assert len(a.a) == 2, 'a.a = %s' % a.a
265     assert b in a.a
266     assert c in a.a
267     assert d == a.b
268     assert len(a.u) == 3, 'a.u = %s' % a.u
269     assert b in a.u
270     assert c in a.u
271     assert d in a.u
272
273     class E(object):
274         notified=False
275         def notify(self, name):
276             if name == 'u':
277                 self.notified = True
278
279     E.a = association('a', A)
280     E.u = derivedunion('u', E.a)
281
282     e = E()
283     assert e.notified == False
284     e.a = a
285     assert e.notified == True
286
287 if __name__ == '__main__':
288     test_associations()
289     test_attributes()
290     test_enumerations()
291     test_notify()
292     test_derivedunion()
293     print 'All tests passed.'
Note: See TracBrowser for help on using the browser.