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

Revision 196, 7.4 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 from element import Element
6
7 def test_associations():
8     #
9     # 1:-
10     #
11     class A(Element): pass
12     class B(Element): pass
13     class C(Element): pass
14
15     A.one = association('one', B, 0, 1, 'two')
16     B.two = association('two', A, 0, 1)
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(Element): pass
27     class B(Element): pass
28     class C(Element): 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(Element): pass
43     class B(Element): pass
44     class C(Element): 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     a.one = b
53
54     assert a.one is b
55     assert b.two is a
56     assert len(a._observers.get('__unlink__')) == 1
57     assert len(b._observers.get('__unlink__')) == 1
58
59     a.one = B()
60     assert a.one is not b
61     assert b.two is None
62     assert len(a._observers.get('__unlink__')) == 1
63     assert len(b._observers.get('__unlink__')) == 0
64
65     c = C()
66     try:
67         a.one = c
68     except Exception, e:
69         pass #ok print 'exception caught:', e
70     else:
71         assert a.one is not c
72
73     del a.one
74     assert a.one is None
75     assert b.two is None
76     assert len(a._observers.get('__unlink__')) == 0
77     assert len(b._observers.get('__unlink__')) == 0
78
79     #
80     # 1:n
81     #
82     class A(Element): pass
83     class B(Element): pass
84     class C(Element): pass
85
86     A.one = association('one', B, 0, 1, 'two')
87     B.two = association('two', A, 0, infinite, 'one')
88
89     a1 = A()
90     a2 = A()
91     b1 = B()
92     b2 = B()
93
94     b1.two = a1
95     b1.two = a1
96     b1.two = a1
97     assert len(b1.two) == 1, 'len(b1.two) == %d' % len(b1.two)
98     assert a1 in b1.two
99     assert a1.one is b1
100     assert len(a1._observers.get('__unlink__')) == 1
101     assert len(b1._observers.get('__unlink__')) == 1
102
103     b1.two = a2
104     assert a1 in b1.two
105     assert a2 in b1.two
106     assert a1.one is b1
107     assert a2.one is b1
108
109     try:
110         del b1.two
111     except Exception:
112         pass #ok
113     else:
114         assert b1.two != []
115
116     assert a1 in b1.two
117     assert a2 in b1.two
118     assert a1.one is b1
119     assert a2.one is b1
120
121     b1.two.remove(a1)
122
123     assert len(b1.two) == 1
124     assert a1 not in b1.two
125     assert a2 in b1.two
126     assert a1.one is None
127     assert a2.one is b1
128     assert len(a1._observers.get('__unlink__')) == 0
129     assert len(b1._observers.get('__unlink__')) == 1
130
131     a2.one = b2
132
133     assert len(b1.two) == 0
134     assert len(b2.two) == 1
135     assert a2 in b2.two
136     assert a1.one is None
137     assert a2.one is b2
138
139     try:
140         b1.two.remove(a1)
141     except AttributeError:
142         pass #ok
143     else:
144         assert 0, 'should not be removed'
145     #
146     # n:n
147     #
148     class A(Element): pass
149     class B(Element): pass
150     class C(Element): pass
151
152     A.one = association('one', B, 0, infinite, 'two')
153     B.two = association('two', A, 0, infinite, 'one')
154
155     a1 = A()
156     a2 = A()
157     b1 = B()
158     b2 = B()
159
160     a1.one = b1
161     assert b1 in a1.one
162     assert a1 in b1.two
163     assert not a2.one
164     assert not b2.two
165
166     a1.one = b2
167     assert b1 in a1.one
168     assert b2 in a1.one
169     assert a1 in b1.two
170     assert a1 in b2.two
171     assert not a2.one
172     assert len(a1._observers.get('__unlink__')) == 2
173     assert len(b1._observers.get('__unlink__')) == 1
174
175     a2.one = b1
176     assert len(a1.one) == 2
177     assert len(a2.one) == 1
178     assert len(b1.two) == 2
179     assert len(b2.two) == 1
180     assert b1 in a1.one
181     assert b2 in a1.one
182     assert a1 in b1.two
183     assert a1 in b2.two
184     assert b1 in a2.one
185     assert a2 in b1.two
186
187     del a1.one[b1]
188     assert len(a1.one) == 1
189     assert len(a2.one) == 1
190     assert len(b1.two) == 1
191     assert len(b2.two) == 1
192     assert b1 not in a1.one
193     assert b2 in a1.one
194     assert a1 not in b1.two
195     assert a1 in b2.two
196     assert b1 in a2.one
197     assert a2 in b1.two
198     assert len(a1._observers.get('__unlink__')) == 1
199     assert len(b1._observers.get('__unlink__')) == 1
200
201     #
202     # unlink
203     #
204     class A(Element): pass
205     class B(Element): pass
206     class C(Element): pass
207
208     A.one = association('one', B, 0, infinite, 'two')
209     B.two = association('two', A, 0, infinite)
210
211     a1 = A()
212     a2 = A()
213     b1 = B()
214     b2 = B()
215
216     a1.one = b1
217     a1.one = b2
218     assert b1 in a1.one
219     assert b2 in a1.one
220     assert a1 in b1.two
221     assert a1 in b2.two
222
223     a2.one = b1
224     assert len(a1._observers.get('__unlink__')) == 2
225     assert len(b1._observers.get('__unlink__')) == 2
226
227     # remove b1 from all elements connected to b1
228     # also the signal should be removed
229     b1.unlink()
230
231     assert len(a1._observers.get('__unlink__')) == 1
232     assert len(b1._observers.get('__unlink__')) == 1
233
234     assert b1 not in a1.one
235     assert b2 in a1.one
236     assert a1 not in b1.two
237     assert a1 in b2.two
238
239 def test_attributes():
240     import types
241     class A(Element): pass
242
243     A.a = attribute('a', types.StringType, 'default')
244
245     a = A()
246     assert a.a == 'default'
247     a.a = 'bar'
248     assert a.a == 'bar'
249     del a.a
250     assert a.a == 'default'
251     try:
252         a.a = 1
253     except AttributeError:
254         pass #ok
255     else:
256         assert 0, 'should not set integer'
257
258 def test_enumerations():
259     import types
260     class A(Element): pass
261
262     A.a = enumeration('a', ('one', 'two', 'three'), 'one')
263     a = A()
264     assert a.a == 'one'
265     a.a = 'two'
266     assert a.a == 'two'
267     a.a = 'three'
268     assert a.a == 'three'
269     try:
270         a.a = 'four'
271     except AttributeError:
272         assert a.a == 'three'
273     else:
274         assert 0, 'a.a could not be four'
275     del a.a
276     assert a.a == 'one'
277
278 def test_notify():
279     import types
280     class A(Element):
281         notified=None
282         def notify(self, name):
283             self.notified = name
284
285     A.assoc = association('assoc', A)
286     A.attr = attribute('attr', types.StringType, 'default')
287     A.enum = enumeration('enum', ('one', 'two'), 'one')
288
289     a = A()
290     assert a.notified == None
291     a.assoc = A()
292     assert a.notified == 'assoc'
293     a.attr = 'newval'
294     assert a.notified == 'attr'
295     a.enum = 'two'
296     assert a.notified == 'enum'
297     a.notified = None
298     a.enum = 'two' # should not notify since value hasn't changed.
299     assert a.notified == None
300
301 def test_derivedunion():
302     class A(Element): pass
303
304     A.a = association('a', A)
305     A.b = association('b', A, 0, 1)
306     A.u = derivedunion('u', A.a, A.b)
307
308     a = A()
309     assert len(a.a) == 0, 'a.a = %s' % a.a
310     assert len(a.u) == 0, 'a.u = %s' % a.u
311     a.a = b = A()
312     a.a = c = A()
313     assert len(a.a) == 2, 'a.a = %s' % a.a
314     assert b in a.a
315     assert c in a.a
316     assert len(a.u) == 2, 'a.u = %s' % a.u
317     assert b in a.u
318     assert c in a.u
319
320     a.b = d = A()
321     assert len(a.a) == 2, 'a.a = %s' % a.a
322     assert b in a.a
323     assert c in a.a
324     assert d == a.b
325     assert len(a.u) == 3, 'a.u = %s' % a.u
326     assert b in a.u
327     assert c in a.u
328     assert d in a.u
329
330     class E(Element):
331         notified=False
332         def notify(self, name):
333             if name == 'u':
334                 self.notified = True
335
336     E.a = association('a', A)
337     E.u = derivedunion('u', E.a)
338
339     e = E()
340     assert e.notified == False
341     e.a = a
342     assert e.notified == True
343
344 if __name__ == '__main__':
345     test_associations()
346     test_attributes()
347     test_enumerations()
348     test_notify()
349     test_derivedunion()
350     print 'All tests passed.'
Note: See TracBrowser for help on using the browser.