Module ObjectTest
[hide private]
[frames] | no frames]

Source Code for Module ObjectTest

  1  ##################################################################### 
  2  # -*- coding: iso-8859-1 -*-                                        # 
  3  #                                                                   # 
  4  # Frets on Fire                                                     # 
  5  # Copyright (C) 2006 Sami Kyöstilä                                  # 
  6  #                                                                   # 
  7  # This program is free software; you can redistribute it and/or     # 
  8  # modify it under the terms of the GNU General Public License       # 
  9  # as published by the Free Software Foundation; either version 2    # 
 10  # of the License, or (at your option) any later version.            # 
 11  #                                                                   # 
 12  # This program is distributed in the hope that it will be useful,   # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of    # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     # 
 15  # GNU General Public License for more details.                      # 
 16  #                                                                   # 
 17  # You should have received a copy of the GNU General Public License # 
 18  # along with this program; if not, write to the Free Software       # 
 19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        # 
 20  # MA  02110-1301, USA.                                              # 
 21  ##################################################################### 
 22   
 23  import unittest 
 24  import Object 
 25   
26 -class TestMessage(Object.Message):
27 pass
28 29 manager = Object.Manager() 30
31 -class TestObject(Object.Object):
32 - def __init__(self, id = None, name = "unnamed", manager = manager):
33 Object.Object.__init__(self, id, name = name, manager = manager) 34 self.x = 1 35 self.y = 2 36 self.z = 3 37 self.name = name 38 self.share("x", "y", "z", "name") 39 self.connect(TestMessage, self.message)
40
41 - def message(self, message):
42 self.lastMessage = message
43
44 -class ObjectTest(unittest.TestCase):
45 - def testAttributes(self):
46 o = TestObject() 47 o.x = 1234 48 data = o.getChanges() 49 assert data 50 assert not o.getChanges() 51 o.applyChanges(data) 52 53 o2 = TestObject() 54 o2.applyChanges(data) 55 assert o.x == o2.x 56 o2.x = 5678 57 o.applyChanges(o2.getChanges()) 58 assert not o.getChanges() 59 assert not o2.getChanges() 60 assert o.x == o2.x
61
62 - def testMessaging(self):
63 o = TestObject() 64 o2 = TestObject() 65 66 o.emit(TestMessage()) 67 o2.applyChanges(o.getChanges()) 68 69 assert isinstance(o.lastMessage, TestMessage) 70 assert isinstance(o2.lastMessage, TestMessage)
71
73 o = TestObject(name = "first") 74 o.x = 31337 75 76 id = manager.id 77 objId = o.id 78 data = manager.getChanges(everything = True) 79 assert data 80 81 manager.reset() 82 manager.setId(1) 83 manager.applyChanges(id, data) 84 85 o2 = manager.objects[objId] 86 assert o2.x == o.x 87 assert o2 is not o 88 89 o3 = TestObject(name = "third") 90 obj3Id = o3.id 91 o3.x = 0xdada 92 93 o4 = TestObject(name = "fourth") 94 obj4Id = o4.id 95 o4.delete() 96 97 # read the differential states 98 manager.getChanges() 99 100 data = manager.getChanges(everything = True) 101 102 manager.reset() 103 manager.setId(2) 104 manager.applyChanges(id, data) 105 106 assert objId in manager.objects 107 assert obj3Id in manager.objects 108 assert obj4Id not in manager.objects 109 assert manager.objects[obj3Id].x == 0xdada
110
111 - def testReferences(self):
112 o = TestObject(name = "bag") 113 o2 = TestObject(name = "apple") 114 o.x = [o2] 115 ids = [o.id, o2.id] 116 117 data = manager.getChanges() 118 manager.reset() 119 manager.setId(1) 120 manager.applyChanges(id, data) 121 122 o = manager.objects[ids[0]] 123 o2 = manager.objects[ids[1]] 124 125 assert o.name == "bag" 126 assert o2.name == "apple" 127 assert o2 in o.x
128
129 - def testMultipleManagers(self):
130 m1 = Object.Manager(1000) 131 m2 = Object.Manager(2000) 132 133 o1 = TestObject(manager = m1) 134 o2 = TestObject(manager = m2) 135 136 m1.applyChanges(m2.id, m2.getChanges()) 137 m2.applyChanges(m1.id, m1.getChanges()) 138 139 assert len(m1.objects) == 2 140 assert len(m2.objects) == 2
141
142 - def tearDown(self):
143 manager.reset()
144 145 if __name__ == "__main__": 146 unittest.main() 147