Changeset 1057

Show
Ignore:
Timestamp:
10/30/06 23:51:42 (2 years ago)
Author:
arjanmol
Message:

unittest cleanup, first attempts to get storage working again.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphor/branches/new-canvas/ChangeLog

    r1044 r1057  
     12006-10-30  arjan <arjan at yirdis dot nl> 
     2 
     3        * gaphor/storage.py: some updates, time to get it working again. 
     4        * gaphor/tests/test_storage.py: unit tests for storage.py 
     5        * tests/*: moved several tests to gaphor/*/tests directory 
     6 
    172006-10-24  arjan <arjan at yirdis dot nl> 
    28 
  • gaphor/branches/new-canvas/gaphor/UML/__init__.py

    r513 r1057  
    2323def select(expression=None): 
    2424    """Query the default ElementFactory for items that comply with expression. 
     25    Returns an iterator. 
    2526    """ 
    2627    return _default_element_factory.select(expression) 
     28 
     29def lselect(expression=None): 
     30    """Query the default ElementFactory for items that comply with expression. 
     31    Returns a list of elements. 
     32    """ 
     33    return _default_element_factory.lselect(expression) 
    2734 
    2835def flush(): 
  • gaphor/branches/new-canvas/gaphor/UML/element.py

    r935 r1057  
    5858        """Save the state by calling save_func(name, value).""" 
    5959        umlprop = umlproperty 
    60         clazz = type(self) 
    61         for propname in dir(clazz): 
    62             prop = getattr(clazz, propname) 
    63             if isinstance(prop, umlprop): 
    64                 prop.save(self, save_func) 
     60        class_ = type(self) 
     61        for propname in dir(class_): 
     62            if not propname.startswith('_'): 
     63                prop = getattr(class_, propname) 
     64                if isinstance(prop, umlprop): 
     65                    prop.save(self, save_func) 
    6566 
    6667    def load(self, name, value): 
     
    144145 
    145146    def isKindOf(self, class_): 
    146         """Returns true if the object is an instance of clazz.""" 
     147        """Returns true if the object is an instance of class_.""" 
    147148        return isinstance(self, class_) 
    148149 
  • gaphor/branches/new-canvas/gaphor/UML/elementfactory.py

    r1044 r1057  
    107107                    yield e #l.append(e) 
    108108        #return l 
     109 
     110    def lselect(self, expression=None): 
     111        """Like select(), but returns a list. 
     112        """ 
     113        return list(self.select(expression)) 
    109114 
    110115    def keys(self): 
  • gaphor/branches/new-canvas/gaphor/UML/tests/test_properties.py

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

    r924 r1057  
    1111        b = UML.Element() 
    1212        def cb_func(name, *args): 
    13             print '  cb_func:', name, args 
     13            #print '  cb_func:', name, args 
     14            pass 
    1415 
    1516        a.connect('ev1', cb_func, a) 
  • gaphor/branches/new-canvas/gaphor/diagram/association.py

    r1044 r1057  
    1414# tail end and visa versa. 
    1515 
    16 from math import atan2, atan, pi, sin, cos 
     16from math import atan2, pi 
    1717 
    1818from gaphas.util import text_extents 
     
    119119    def save(self, save_func): 
    120120        DiagramLine.save(self, save_func) 
    121         self.save_property(save_func, 'show-direction'
     121        save_func('show-direction', self._show_direction
    122122        if self._head_end.subject: 
    123123            save_func('head-subject', self._head_end.subject) 
     
    740740     
    741741 
    742 def rotate(p1, p2, points): 
    743     """ 
    744     Rotate points around p1. Rotation angle is determined by line (p0, p1). 
    745     Every point is moved into p1 + (10, 0) after rotation. 
    746     """ 
    747     try: 
    748         angle = atan((p1[1] - p2[1]) / (p1[0] - p2[0])) 
    749     except ZeroDivisionError: 
    750         angle = pi * 1.5 
    751          
    752  
    753     sin_angle = sin(angle) 
    754     cos_angle = cos(angle) 
    755  
    756     def r(a, b, x, y): 
    757         return (cos_angle * a - sin_angle * b + x, 
    758                 sin_angle * a + cos_angle * b + y) 
    759  
    760     x0 = p1[0] < p2[0] and 10 or -10 
    761     y0 = 0 
    762  
    763     # rotate around the (10, 0) 
    764     x0, y0 = r(x0, y0, 0, 0) 
    765  
    766     # move to the destination point 
    767     x0 += p1[0] 
    768     y0 += p1[1] 
    769  
    770     return [ r(x, y, x0, y0) for x, y in points ] 
    771  
    772  
    773742# vim:sw=4:et 
  • gaphor/branches/new-canvas/gaphor/diagram/comment.py

    r1037 r1057  
    2727        self.height = 50 
    2828        self.width = 100 
    29  
    30     def postload(self): 
    31         #self._body.set_text(self.subject.body or '') 
    32         pass 
    3329 
    3430    def edit(self): 
  • gaphor/branches/new-canvas/gaphor/diagram/diagramitem.py

    r1051 r1057  
    7373        # save persistent properties 
    7474        for p in self._persistent_props: 
    75             save_func(p, getattr(self.props, p)) 
     75            save_func(p, getattr(self, p.replace('-', '_'))) 
    7676 
    7777 
     
    9494        """Save a property, this is a shorthand method. 
    9595        """ 
    96         save_func(name, self.get_property(name)) 
     96        save_func(name, getattr(self, name.replace('-', '_'))) 
    9797 
    9898    def save_properties(self, save_func, *names): 
  • gaphor/branches/new-canvas/gaphor/diagram/elementitem.py

    r1049 r1057  
    1212 
    1313class ElementItem(gaphas.Element, DiagramItem): 
     14 
    1415    def __init__(self, id=None): 
    1516        gaphas.Element.__init__(self) 
     
    1819 
    1920    def save(self, save_func): 
    20         for prop in ('affine', 'width', 'height', 'auto-resize'): 
     21        save_func('matrix', tuple(self.matrix)) 
     22        for prop in ('width', 'height'): 
    2123            self.save_property(save_func, prop) 
    2224        DiagramItem.save(self, save_func) 
    2325 
    2426    def load(self, name, value): 
    25         DiagramItem.load(self, name, value) 
     27        if name == 'matrix': 
     28            self.matrix(*eval(value)) 
     29        else: 
     30            DiagramItem.load(self, name, value) 
    2631 
    2732 
  • gaphor/branches/new-canvas/gaphor/storage.py

    r930 r1057  
    2626from gaphor import parser 
    2727from gaphor import diagram 
     28from gaphor.diagram import items 
    2829from gaphor import resource 
    2930from gaphor.i18n import _ 
     
    113114        """ 
    114115        #log.debug('saving canvasitem: %s|%s %s' % (name, value, type(value))) 
    115         if reference or isinstance(value, UML.Element)
     116        if reference
    116117            save_reference(name, value) 
    117118        elif isinstance(value, UML.collection): 
     
    122123            value.save(save_canvasitem) 
    123124            writer.endElement('item') 
     125        elif isinstance(value, UML.Element): 
     126            save_reference(name, value) 
    124127        else: 
    125128            save_value(name, value) 
     
    185188                raise 
    186189        elif isinstance(elem, parser.canvasitem): 
    187             cls = getattr(diagram, elem.type) 
     190            cls = getattr(items, elem.type) 
    188191            #log.debug('Creating canvas item for %s' % elem) 
    189192            elem.element = diagram.create_as(cls, id) 
     
    203206            for item in elem.canvas.canvasitems: 
    204207                assert item in elements.values(), 'Item %s (%s) is a canvas item, but it is not in the parsed objects table' % (item, item.id) 
    205                 item.element.set_property('parent', elem.element.canvas.root) 
     208                #item.element.set_property('parent', elem.element.canvas.root) 
    206209        if isinstance(elem, parser.canvasitem): 
    207210            for item in elem.canvasitems: 
    208211                assert item in elements.values(), 'Item %s (%s) is a canvas item, but it is not in the parsed objects table' % (item, item.id) 
    209                 item.element.set_property('parent', elem.element) 
     212                #item.element.set_property('parent', elem.element) 
    210213 
    211214        # load attributes and references: